[1021] | 1 | # PyKota |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[1021] | 3 | # |
---|
| 4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
[2622] | 6 | # (c) 2003, 2004, 2005, 2006 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 | |
---|
[2599] | 25 | from types import StringType |
---|
| 26 | |
---|
[1274] | 27 | from pykota.storage import PyKotaStorageError,BaseStorage,StorageObject,StorageUser,StorageGroup,StoragePrinter,StorageJob,StorageLastJob,StorageUserPQuota,StorageGroupPQuota |
---|
[1327] | 28 | from pykota.storages.sql import SQLStorage |
---|
[1021] | 29 | |
---|
| 30 | try : |
---|
| 31 | import pg |
---|
| 32 | except ImportError : |
---|
| 33 | import sys |
---|
| 34 | # TODO : to translate or not to translate ? |
---|
| 35 | raise PyKotaStorageError, "This python version (%s) doesn't seem to have the PygreSQL module installed correctly." % sys.version.split()[0] |
---|
[2031] | 36 | else : |
---|
| 37 | try : |
---|
| 38 | PGError = pg.Error |
---|
| 39 | except AttributeError : |
---|
| 40 | PGError = pg.error |
---|
[1021] | 41 | |
---|
[1327] | 42 | class Storage(BaseStorage, SQLStorage) : |
---|
[1021] | 43 | def __init__(self, pykotatool, host, dbname, user, passwd) : |
---|
| 44 | """Opens the PostgreSQL database connection.""" |
---|
[1130] | 45 | BaseStorage.__init__(self, pykotatool) |
---|
[1021] | 46 | try : |
---|
| 47 | (host, port) = host.split(":") |
---|
| 48 | port = int(port) |
---|
| 49 | except ValueError : |
---|
| 50 | port = -1 # Use PostgreSQL's default tcp/ip port (5432). |
---|
| 51 | |
---|
[2418] | 52 | self.tool.logdebug("Trying to open database (host=%s, port=%s, dbname=%s, user=%s)..." % (host, port, dbname, user)) |
---|
[2210] | 53 | self.database = pg.connect(host=host, port=port, dbname=dbname, user=user, passwd=passwd) |
---|
| 54 | self.closed = 0 |
---|
| 55 | self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user)) |
---|
[1021] | 56 | |
---|
[1113] | 57 | def close(self) : |
---|
[1021] | 58 | """Closes the database connection.""" |
---|
| 59 | if not self.closed : |
---|
| 60 | self.database.close() |
---|
| 61 | self.closed = 1 |
---|
[1130] | 62 | self.tool.logdebug("Database closed.") |
---|
[1021] | 63 | |
---|
[1041] | 64 | def beginTransaction(self) : |
---|
| 65 | """Starts a transaction.""" |
---|
| 66 | self.database.query("BEGIN;") |
---|
[1130] | 67 | self.tool.logdebug("Transaction begins...") |
---|
[1041] | 68 | |
---|
| 69 | def commitTransaction(self) : |
---|
| 70 | """Commits a transaction.""" |
---|
| 71 | self.database.query("COMMIT;") |
---|
[1130] | 72 | self.tool.logdebug("Transaction committed.") |
---|
[1041] | 73 | |
---|
| 74 | def rollbackTransaction(self) : |
---|
| 75 | """Rollbacks a transaction.""" |
---|
| 76 | self.database.query("ROLLBACK;") |
---|
[1130] | 77 | self.tool.logdebug("Transaction aborted.") |
---|
[1041] | 78 | |
---|
[1717] | 79 | def doRawSearch(self, query) : |
---|
| 80 | """Does a raw search query.""" |
---|
[1021] | 81 | query = query.strip() |
---|
| 82 | if not query.endswith(';') : |
---|
| 83 | query += ';' |
---|
| 84 | try : |
---|
[1130] | 85 | self.tool.logdebug("QUERY : %s" % query) |
---|
[1021] | 86 | result = self.database.query(query) |
---|
[2031] | 87 | except PGError, msg : |
---|
[2033] | 88 | raise PyKotaStorageError, str(msg) |
---|
[1021] | 89 | else : |
---|
[1717] | 90 | return result |
---|
[1041] | 91 | |
---|
[1717] | 92 | def doSearch(self, query) : |
---|
| 93 | """Does a search query.""" |
---|
| 94 | result = self.doRawSearch(query) |
---|
| 95 | if (result is not None) and (result.ntuples() > 0) : |
---|
| 96 | return result.dictresult() |
---|
| 97 | |
---|
[1041] | 98 | def doModify(self, query) : |
---|
| 99 | """Does a (possibly multiple) modify query.""" |
---|
| 100 | query = query.strip() |
---|
| 101 | if not query.endswith(';') : |
---|
| 102 | query += ';' |
---|
| 103 | try : |
---|
[1130] | 104 | self.tool.logdebug("QUERY : %s" % query) |
---|
[1041] | 105 | result = self.database.query(query) |
---|
[2031] | 106 | except PGError, msg : |
---|
[2033] | 107 | raise PyKotaStorageError, str(msg) |
---|
[1068] | 108 | else : |
---|
| 109 | return result |
---|
[1041] | 110 | |
---|
[1021] | 111 | def doQuote(self, field) : |
---|
| 112 | """Quotes a field for use as a string in SQL queries.""" |
---|
| 113 | if type(field) == type(0.0) : |
---|
| 114 | typ = "decimal" |
---|
| 115 | elif type(field) == type(0) : |
---|
| 116 | typ = "int" |
---|
[1520] | 117 | elif type(field) == type(0L) : |
---|
| 118 | typ = "int" |
---|
[1021] | 119 | else : |
---|
| 120 | typ = "text" |
---|
| 121 | return pg._quote(field, typ) |
---|
[2593] | 122 | |
---|
| 123 | def prepareRawResult(self, result) : |
---|
| 124 | """Prepares a raw result by including the headers.""" |
---|
| 125 | if result.ntuples() > 0 : |
---|
| 126 | entries = [result.listfields()] |
---|
| 127 | entries.extend(result.getresult()) |
---|
| 128 | nbfields = len(entries[0]) |
---|
| 129 | for i in range(1, len(entries)) : |
---|
| 130 | fields = list(entries[i]) |
---|
| 131 | for j in range(nbfields) : |
---|
| 132 | field = fields[j] |
---|
| 133 | if type(field) == StringType : |
---|
| 134 | fields[j] = self.databaseToUserCharset(field) |
---|
| 135 | entries[i] = tuple(fields) |
---|
| 136 | return entries |
---|
| 137 | |
---|