[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 |
---|
| 19 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 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 | |
---|
| 50 | try : |
---|
| 51 | self.database = pg.connect(host=host, port=port, dbname=dbname, user=user, passwd=passwd) |
---|
[2031] | 52 | except PGError, msg : |
---|
[2033] | 53 | raise PyKotaStorageError, str(msg) |
---|
[1021] | 54 | else : |
---|
[1024] | 55 | self.closed = 0 |
---|
[1130] | 56 | self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user)) |
---|
[1021] | 57 | |
---|
[1113] | 58 | def close(self) : |
---|
[1021] | 59 | """Closes the database connection.""" |
---|
| 60 | if not self.closed : |
---|
| 61 | self.database.close() |
---|
| 62 | self.closed = 1 |
---|
[1130] | 63 | self.tool.logdebug("Database closed.") |
---|
[1021] | 64 | |
---|
[1041] | 65 | def beginTransaction(self) : |
---|
| 66 | """Starts a transaction.""" |
---|
| 67 | self.database.query("BEGIN;") |
---|
[1130] | 68 | self.tool.logdebug("Transaction begins...") |
---|
[1041] | 69 | |
---|
| 70 | def commitTransaction(self) : |
---|
| 71 | """Commits a transaction.""" |
---|
| 72 | self.database.query("COMMIT;") |
---|
[1130] | 73 | self.tool.logdebug("Transaction committed.") |
---|
[1041] | 74 | |
---|
| 75 | def rollbackTransaction(self) : |
---|
| 76 | """Rollbacks a transaction.""" |
---|
| 77 | self.database.query("ROLLBACK;") |
---|
[1130] | 78 | self.tool.logdebug("Transaction aborted.") |
---|
[1041] | 79 | |
---|
[1717] | 80 | def doRawSearch(self, query) : |
---|
| 81 | """Does a raw search query.""" |
---|
[1021] | 82 | query = query.strip() |
---|
| 83 | if not query.endswith(';') : |
---|
| 84 | query += ';' |
---|
| 85 | try : |
---|
[1130] | 86 | self.tool.logdebug("QUERY : %s" % query) |
---|
[1021] | 87 | result = self.database.query(query) |
---|
[2031] | 88 | except PGError, msg : |
---|
[2033] | 89 | raise PyKotaStorageError, str(msg) |
---|
[1021] | 90 | else : |
---|
[1717] | 91 | return result |
---|
[1041] | 92 | |
---|
[1717] | 93 | def doSearch(self, query) : |
---|
| 94 | """Does a search query.""" |
---|
| 95 | result = self.doRawSearch(query) |
---|
| 96 | if (result is not None) and (result.ntuples() > 0) : |
---|
| 97 | return result.dictresult() |
---|
| 98 | |
---|
[1041] | 99 | def doModify(self, query) : |
---|
| 100 | """Does a (possibly multiple) modify query.""" |
---|
| 101 | query = query.strip() |
---|
| 102 | if not query.endswith(';') : |
---|
| 103 | query += ';' |
---|
| 104 | try : |
---|
[1130] | 105 | self.tool.logdebug("QUERY : %s" % query) |
---|
[1041] | 106 | result = self.database.query(query) |
---|
[2031] | 107 | except PGError, msg : |
---|
[2033] | 108 | raise PyKotaStorageError, str(msg) |
---|
[1068] | 109 | else : |
---|
| 110 | return result |
---|
[1041] | 111 | |
---|
[1021] | 112 | def doQuote(self, field) : |
---|
| 113 | """Quotes a field for use as a string in SQL queries.""" |
---|
| 114 | if type(field) == type(0.0) : |
---|
| 115 | typ = "decimal" |
---|
| 116 | elif type(field) == type(0) : |
---|
| 117 | typ = "int" |
---|
[1520] | 118 | elif type(field) == type(0L) : |
---|
| 119 | typ = "int" |
---|
[1021] | 120 | else : |
---|
| 121 | typ = "text" |
---|
| 122 | return pg._quote(field, typ) |
---|