[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) |
---|
[2741] | 50 | self.database.autocommit(1) |
---|
[2644] | 51 | self.cursor = self.database.cursor() |
---|
[2954] | 52 | self.cursor.execute("SET NAMES 'utf8';") |
---|
[2821] | 53 | self.cursor.execute("SET TRANSACTION ISOLATION LEVEL READ COMMITTED;") # Same as PostgreSQL and Oracle's default |
---|
[2639] | 54 | self.closed = 0 |
---|
| 55 | self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user)) |
---|
| 56 | |
---|
| 57 | def close(self) : |
---|
| 58 | """Closes the database connection.""" |
---|
| 59 | if not self.closed : |
---|
[2644] | 60 | self.cursor.close() |
---|
[2639] | 61 | self.database.close() |
---|
| 62 | self.closed = 1 |
---|
| 63 | self.tool.logdebug("Database closed.") |
---|
| 64 | |
---|
| 65 | def beginTransaction(self) : |
---|
| 66 | """Starts a transaction.""" |
---|
[2741] | 67 | self.before = time.time() |
---|
[2646] | 68 | self.cursor.execute("BEGIN;") |
---|
[2639] | 69 | self.tool.logdebug("Transaction begins...") |
---|
| 70 | |
---|
| 71 | def commitTransaction(self) : |
---|
| 72 | """Commits a transaction.""" |
---|
| 73 | self.database.commit() |
---|
[2741] | 74 | after = time.time() |
---|
[2639] | 75 | self.tool.logdebug("Transaction committed.") |
---|
[2741] | 76 | self.tool.logdebug("Transaction duration : %.4f seconds" % (after - self.before)) |
---|
[2639] | 77 | |
---|
| 78 | def rollbackTransaction(self) : |
---|
| 79 | """Rollbacks a transaction.""" |
---|
| 80 | self.database.rollback() |
---|
[2741] | 81 | after = time.time() |
---|
[2639] | 82 | self.tool.logdebug("Transaction aborted.") |
---|
[2741] | 83 | self.tool.logdebug("Transaction duration : %.4f seconds" % (after - self.before)) |
---|
[2639] | 84 | |
---|
| 85 | def doRawSearch(self, query) : |
---|
| 86 | """Does a raw search query.""" |
---|
| 87 | query = query.strip() |
---|
| 88 | if not query.endswith(';') : |
---|
| 89 | query += ';' |
---|
| 90 | try : |
---|
[2741] | 91 | before = time.time() |
---|
[2639] | 92 | self.tool.logdebug("QUERY : %s" % query) |
---|
| 93 | self.cursor.execute(query) |
---|
| 94 | except self.database.Error, msg : |
---|
| 95 | raise PyKotaStorageError, str(msg) |
---|
| 96 | else : |
---|
[2644] | 97 | # This returns a list of lists. Integers are returned as longs. |
---|
[2741] | 98 | result = self.cursor.fetchall() |
---|
| 99 | after = time.time() |
---|
| 100 | self.tool.logdebug("Query Duration : %.4f seconds" % (after - before)) |
---|
| 101 | return result |
---|
[2639] | 102 | |
---|
| 103 | def doSearch(self, query) : |
---|
| 104 | """Does a search query.""" |
---|
| 105 | result = self.doRawSearch(query) |
---|
[2644] | 106 | if result : |
---|
| 107 | rows = [] |
---|
[2639] | 108 | fields = {} |
---|
| 109 | for i in range(len(self.cursor.description)) : |
---|
[2644] | 110 | fields[i] = self.cursor.description[i][0] |
---|
| 111 | for row in result : |
---|
[2639] | 112 | rowdict = {} |
---|
| 113 | for field in fields.keys() : |
---|
[2644] | 114 | value = row[field] |
---|
| 115 | try : |
---|
| 116 | value = value.encode("UTF-8") |
---|
| 117 | except: |
---|
| 118 | pass |
---|
| 119 | rowdict[fields[field]] = value |
---|
| 120 | rows.append(rowdict) |
---|
| 121 | # returns a list of dicts |
---|
| 122 | return rows |
---|
[2639] | 123 | |
---|
| 124 | def doModify(self, query) : |
---|
| 125 | """Does a (possibly multiple) modify query.""" |
---|
| 126 | query = query.strip() |
---|
| 127 | if not query.endswith(';') : |
---|
| 128 | query += ';' |
---|
| 129 | try : |
---|
[2741] | 130 | before = time.time() |
---|
[2639] | 131 | self.tool.logdebug("QUERY : %s" % query) |
---|
| 132 | self.cursor.execute(query) |
---|
| 133 | except self.database.Error, msg : |
---|
[2773] | 134 | self.tool.logdebug("Query failed : %s" % repr(msg)) |
---|
[2639] | 135 | raise PyKotaStorageError, str(msg) |
---|
[2741] | 136 | else : |
---|
| 137 | after = time.time() |
---|
| 138 | self.tool.logdebug("Query Duration : %.4f seconds" % (after - before)) |
---|
[2639] | 139 | |
---|
| 140 | def doQuote(self, field) : |
---|
| 141 | """Quotes a field for use as a string in SQL queries.""" |
---|
| 142 | if type(field) == type(0.0) : |
---|
| 143 | return field |
---|
| 144 | elif type(field) == type(0) : |
---|
| 145 | return field |
---|
| 146 | elif type(field) == type(0L) : |
---|
| 147 | return field |
---|
| 148 | elif field is not None : |
---|
[2862] | 149 | newfield = self.database.string_literal(field) |
---|
| 150 | try : |
---|
| 151 | return newfield.encode("UTF-8") |
---|
[2863] | 152 | except : |
---|
[2862] | 153 | return newfield |
---|
[2639] | 154 | else : |
---|
[2644] | 155 | self.tool.logdebug("WARNING: field has no type, returning NULL") |
---|
[2639] | 156 | return "NULL" |
---|
| 157 | |
---|
| 158 | def prepareRawResult(self, result) : |
---|
| 159 | """Prepares a raw result by including the headers.""" |
---|
| 160 | if result : |
---|
| 161 | entries = [tuple([f[0] for f in self.cursor.description])] |
---|
[2644] | 162 | for entry in result : |
---|
[2639] | 163 | row = [] |
---|
| 164 | for value in entry : |
---|
| 165 | try : |
---|
| 166 | value = value.encode("UTF-8") |
---|
| 167 | except : |
---|
| 168 | pass |
---|
| 169 | row.append(value) |
---|
| 170 | entries.append(tuple(row)) |
---|
| 171 | return entries |
---|