[2591] | 1 | # PyKota |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | # |
---|
| 4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
[2622] | 6 | # (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com> |
---|
[2591] | 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 | |
---|
[2741] | 25 | import time |
---|
| 26 | |
---|
[2830] | 27 | from pykota.storage import PyKotaStorageError, BaseStorage |
---|
[2591] | 28 | from pykota.storages.sql import SQLStorage |
---|
| 29 | |
---|
| 30 | try : |
---|
| 31 | from pysqlite2 import dbapi2 as sqlite |
---|
| 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 PySQLite module installed correctly." % sys.version.split()[0] |
---|
| 36 | |
---|
| 37 | class Storage(BaseStorage, SQLStorage) : |
---|
| 38 | def __init__(self, pykotatool, host, dbname, user, passwd) : |
---|
| 39 | """Opens the SQLite database connection.""" |
---|
| 40 | BaseStorage.__init__(self, pykotatool) |
---|
| 41 | |
---|
| 42 | self.tool.logdebug("Trying to open database (dbname=%s)..." % dbname) |
---|
| 43 | self.database = sqlite.connect(dbname, isolation_level=None) |
---|
| 44 | self.cursor = self.database.cursor() |
---|
| 45 | self.closed = 0 |
---|
| 46 | self.tool.logdebug("Database opened (dbname=%s)" % dbname) |
---|
| 47 | |
---|
| 48 | def close(self) : |
---|
| 49 | """Closes the database connection.""" |
---|
| 50 | if not self.closed : |
---|
| 51 | self.cursor.close() |
---|
| 52 | self.database.close() |
---|
| 53 | self.closed = 1 |
---|
| 54 | self.tool.logdebug("Database closed.") |
---|
| 55 | |
---|
| 56 | def beginTransaction(self) : |
---|
| 57 | """Starts a transaction.""" |
---|
[2741] | 58 | self.before = time.time() |
---|
[2591] | 59 | self.cursor.execute("BEGIN;") |
---|
| 60 | self.tool.logdebug("Transaction begins...") |
---|
| 61 | |
---|
| 62 | def commitTransaction(self) : |
---|
| 63 | """Commits a transaction.""" |
---|
| 64 | self.cursor.execute("COMMIT;") |
---|
[2741] | 65 | after = time.time() |
---|
[2591] | 66 | self.tool.logdebug("Transaction committed.") |
---|
[2741] | 67 | self.tool.logdebug("Transaction duration : %.4f seconds" % (after - self.before)) |
---|
[2591] | 68 | |
---|
| 69 | def rollbackTransaction(self) : |
---|
| 70 | """Rollbacks a transaction.""" |
---|
| 71 | self.cursor.execute("ROLLBACK;") |
---|
[2741] | 72 | after = time.time() |
---|
[2591] | 73 | self.tool.logdebug("Transaction aborted.") |
---|
[2741] | 74 | self.tool.logdebug("Transaction duration : %.4f seconds" % (after - self.before)) |
---|
[2591] | 75 | |
---|
| 76 | def doRawSearch(self, query) : |
---|
| 77 | """Does a raw search query.""" |
---|
| 78 | query = query.strip() |
---|
| 79 | if not query.endswith(';') : |
---|
| 80 | query += ';' |
---|
| 81 | try : |
---|
[2741] | 82 | before = time.time() |
---|
[2591] | 83 | self.tool.logdebug("QUERY : %s" % query) |
---|
| 84 | self.cursor.execute(query) |
---|
| 85 | except self.database.Error, msg : |
---|
| 86 | raise PyKotaStorageError, str(msg) |
---|
| 87 | else : |
---|
[2741] | 88 | result = self.cursor.fetchall() |
---|
| 89 | after = time.time() |
---|
| 90 | self.tool.logdebug("Query Duration : %.4f seconds" % (after - before)) |
---|
| 91 | return result |
---|
[2591] | 92 | |
---|
| 93 | def doSearch(self, query) : |
---|
| 94 | """Does a search query.""" |
---|
| 95 | result = self.doRawSearch(query) |
---|
| 96 | if result : |
---|
| 97 | rows = [] |
---|
| 98 | fields = {} |
---|
| 99 | for i in range(len(self.cursor.description)) : |
---|
| 100 | fields[i] = self.cursor.description[i][0] |
---|
| 101 | for row in result : |
---|
| 102 | rowdict = {} |
---|
| 103 | for field in fields.keys() : |
---|
| 104 | value = row[field] |
---|
| 105 | try : |
---|
| 106 | value = value.encode("UTF-8") |
---|
| 107 | except : |
---|
| 108 | pass |
---|
| 109 | rowdict[fields[field]] = value |
---|
| 110 | rows.append(rowdict) |
---|
| 111 | return rows |
---|
| 112 | |
---|
| 113 | def doModify(self, query) : |
---|
| 114 | """Does a (possibly multiple) modify query.""" |
---|
| 115 | query = query.strip() |
---|
| 116 | if not query.endswith(';') : |
---|
| 117 | query += ';' |
---|
| 118 | try : |
---|
[2741] | 119 | before = time.time() |
---|
[2591] | 120 | self.tool.logdebug("QUERY : %s" % query) |
---|
| 121 | self.cursor.execute(query) |
---|
| 122 | except self.database.Error, msg : |
---|
[2773] | 123 | self.tool.logdebug("Query failed : %s" % repr(msg)) |
---|
[2591] | 124 | raise PyKotaStorageError, str(msg) |
---|
[2741] | 125 | else : |
---|
| 126 | after = time.time() |
---|
| 127 | self.tool.logdebug("Query Duration : %.4f seconds" % (after - before)) |
---|
[2591] | 128 | |
---|
| 129 | def doQuote(self, field) : |
---|
| 130 | """Quotes a field for use as a string in SQL queries.""" |
---|
| 131 | if type(field) == type(0.0) : |
---|
| 132 | return field |
---|
| 133 | elif type(field) == type(0) : |
---|
| 134 | return field |
---|
| 135 | elif type(field) == type(0L) : |
---|
| 136 | return field |
---|
| 137 | elif field is not None : |
---|
| 138 | return ("'%s'" % field.replace("'", "''")).decode("UTF-8") |
---|
| 139 | else : |
---|
| 140 | return "NULL" |
---|
[2593] | 141 | |
---|
| 142 | def prepareRawResult(self, result) : |
---|
| 143 | """Prepares a raw result by including the headers.""" |
---|
| 144 | if result : |
---|
| 145 | entries = [tuple([f[0] for f in self.cursor.description])] |
---|
| 146 | for entry in result : |
---|
| 147 | row = [] |
---|
| 148 | for value in entry : |
---|
| 149 | try : |
---|
| 150 | value = value.encode("UTF-8") |
---|
| 151 | except : |
---|
| 152 | pass |
---|
| 153 | row.append(value) |
---|
| 154 | entries.append(tuple(row)) |
---|
| 155 | return entries |
---|
| 156 | |
---|