[1021] | 1 | # PyKota |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[1021] | 3 | # |
---|
| 4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
[3133] | 6 | # (c) 2003, 2004, 2005, 2006, 2007 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. |
---|
[3500] | 16 | # |
---|
[1021] | 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 | |
---|
[3184] | 25 | """This module defines a class to access to a PostgreSQL database backend.""" |
---|
| 26 | |
---|
[2741] | 27 | import time |
---|
[2599] | 28 | from types import StringType |
---|
| 29 | |
---|
[2830] | 30 | from pykota.storage import PyKotaStorageError, BaseStorage |
---|
[1327] | 31 | from pykota.storages.sql import SQLStorage |
---|
[1021] | 32 | |
---|
| 33 | try : |
---|
| 34 | import pg |
---|
[3500] | 35 | except ImportError : |
---|
[1021] | 36 | import sys |
---|
| 37 | # TODO : to translate or not to translate ? |
---|
| 38 | raise PyKotaStorageError, "This python version (%s) doesn't seem to have the PygreSQL module installed correctly." % sys.version.split()[0] |
---|
[3500] | 39 | else : |
---|
[2031] | 40 | try : |
---|
| 41 | PGError = pg.Error |
---|
[3500] | 42 | except AttributeError : |
---|
[2031] | 43 | PGError = pg.error |
---|
[1021] | 44 | |
---|
[1327] | 45 | class Storage(BaseStorage, SQLStorage) : |
---|
[1021] | 46 | def __init__(self, pykotatool, host, dbname, user, passwd) : |
---|
| 47 | """Opens the PostgreSQL database connection.""" |
---|
[1130] | 48 | BaseStorage.__init__(self, pykotatool) |
---|
[1021] | 49 | try : |
---|
| 50 | (host, port) = host.split(":") |
---|
| 51 | port = int(port) |
---|
[3500] | 52 | except ValueError : |
---|
[2874] | 53 | port = 5432 # Use PostgreSQL's default tcp/ip port (5432). |
---|
[3500] | 54 | |
---|
[2418] | 55 | self.tool.logdebug("Trying to open database (host=%s, port=%s, dbname=%s, user=%s)..." % (host, port, dbname, user)) |
---|
[3182] | 56 | try : |
---|
[3500] | 57 | self.database = pg.DB(host=host, port=port, dbname=dbname, user=user, passwd=passwd) |
---|
| 58 | except PGError, msg : |
---|
[3182] | 59 | msg = "%(msg)s --- the most probable cause of your problem is that PostgreSQL is down, or doesn't accept incoming connections because you didn't configure it as explained in PyKota's documentation." % locals() |
---|
| 60 | raise PGError, msg |
---|
[2210] | 61 | self.closed = 0 |
---|
[2960] | 62 | try : |
---|
[3500] | 63 | self.quote = self.database._quote |
---|
| 64 | except AttributeError : # pg <v4.x |
---|
| 65 | self.quote = pg._quote |
---|
| 66 | try : |
---|
[2960] | 67 | self.database.query("SET CLIENT_ENCODING TO 'UTF-8';") |
---|
[3500] | 68 | except PGError, msg : |
---|
[2972] | 69 | self.tool.logdebug("Impossible to set database client encoding to UTF-8 : %s" % msg) |
---|
[2210] | 70 | self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user)) |
---|
[3500] | 71 | |
---|
| 72 | def close(self) : |
---|
[1021] | 73 | """Closes the database connection.""" |
---|
| 74 | if not self.closed : |
---|
| 75 | self.database.close() |
---|
| 76 | self.closed = 1 |
---|
[1130] | 77 | self.tool.logdebug("Database closed.") |
---|
[3500] | 78 | |
---|
| 79 | def beginTransaction(self) : |
---|
[1041] | 80 | """Starts a transaction.""" |
---|
[2741] | 81 | self.before = time.time() |
---|
[1041] | 82 | self.database.query("BEGIN;") |
---|
[1130] | 83 | self.tool.logdebug("Transaction begins...") |
---|
[3500] | 84 | |
---|
| 85 | def commitTransaction(self) : |
---|
[1041] | 86 | """Commits a transaction.""" |
---|
| 87 | self.database.query("COMMIT;") |
---|
[2741] | 88 | after = time.time() |
---|
[1130] | 89 | self.tool.logdebug("Transaction committed.") |
---|
[3182] | 90 | #self.tool.logdebug("Transaction duration : %.4f seconds" % (after - self.before)) |
---|
[3500] | 91 | |
---|
| 92 | def rollbackTransaction(self) : |
---|
[1041] | 93 | """Rollbacks a transaction.""" |
---|
| 94 | self.database.query("ROLLBACK;") |
---|
[2741] | 95 | after = time.time() |
---|
[1130] | 96 | self.tool.logdebug("Transaction aborted.") |
---|
[3182] | 97 | #self.tool.logdebug("Transaction duration : %.4f seconds" % (after - self.before)) |
---|
[3500] | 98 | |
---|
[1717] | 99 | def doRawSearch(self, query) : |
---|
| 100 | """Does a raw search query.""" |
---|
[3500] | 101 | query = query.strip() |
---|
| 102 | if not query.endswith(';') : |
---|
[1021] | 103 | query += ';' |
---|
| 104 | try : |
---|
[2741] | 105 | before = time.time() |
---|
[1130] | 106 | self.tool.logdebug("QUERY : %s" % query) |
---|
[1021] | 107 | result = self.database.query(query) |
---|
[3500] | 108 | except PGError, msg : |
---|
[2033] | 109 | raise PyKotaStorageError, str(msg) |
---|
[3500] | 110 | else : |
---|
[2741] | 111 | after = time.time() |
---|
[3182] | 112 | #self.tool.logdebug("Query Duration : %.4f seconds" % (after - before)) |
---|
[1717] | 113 | return result |
---|
[3500] | 114 | |
---|
| 115 | def doSearch(self, query) : |
---|
[1717] | 116 | """Does a search query.""" |
---|
| 117 | result = self.doRawSearch(query) |
---|
[3500] | 118 | if (result is not None) and (result.ntuples() > 0) : |
---|
[1717] | 119 | return result.dictresult() |
---|
[3500] | 120 | |
---|
[1041] | 121 | def doModify(self, query) : |
---|
| 122 | """Does a (possibly multiple) modify query.""" |
---|
[3500] | 123 | query = query.strip() |
---|
| 124 | if not query.endswith(';') : |
---|
[1041] | 125 | query += ';' |
---|
| 126 | try : |
---|
[2741] | 127 | before = time.time() |
---|
[1130] | 128 | self.tool.logdebug("QUERY : %s" % query) |
---|
[1041] | 129 | result = self.database.query(query) |
---|
[3500] | 130 | except PGError, msg : |
---|
[2773] | 131 | self.tool.logdebug("Query failed : %s" % repr(msg)) |
---|
[2033] | 132 | raise PyKotaStorageError, str(msg) |
---|
[3500] | 133 | else : |
---|
[2741] | 134 | after = time.time() |
---|
[3182] | 135 | #self.tool.logdebug("Query Duration : %.4f seconds" % (after - before)) |
---|
[1068] | 136 | return result |
---|
[3500] | 137 | |
---|
[1021] | 138 | def doQuote(self, field) : |
---|
| 139 | """Quotes a field for use as a string in SQL queries.""" |
---|
[3500] | 140 | if type(field) == type(0.0) : |
---|
[1021] | 141 | typ = "decimal" |
---|
[3500] | 142 | elif type(field) == type(0) : |
---|
[1021] | 143 | typ = "int" |
---|
[3500] | 144 | elif type(field) == type(0L) : |
---|
[1520] | 145 | typ = "int" |
---|
[3500] | 146 | else : |
---|
[1021] | 147 | typ = "text" |
---|
[3500] | 148 | return self.quote(field, typ) |
---|
| 149 | |
---|
[2593] | 150 | def prepareRawResult(self, result) : |
---|
| 151 | """Prepares a raw result by including the headers.""" |
---|
| 152 | if result.ntuples() > 0 : |
---|
| 153 | entries = [result.listfields()] |
---|
| 154 | entries.extend(result.getresult()) |
---|
| 155 | nbfields = len(entries[0]) |
---|
| 156 | for i in range(1, len(entries)) : |
---|
| 157 | fields = list(entries[i]) |
---|
| 158 | for j in range(nbfields) : |
---|
| 159 | field = fields[j] |
---|
| 160 | if type(field) == StringType : |
---|
[3500] | 161 | fields[j] = self.databaseToUserCharset(field) |
---|
| 162 | entries[i] = tuple(fields) |
---|
[2593] | 163 | return entries |
---|