[1021] | 1 | # PyKota |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[1021] | 3 | # |
---|
| 4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
[1257] | 6 | # (c) 2003-2004 Jerome Alet <alet@librelogiciel.com> |
---|
[1021] | 7 | # This program is free software; you can redistribute it and/or modify |
---|
| 8 | # it under the terms of the GNU General Public License as published by |
---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 10 | # (at your option) any later version. |
---|
| 11 | # |
---|
| 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
| 18 | # along with this program; if not, write to the Free Software |
---|
[2302] | 19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[1021] | 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
[2033] | 23 | # |
---|
[1021] | 24 | |
---|
[1274] | 25 | from pykota.storage import PyKotaStorageError,BaseStorage,StorageObject,StorageUser,StorageGroup,StoragePrinter,StorageJob,StorageLastJob,StorageUserPQuota,StorageGroupPQuota |
---|
[1327] | 26 | from pykota.storages.sql import SQLStorage |
---|
[1021] | 27 | |
---|
| 28 | try : |
---|
| 29 | import pg |
---|
| 30 | except ImportError : |
---|
| 31 | import sys |
---|
| 32 | # TODO : to translate or not to translate ? |
---|
| 33 | raise PyKotaStorageError, "This python version (%s) doesn't seem to have the PygreSQL module installed correctly." % sys.version.split()[0] |
---|
[2031] | 34 | else : |
---|
| 35 | try : |
---|
| 36 | PGError = pg.Error |
---|
| 37 | except AttributeError : |
---|
| 38 | PGError = pg.error |
---|
[1021] | 39 | |
---|
[1327] | 40 | class Storage(BaseStorage, SQLStorage) : |
---|
[1021] | 41 | def __init__(self, pykotatool, host, dbname, user, passwd) : |
---|
| 42 | """Opens the PostgreSQL database connection.""" |
---|
[1130] | 43 | BaseStorage.__init__(self, pykotatool) |
---|
[1021] | 44 | try : |
---|
| 45 | (host, port) = host.split(":") |
---|
| 46 | port = int(port) |
---|
| 47 | except ValueError : |
---|
| 48 | port = -1 # Use PostgreSQL's default tcp/ip port (5432). |
---|
| 49 | |
---|
[2210] | 50 | self.database = pg.connect(host=host, port=port, dbname=dbname, user=user, passwd=passwd) |
---|
| 51 | self.closed = 0 |
---|
| 52 | self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user)) |
---|
[1021] | 53 | |
---|
[1113] | 54 | def close(self) : |
---|
[1021] | 55 | """Closes the database connection.""" |
---|
| 56 | if not self.closed : |
---|
| 57 | self.database.close() |
---|
| 58 | self.closed = 1 |
---|
[1130] | 59 | self.tool.logdebug("Database closed.") |
---|
[1021] | 60 | |
---|
[1041] | 61 | def beginTransaction(self) : |
---|
| 62 | """Starts a transaction.""" |
---|
| 63 | self.database.query("BEGIN;") |
---|
[1130] | 64 | self.tool.logdebug("Transaction begins...") |
---|
[1041] | 65 | |
---|
| 66 | def commitTransaction(self) : |
---|
| 67 | """Commits a transaction.""" |
---|
| 68 | self.database.query("COMMIT;") |
---|
[1130] | 69 | self.tool.logdebug("Transaction committed.") |
---|
[1041] | 70 | |
---|
| 71 | def rollbackTransaction(self) : |
---|
| 72 | """Rollbacks a transaction.""" |
---|
| 73 | self.database.query("ROLLBACK;") |
---|
[1130] | 74 | self.tool.logdebug("Transaction aborted.") |
---|
[1041] | 75 | |
---|
[1717] | 76 | def doRawSearch(self, query) : |
---|
| 77 | """Does a raw search query.""" |
---|
[1021] | 78 | query = query.strip() |
---|
| 79 | if not query.endswith(';') : |
---|
| 80 | query += ';' |
---|
| 81 | try : |
---|
[1130] | 82 | self.tool.logdebug("QUERY : %s" % query) |
---|
[1021] | 83 | result = self.database.query(query) |
---|
[2031] | 84 | except PGError, msg : |
---|
[2033] | 85 | raise PyKotaStorageError, str(msg) |
---|
[1021] | 86 | else : |
---|
[1717] | 87 | return result |
---|
[1041] | 88 | |
---|
[1717] | 89 | def doSearch(self, query) : |
---|
| 90 | """Does a search query.""" |
---|
| 91 | result = self.doRawSearch(query) |
---|
| 92 | if (result is not None) and (result.ntuples() > 0) : |
---|
| 93 | return result.dictresult() |
---|
| 94 | |
---|
[1041] | 95 | def doModify(self, query) : |
---|
| 96 | """Does a (possibly multiple) modify query.""" |
---|
| 97 | query = query.strip() |
---|
| 98 | if not query.endswith(';') : |
---|
| 99 | query += ';' |
---|
| 100 | try : |
---|
[1130] | 101 | self.tool.logdebug("QUERY : %s" % query) |
---|
[1041] | 102 | result = self.database.query(query) |
---|
[2031] | 103 | except PGError, msg : |
---|
[2033] | 104 | raise PyKotaStorageError, str(msg) |
---|
[1068] | 105 | else : |
---|
| 106 | return result |
---|
[1041] | 107 | |
---|
[1021] | 108 | def doQuote(self, field) : |
---|
| 109 | """Quotes a field for use as a string in SQL queries.""" |
---|
| 110 | if type(field) == type(0.0) : |
---|
| 111 | typ = "decimal" |
---|
| 112 | elif type(field) == type(0) : |
---|
| 113 | typ = "int" |
---|
[1520] | 114 | elif type(field) == type(0L) : |
---|
| 115 | typ = "int" |
---|
[1021] | 116 | else : |
---|
| 117 | typ = "text" |
---|
| 118 | return pg._quote(field, typ) |
---|