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