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.3 2003/02/06 14:49:04 jalet |
---|
18 | # edpykota should be ok now |
---|
19 | # |
---|
20 | # Revision 1.2 2003/02/05 22:28:38 jalet |
---|
21 | # More robust storage |
---|
22 | # |
---|
23 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
24 | # Initial import into CVS |
---|
25 | # |
---|
26 | # |
---|
27 | # |
---|
28 | |
---|
29 | import pg |
---|
30 | |
---|
31 | from pykota.storage import PyKotaStorageError |
---|
32 | from pykota.storages import sql |
---|
33 | |
---|
34 | class Storage(sql.SQLStorage) : |
---|
35 | def __init__(self, host, dbname, user) : |
---|
36 | """Opens the PostgreSQL database connection.""" |
---|
37 | self.closed = 1 |
---|
38 | try : |
---|
39 | self.database = pg.connect(host=host, dbname=dbname, user=user) |
---|
40 | self.closed = 0 |
---|
41 | except pg.error, msg : |
---|
42 | raise PyKotaStorageError, msg |
---|
43 | |
---|
44 | def __del__(self) : |
---|
45 | """Closes the database connection.""" |
---|
46 | if not self.closed : |
---|
47 | self.database.close() |
---|
48 | self.closed = 1 |
---|
49 | |
---|
50 | def doQuery(self, query) : |
---|
51 | """Does a query.""" |
---|
52 | try : |
---|
53 | return self.database.query(query) |
---|
54 | except pg.error, msg : |
---|
55 | raise PyKotaStorageError, msg |
---|
56 | |
---|
57 | def doQuote(self, field) : |
---|
58 | """Quotes a field for use as a string in SQL queries.""" |
---|
59 | if type(field) == type(0) : # TODO : do something safer |
---|
60 | typ = "decimal" |
---|
61 | else : |
---|
62 | typ = "text" |
---|
63 | return pg._quote(field, typ) |
---|
64 | |
---|
65 | def doParseResult(self, result) : |
---|
66 | """Returns the result as a list of Python mappings.""" |
---|
67 | if (result is not None) and (result.ntuples() > 0) : |
---|
68 | return result.dictresult() |
---|
69 | |
---|