1 | # PyKota |
---|
2 | # |
---|
3 | # PyKota : Print Quotas for CUPS |
---|
4 | # |
---|
5 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
6 | # You're welcome to redistribute this software under the |
---|
7 | # terms of the GNU General Public Licence version 2.0 |
---|
8 | # or, at your option, any higher version. |
---|
9 | # |
---|
10 | # You can read the complete GNU GPL in the file COPYING |
---|
11 | # which should come along with this software, or visit |
---|
12 | # the Free Software Foundation's WEB site http://www.fsf.org |
---|
13 | # |
---|
14 | # $Id$ |
---|
15 | # |
---|
16 | # $Log$ |
---|
17 | # Revision 1.4 2003/02/17 22:05:50 jalet |
---|
18 | # Storage backend now supports admin and user passwords (untested) |
---|
19 | # |
---|
20 | # Revision 1.3 2003/02/06 14:49:04 jalet |
---|
21 | # edpykota should be ok now |
---|
22 | # |
---|
23 | # Revision 1.2 2003/02/05 22:28:38 jalet |
---|
24 | # More robust storage |
---|
25 | # |
---|
26 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
27 | # Initial import into CVS |
---|
28 | # |
---|
29 | # |
---|
30 | # |
---|
31 | |
---|
32 | import pg |
---|
33 | |
---|
34 | from pykota.storage import PyKotaStorageError |
---|
35 | from pykota.storages import sql |
---|
36 | |
---|
37 | class Storage(sql.SQLStorage) : |
---|
38 | def __init__(self, host, dbname, user, passwd) : |
---|
39 | """Opens the PostgreSQL database connection.""" |
---|
40 | self.closed = 1 |
---|
41 | try : |
---|
42 | self.database = pg.connect(host=host, dbname=dbname, user=user, passwd=passwd) |
---|
43 | self.closed = 0 |
---|
44 | except pg.error, msg : |
---|
45 | raise PyKotaStorageError, msg |
---|
46 | |
---|
47 | def __del__(self) : |
---|
48 | """Closes the database connection.""" |
---|
49 | if not self.closed : |
---|
50 | self.database.close() |
---|
51 | self.closed = 1 |
---|
52 | |
---|
53 | def doQuery(self, query) : |
---|
54 | """Does a query.""" |
---|
55 | try : |
---|
56 | return self.database.query(query) |
---|
57 | except pg.error, msg : |
---|
58 | raise PyKotaStorageError, msg |
---|
59 | |
---|
60 | def doQuote(self, field) : |
---|
61 | """Quotes a field for use as a string in SQL queries.""" |
---|
62 | if type(field) == type(0) : # TODO : do something safer |
---|
63 | typ = "decimal" |
---|
64 | else : |
---|
65 | typ = "text" |
---|
66 | return pg._quote(field, typ) |
---|
67 | |
---|
68 | def doParseResult(self, result) : |
---|
69 | """Returns the result as a list of Python mappings.""" |
---|
70 | if (result is not None) and (result.ntuples() > 0) : |
---|
71 | return result.dictresult() |
---|
72 | |
---|