[3260] | 1 | # -*- coding: UTF-8 -*- |
---|
[1021] | 2 | # |
---|
[3260] | 3 | # PyKota : Print Quotas for CUPS |
---|
[1021] | 4 | # |
---|
[3133] | 5 | # (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com> |
---|
[3260] | 6 | # This program is free software: you can redistribute it and/or modify |
---|
[1021] | 7 | # it under the terms of the GNU General Public License as published by |
---|
[3260] | 8 | # the Free Software Foundation, either version 3 of the License, or |
---|
[1021] | 9 | # (at your option) any later version. |
---|
[3260] | 10 | # |
---|
[1021] | 11 | # This program is distributed in the hope that it will be useful, |
---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | # GNU General Public License for more details. |
---|
| 15 | # |
---|
| 16 | # You should have received a copy of the GNU General Public License |
---|
[3260] | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[1021] | 18 | # |
---|
| 19 | # $Id$ |
---|
| 20 | # |
---|
[2033] | 21 | # |
---|
[1021] | 22 | |
---|
[3184] | 23 | """This module defines a class to access to a PostgreSQL database backend.""" |
---|
| 24 | |
---|
[2741] | 25 | import time |
---|
[2599] | 26 | from types import StringType |
---|
| 27 | |
---|
[2830] | 28 | from pykota.storage import PyKotaStorageError, BaseStorage |
---|
[1327] | 29 | from pykota.storages.sql import SQLStorage |
---|
[1021] | 30 | |
---|
| 31 | try : |
---|
| 32 | import pg |
---|
| 33 | except ImportError : |
---|
| 34 | import sys |
---|
| 35 | # TODO : to translate or not to translate ? |
---|
| 36 | raise PyKotaStorageError, "This python version (%s) doesn't seem to have the PygreSQL module installed correctly." % sys.version.split()[0] |
---|
[2031] | 37 | else : |
---|
| 38 | try : |
---|
| 39 | PGError = pg.Error |
---|
| 40 | except AttributeError : |
---|
| 41 | PGError = pg.error |
---|
[1021] | 42 | |
---|
[1327] | 43 | class Storage(BaseStorage, SQLStorage) : |
---|
[1021] | 44 | def __init__(self, pykotatool, host, dbname, user, passwd) : |
---|
| 45 | """Opens the PostgreSQL database connection.""" |
---|
[1130] | 46 | BaseStorage.__init__(self, pykotatool) |
---|
[1021] | 47 | try : |
---|
| 48 | (host, port) = host.split(":") |
---|
| 49 | port = int(port) |
---|
| 50 | except ValueError : |
---|
[2874] | 51 | port = 5432 # Use PostgreSQL's default tcp/ip port (5432). |
---|
[1021] | 52 | |
---|
[2418] | 53 | self.tool.logdebug("Trying to open database (host=%s, port=%s, dbname=%s, user=%s)..." % (host, port, dbname, user)) |
---|
[3182] | 54 | try : |
---|
| 55 | self.database = pg.connect(host=host, port=port, dbname=dbname, user=user, passwd=passwd) |
---|
| 56 | except PGError, msg : |
---|
| 57 | 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() |
---|
| 58 | raise PGError, msg |
---|
[2210] | 59 | self.closed = 0 |
---|
[2960] | 60 | try : |
---|
| 61 | self.database.query("SET CLIENT_ENCODING TO 'UTF-8';") |
---|
| 62 | except PGError, msg : |
---|
[2972] | 63 | self.tool.logdebug("Impossible to set database client encoding to UTF-8 : %s" % msg) |
---|
[2210] | 64 | self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user)) |
---|
[1021] | 65 | |
---|
[1113] | 66 | def close(self) : |
---|
[1021] | 67 | """Closes the database connection.""" |
---|
| 68 | if not self.closed : |
---|
| 69 | self.database.close() |
---|
| 70 | self.closed = 1 |
---|
[1130] | 71 | self.tool.logdebug("Database closed.") |
---|
[1021] | 72 | |
---|
[1041] | 73 | def beginTransaction(self) : |
---|
| 74 | """Starts a transaction.""" |
---|
[2741] | 75 | self.before = time.time() |
---|
[1041] | 76 | self.database.query("BEGIN;") |
---|
[1130] | 77 | self.tool.logdebug("Transaction begins...") |
---|
[1041] | 78 | |
---|
| 79 | def commitTransaction(self) : |
---|
| 80 | """Commits a transaction.""" |
---|
| 81 | self.database.query("COMMIT;") |
---|
[2741] | 82 | after = time.time() |
---|
[1130] | 83 | self.tool.logdebug("Transaction committed.") |
---|
[3182] | 84 | #self.tool.logdebug("Transaction duration : %.4f seconds" % (after - self.before)) |
---|
[1041] | 85 | |
---|
| 86 | def rollbackTransaction(self) : |
---|
| 87 | """Rollbacks a transaction.""" |
---|
| 88 | self.database.query("ROLLBACK;") |
---|
[2741] | 89 | after = time.time() |
---|
[1130] | 90 | self.tool.logdebug("Transaction aborted.") |
---|
[3182] | 91 | #self.tool.logdebug("Transaction duration : %.4f seconds" % (after - self.before)) |
---|
[1041] | 92 | |
---|
[1717] | 93 | def doRawSearch(self, query) : |
---|
| 94 | """Does a raw search query.""" |
---|
[1021] | 95 | query = query.strip() |
---|
| 96 | if not query.endswith(';') : |
---|
| 97 | query += ';' |
---|
| 98 | try : |
---|
[2741] | 99 | before = time.time() |
---|
[1130] | 100 | self.tool.logdebug("QUERY : %s" % query) |
---|
[1021] | 101 | result = self.database.query(query) |
---|
[2031] | 102 | except PGError, msg : |
---|
[2033] | 103 | raise PyKotaStorageError, str(msg) |
---|
[1021] | 104 | else : |
---|
[2741] | 105 | after = time.time() |
---|
[3182] | 106 | #self.tool.logdebug("Query Duration : %.4f seconds" % (after - before)) |
---|
[1717] | 107 | return result |
---|
[1041] | 108 | |
---|
[1717] | 109 | def doSearch(self, query) : |
---|
| 110 | """Does a search query.""" |
---|
| 111 | result = self.doRawSearch(query) |
---|
| 112 | if (result is not None) and (result.ntuples() > 0) : |
---|
| 113 | return result.dictresult() |
---|
| 114 | |
---|
[1041] | 115 | def doModify(self, query) : |
---|
| 116 | """Does a (possibly multiple) modify query.""" |
---|
| 117 | query = query.strip() |
---|
| 118 | if not query.endswith(';') : |
---|
| 119 | query += ';' |
---|
| 120 | try : |
---|
[2741] | 121 | before = time.time() |
---|
[1130] | 122 | self.tool.logdebug("QUERY : %s" % query) |
---|
[1041] | 123 | result = self.database.query(query) |
---|
[2031] | 124 | except PGError, msg : |
---|
[2773] | 125 | self.tool.logdebug("Query failed : %s" % repr(msg)) |
---|
[2033] | 126 | raise PyKotaStorageError, str(msg) |
---|
[1068] | 127 | else : |
---|
[2741] | 128 | after = time.time() |
---|
[3182] | 129 | #self.tool.logdebug("Query Duration : %.4f seconds" % (after - before)) |
---|
[1068] | 130 | return result |
---|
[1041] | 131 | |
---|
[1021] | 132 | def doQuote(self, field) : |
---|
| 133 | """Quotes a field for use as a string in SQL queries.""" |
---|
| 134 | if type(field) == type(0.0) : |
---|
| 135 | typ = "decimal" |
---|
| 136 | elif type(field) == type(0) : |
---|
| 137 | typ = "int" |
---|
[1520] | 138 | elif type(field) == type(0L) : |
---|
| 139 | typ = "int" |
---|
[1021] | 140 | else : |
---|
| 141 | typ = "text" |
---|
| 142 | return pg._quote(field, typ) |
---|
[2593] | 143 | |
---|
| 144 | def prepareRawResult(self, result) : |
---|
| 145 | """Prepares a raw result by including the headers.""" |
---|
| 146 | if result.ntuples() > 0 : |
---|
| 147 | entries = [result.listfields()] |
---|
| 148 | entries.extend(result.getresult()) |
---|
| 149 | nbfields = len(entries[0]) |
---|
| 150 | for i in range(1, len(entries)) : |
---|
| 151 | fields = list(entries[i]) |
---|
| 152 | for j in range(nbfields) : |
---|
| 153 | field = fields[j] |
---|
| 154 | if type(field) == StringType : |
---|
| 155 | fields[j] = self.databaseToUserCharset(field) |
---|
| 156 | entries[i] = tuple(fields) |
---|
| 157 | return entries |
---|
| 158 | |
---|