[2639] | 1 | # PyKota |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | # |
---|
| 4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
[2644] | 6 | # (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com> |
---|
| 7 | # (c) 2005, 2006 Matt Hyclak <hyclak@math.ohiou.edu> |
---|
[2639] | 8 | # This program is free software; you can redistribute it and/or modify |
---|
| 9 | # it under the terms of the GNU General Public License as published by |
---|
| 10 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 11 | # (at your option) any later version. |
---|
| 12 | # |
---|
| 13 | # This program is distributed in the hope that it will be useful, |
---|
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | # GNU General Public License for more details. |
---|
| 17 | # |
---|
| 18 | # You should have received a copy of the GNU General Public License |
---|
| 19 | # along with this program; if not, write to the Free Software |
---|
| 20 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | # |
---|
[2645] | 22 | # $Id$ |
---|
[2639] | 23 | # |
---|
| 24 | # |
---|
| 25 | |
---|
[2741] | 26 | import time |
---|
| 27 | |
---|
[2830] | 28 | from pykota.storage import PyKotaStorageError, BaseStorage |
---|
[2639] | 29 | from pykota.storages.sql import SQLStorage |
---|
| 30 | |
---|
| 31 | try : |
---|
| 32 | import MySQLdb |
---|
| 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 MySQL module installed correctly." % sys.version.split()[0] |
---|
| 37 | |
---|
| 38 | class Storage(BaseStorage, SQLStorage) : |
---|
| 39 | def __init__(self, pykotatool, host, dbname, user, passwd) : |
---|
| 40 | """Opens the MySQL database connection.""" |
---|
| 41 | BaseStorage.__init__(self, pykotatool) |
---|
| 42 | try : |
---|
| 43 | (host, port) = host.split(":") |
---|
| 44 | port = int(port) |
---|
| 45 | except ValueError : |
---|
[2874] | 46 | port = 3306 # Use the default MySQL port |
---|
[2639] | 47 | |
---|
| 48 | self.tool.logdebug("Trying to open database (host=%s, port=%s, dbname=%s, user=%s)..." % (host, port, dbname, user)) |
---|
| 49 | self.database = MySQLdb.connect(host=host, port=port, db=dbname, user=user, passwd=passwd) |
---|
[3009] | 50 | try : |
---|
| 51 | self.database.autocommit(1) |
---|
| 52 | except AttributeError : |
---|
[3010] | 53 | raise PyKotaStorageError, _("Your version of python-mysqldb is too old. Please install a newer release.") |
---|
[2644] | 54 | self.cursor = self.database.cursor() |
---|
[2954] | 55 | self.cursor.execute("SET NAMES 'utf8';") |
---|
[2821] | 56 | self.cursor.execute("SET TRANSACTION ISOLATION LEVEL READ COMMITTED;") # Same as PostgreSQL and Oracle's default |
---|
[2639] | 57 | self.closed = 0 |
---|
| 58 | self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user)) |
---|
| 59 | |
---|
| 60 | def close(self) : |
---|
| 61 | """Closes the database connection.""" |
---|
| 62 | if not self.closed : |
---|
[2644] | 63 | self.cursor.close() |
---|
[2639] | 64 | self.database.close() |
---|
| 65 | self.closed = 1 |
---|
| 66 | self.tool.logdebug("Database closed.") |
---|
| 67 | |
---|
| 68 | def beginTransaction(self) : |
---|
| 69 | """Starts a transaction.""" |
---|
[2741] | 70 | self.before = time.time() |
---|
[2646] | 71 | self.cursor.execute("BEGIN;") |
---|
[2639] | 72 | self.tool.logdebug("Transaction begins...") |
---|
| 73 | |
---|
| 74 | def commitTransaction(self) : |
---|
| 75 | """Commits a transaction.""" |
---|
| 76 | self.database.commit() |
---|
[2741] | 77 | after = time.time() |
---|
[2639] | 78 | self.tool.logdebug("Transaction committed.") |
---|
[2741] | 79 | self.tool.logdebug("Transaction duration : %.4f seconds" % (after - self.before)) |
---|
[2639] | 80 | |
---|
| 81 | def rollbackTransaction(self) : |
---|
| 82 | """Rollbacks a transaction.""" |
---|
| 83 | self.database.rollback() |
---|
[2741] | 84 | after = time.time() |
---|
[2639] | 85 | self.tool.logdebug("Transaction aborted.") |
---|
[2741] | 86 | self.tool.logdebug("Transaction duration : %.4f seconds" % (after - self.before)) |
---|
[2639] | 87 | |
---|
| 88 | def doRawSearch(self, query) : |
---|
| 89 | """Does a raw search query.""" |
---|
| 90 | query = query.strip() |
---|
| 91 | if not query.endswith(';') : |
---|
| 92 | query += ';' |
---|
| 93 | try : |
---|
[2741] | 94 | before = time.time() |
---|
[2639] | 95 | self.tool.logdebug("QUERY : %s" % query) |
---|
| 96 | self.cursor.execute(query) |
---|
| 97 | except self.database.Error, msg : |
---|
| 98 | raise PyKotaStorageError, str(msg) |
---|
| 99 | else : |
---|
[2644] | 100 | # This returns a list of lists. Integers are returned as longs. |
---|
[2741] | 101 | result = self.cursor.fetchall() |
---|
| 102 | after = time.time() |
---|
| 103 | self.tool.logdebug("Query Duration : %.4f seconds" % (after - before)) |
---|
| 104 | return result |
---|
[2639] | 105 | |
---|
| 106 | def doSearch(self, query) : |
---|
| 107 | """Does a search query.""" |
---|
| 108 | result = self.doRawSearch(query) |
---|
[2644] | 109 | if result : |
---|
| 110 | rows = [] |
---|
[2639] | 111 | fields = {} |
---|
| 112 | for i in range(len(self.cursor.description)) : |
---|
[2644] | 113 | fields[i] = self.cursor.description[i][0] |
---|
| 114 | for row in result : |
---|
[2639] | 115 | rowdict = {} |
---|
| 116 | for field in fields.keys() : |
---|
[2644] | 117 | value = row[field] |
---|
| 118 | try : |
---|
| 119 | value = value.encode("UTF-8") |
---|
| 120 | except: |
---|
| 121 | pass |
---|
| 122 | rowdict[fields[field]] = value |
---|
| 123 | rows.append(rowdict) |
---|
| 124 | # returns a list of dicts |
---|
| 125 | return rows |
---|
[2639] | 126 | |
---|
| 127 | def doModify(self, query) : |
---|
| 128 | """Does a (possibly multiple) modify query.""" |
---|
| 129 | query = query.strip() |
---|
| 130 | if not query.endswith(';') : |
---|
| 131 | query += ';' |
---|
| 132 | try : |
---|
[2741] | 133 | before = time.time() |
---|
[2639] | 134 | self.tool.logdebug("QUERY : %s" % query) |
---|
| 135 | self.cursor.execute(query) |
---|
| 136 | except self.database.Error, msg : |
---|
[2773] | 137 | self.tool.logdebug("Query failed : %s" % repr(msg)) |
---|
[2639] | 138 | raise PyKotaStorageError, str(msg) |
---|
[2741] | 139 | else : |
---|
| 140 | after = time.time() |
---|
| 141 | self.tool.logdebug("Query Duration : %.4f seconds" % (after - before)) |
---|
[2639] | 142 | |
---|
| 143 | def doQuote(self, field) : |
---|
| 144 | """Quotes a field for use as a string in SQL queries.""" |
---|
| 145 | if type(field) == type(0.0) : |
---|
| 146 | return field |
---|
| 147 | elif type(field) == type(0) : |
---|
| 148 | return field |
---|
| 149 | elif type(field) == type(0L) : |
---|
| 150 | return field |
---|
| 151 | elif field is not None : |
---|
[2862] | 152 | newfield = self.database.string_literal(field) |
---|
| 153 | try : |
---|
| 154 | return newfield.encode("UTF-8") |
---|
[2863] | 155 | except : |
---|
[2862] | 156 | return newfield |
---|
[2639] | 157 | else : |
---|
[2644] | 158 | self.tool.logdebug("WARNING: field has no type, returning NULL") |
---|
[2639] | 159 | return "NULL" |
---|
| 160 | |
---|
| 161 | def prepareRawResult(self, result) : |
---|
| 162 | """Prepares a raw result by including the headers.""" |
---|
| 163 | if result : |
---|
| 164 | entries = [tuple([f[0] for f in self.cursor.description])] |
---|
[2644] | 165 | for entry in result : |
---|
[2639] | 166 | row = [] |
---|
| 167 | for value in entry : |
---|
| 168 | try : |
---|
| 169 | value = value.encode("UTF-8") |
---|
| 170 | except : |
---|
| 171 | pass |
---|
| 172 | row.append(value) |
---|
| 173 | entries.append(tuple(row)) |
---|
| 174 | return entries |
---|