1 | # PyKota |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | # |
---|
4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
5 | # |
---|
6 | # (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com> |
---|
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | # |
---|
21 | # $Id$ |
---|
22 | # |
---|
23 | # |
---|
24 | |
---|
25 | import time |
---|
26 | from types import StringType |
---|
27 | |
---|
28 | from pykota.storage import PyKotaStorageError, BaseStorage |
---|
29 | from pykota.storages.sql import SQLStorage |
---|
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] |
---|
37 | else : |
---|
38 | try : |
---|
39 | PGError = pg.Error |
---|
40 | except AttributeError : |
---|
41 | PGError = pg.error |
---|
42 | |
---|
43 | class Storage(BaseStorage, SQLStorage) : |
---|
44 | def __init__(self, pykotatool, host, dbname, user, passwd) : |
---|
45 | """Opens the PostgreSQL database connection.""" |
---|
46 | BaseStorage.__init__(self, pykotatool) |
---|
47 | try : |
---|
48 | (host, port) = host.split(":") |
---|
49 | port = int(port) |
---|
50 | except ValueError : |
---|
51 | port = 5432 # Use PostgreSQL's default tcp/ip port (5432). |
---|
52 | |
---|
53 | self.tool.logdebug("Trying to open database (host=%s, port=%s, dbname=%s, user=%s)..." % (host, port, dbname, user)) |
---|
54 | self.database = pg.connect(host=host, port=port, dbname=dbname, user=user, passwd=passwd) |
---|
55 | self.closed = 0 |
---|
56 | try : |
---|
57 | self.database.query("SET CLIENT_ENCODING TO 'UTF-8';") |
---|
58 | except PGError, msg : |
---|
59 | self.tool.logdebug("Impossible to set database client encoding to UTF-8 : %s" % msg) |
---|
60 | self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user)) |
---|
61 | |
---|
62 | def close(self) : |
---|
63 | """Closes the database connection.""" |
---|
64 | if not self.closed : |
---|
65 | self.database.close() |
---|
66 | self.closed = 1 |
---|
67 | self.tool.logdebug("Database closed.") |
---|
68 | |
---|
69 | def beginTransaction(self) : |
---|
70 | """Starts a transaction.""" |
---|
71 | self.before = time.time() |
---|
72 | self.database.query("BEGIN;") |
---|
73 | self.tool.logdebug("Transaction begins...") |
---|
74 | |
---|
75 | def commitTransaction(self) : |
---|
76 | """Commits a transaction.""" |
---|
77 | self.database.query("COMMIT;") |
---|
78 | after = time.time() |
---|
79 | self.tool.logdebug("Transaction committed.") |
---|
80 | self.tool.logdebug("Transaction duration : %.4f seconds" % (after - self.before)) |
---|
81 | |
---|
82 | def rollbackTransaction(self) : |
---|
83 | """Rollbacks a transaction.""" |
---|
84 | self.database.query("ROLLBACK;") |
---|
85 | after = time.time() |
---|
86 | self.tool.logdebug("Transaction aborted.") |
---|
87 | self.tool.logdebug("Transaction duration : %.4f seconds" % (after - self.before)) |
---|
88 | |
---|
89 | def doRawSearch(self, query) : |
---|
90 | """Does a raw search query.""" |
---|
91 | query = query.strip() |
---|
92 | if not query.endswith(';') : |
---|
93 | query += ';' |
---|
94 | try : |
---|
95 | before = time.time() |
---|
96 | self.tool.logdebug("QUERY : %s" % query) |
---|
97 | result = self.database.query(query) |
---|
98 | except PGError, msg : |
---|
99 | raise PyKotaStorageError, str(msg) |
---|
100 | else : |
---|
101 | after = time.time() |
---|
102 | self.tool.logdebug("Query Duration : %.4f seconds" % (after - before)) |
---|
103 | return result |
---|
104 | |
---|
105 | def doSearch(self, query) : |
---|
106 | """Does a search query.""" |
---|
107 | result = self.doRawSearch(query) |
---|
108 | if (result is not None) and (result.ntuples() > 0) : |
---|
109 | return result.dictresult() |
---|
110 | |
---|
111 | def doModify(self, query) : |
---|
112 | """Does a (possibly multiple) modify query.""" |
---|
113 | query = query.strip() |
---|
114 | if not query.endswith(';') : |
---|
115 | query += ';' |
---|
116 | try : |
---|
117 | before = time.time() |
---|
118 | self.tool.logdebug("QUERY : %s" % query) |
---|
119 | result = self.database.query(query) |
---|
120 | except PGError, msg : |
---|
121 | self.tool.logdebug("Query failed : %s" % repr(msg)) |
---|
122 | raise PyKotaStorageError, str(msg) |
---|
123 | else : |
---|
124 | after = time.time() |
---|
125 | self.tool.logdebug("Query Duration : %.4f seconds" % (after - before)) |
---|
126 | return result |
---|
127 | |
---|
128 | def doQuote(self, field) : |
---|
129 | """Quotes a field for use as a string in SQL queries.""" |
---|
130 | if type(field) == type(0.0) : |
---|
131 | typ = "decimal" |
---|
132 | elif type(field) == type(0) : |
---|
133 | typ = "int" |
---|
134 | elif type(field) == type(0L) : |
---|
135 | typ = "int" |
---|
136 | else : |
---|
137 | typ = "text" |
---|
138 | return pg._quote(field, typ) |
---|
139 | |
---|
140 | def prepareRawResult(self, result) : |
---|
141 | """Prepares a raw result by including the headers.""" |
---|
142 | if result.ntuples() > 0 : |
---|
143 | entries = [result.listfields()] |
---|
144 | entries.extend(result.getresult()) |
---|
145 | nbfields = len(entries[0]) |
---|
146 | for i in range(1, len(entries)) : |
---|
147 | fields = list(entries[i]) |
---|
148 | for j in range(nbfields) : |
---|
149 | field = fields[j] |
---|
150 | if type(field) == StringType : |
---|
151 | fields[j] = self.databaseToUserCharset(field) |
---|
152 | entries[i] = tuple(fields) |
---|
153 | return entries |
---|
154 | |
---|