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