[3260] | 1 | # -*- coding: UTF-8 -*- |
---|
[2639] | 2 | # |
---|
[3260] | 3 | # PyKota : Print Quotas for CUPS |
---|
[2639] | 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 |
---|
[2639] | 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 |
---|
[2639] | 9 | # (at your option) any later version. |
---|
[3260] | 10 | # |
---|
[2639] | 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/>. |
---|
[2639] | 18 | # |
---|
[2645] | 19 | # $Id$ |
---|
[2639] | 20 | # |
---|
| 21 | # |
---|
| 22 | |
---|
[3184] | 23 | """This module defines a class to access to a MySQL database backend.""" |
---|
| 24 | |
---|
[2741] | 25 | import time |
---|
| 26 | |
---|
[3288] | 27 | from pykota.errors import PyKotaStorageError |
---|
| 28 | from pykota.storage import 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)) |
---|
[3009] | 49 | try : |
---|
[3138] | 50 | self.database = MySQLdb.connect(host=host, port=port, db=dbname, user=user, passwd=passwd, charset="utf8") |
---|
| 51 | except TypeError : |
---|
| 52 | self.tool.logdebug("'charset' argument not allowed with this version of python-mysqldb, retrying without...") |
---|
| 53 | self.database = MySQLdb.connect(host=host, port=port, db=dbname, user=user, passwd=passwd) |
---|
| 54 | |
---|
| 55 | try : |
---|
[3009] | 56 | self.database.autocommit(1) |
---|
| 57 | except AttributeError : |
---|
[3010] | 58 | raise PyKotaStorageError, _("Your version of python-mysqldb is too old. Please install a newer release.") |
---|
[2644] | 59 | self.cursor = self.database.cursor() |
---|
[2954] | 60 | self.cursor.execute("SET NAMES 'utf8';") |
---|
[2821] | 61 | self.cursor.execute("SET TRANSACTION ISOLATION LEVEL READ COMMITTED;") # Same as PostgreSQL and Oracle's default |
---|
[2639] | 62 | self.closed = 0 |
---|
| 63 | self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user)) |
---|
| 64 | |
---|
| 65 | def close(self) : |
---|
| 66 | """Closes the database connection.""" |
---|
| 67 | if not self.closed : |
---|
[2644] | 68 | self.cursor.close() |
---|
[2639] | 69 | self.database.close() |
---|
| 70 | self.closed = 1 |
---|
| 71 | self.tool.logdebug("Database closed.") |
---|
| 72 | |
---|
| 73 | def beginTransaction(self) : |
---|
| 74 | """Starts a transaction.""" |
---|
[2741] | 75 | self.before = time.time() |
---|
[2646] | 76 | self.cursor.execute("BEGIN;") |
---|
[2639] | 77 | self.tool.logdebug("Transaction begins...") |
---|
| 78 | |
---|
| 79 | def commitTransaction(self) : |
---|
| 80 | """Commits a transaction.""" |
---|
| 81 | self.database.commit() |
---|
[2741] | 82 | after = time.time() |
---|
[2639] | 83 | self.tool.logdebug("Transaction committed.") |
---|
[3183] | 84 | #self.tool.logdebug("Transaction duration : %.4f seconds" % (after - self.before)) |
---|
[2639] | 85 | |
---|
| 86 | def rollbackTransaction(self) : |
---|
| 87 | """Rollbacks a transaction.""" |
---|
| 88 | self.database.rollback() |
---|
[2741] | 89 | after = time.time() |
---|
[2639] | 90 | self.tool.logdebug("Transaction aborted.") |
---|
[3183] | 91 | #self.tool.logdebug("Transaction duration : %.4f seconds" % (after - self.before)) |
---|
[2639] | 92 | |
---|
| 93 | def doRawSearch(self, query) : |
---|
| 94 | """Does a raw search query.""" |
---|
| 95 | query = query.strip() |
---|
| 96 | if not query.endswith(';') : |
---|
| 97 | query += ';' |
---|
| 98 | try : |
---|
[2741] | 99 | before = time.time() |
---|
[2639] | 100 | self.tool.logdebug("QUERY : %s" % query) |
---|
| 101 | self.cursor.execute(query) |
---|
| 102 | except self.database.Error, msg : |
---|
| 103 | raise PyKotaStorageError, str(msg) |
---|
| 104 | else : |
---|
[2644] | 105 | # This returns a list of lists. Integers are returned as longs. |
---|
[2741] | 106 | result = self.cursor.fetchall() |
---|
| 107 | after = time.time() |
---|
[3183] | 108 | #self.tool.logdebug("Query Duration : %.4f seconds" % (after - before)) |
---|
[2741] | 109 | return result |
---|
[2639] | 110 | |
---|
| 111 | def doSearch(self, query) : |
---|
| 112 | """Does a search query.""" |
---|
| 113 | result = self.doRawSearch(query) |
---|
[2644] | 114 | if result : |
---|
| 115 | rows = [] |
---|
[2639] | 116 | fields = {} |
---|
| 117 | for i in range(len(self.cursor.description)) : |
---|
[2644] | 118 | fields[i] = self.cursor.description[i][0] |
---|
| 119 | for row in result : |
---|
[2639] | 120 | rowdict = {} |
---|
| 121 | for field in fields.keys() : |
---|
[2644] | 122 | value = row[field] |
---|
| 123 | try : |
---|
| 124 | value = value.encode("UTF-8") |
---|
| 125 | except: |
---|
| 126 | pass |
---|
| 127 | rowdict[fields[field]] = value |
---|
| 128 | rows.append(rowdict) |
---|
| 129 | # returns a list of dicts |
---|
| 130 | return rows |
---|
[2639] | 131 | |
---|
| 132 | def doModify(self, query) : |
---|
| 133 | """Does a (possibly multiple) modify query.""" |
---|
| 134 | query = query.strip() |
---|
| 135 | if not query.endswith(';') : |
---|
| 136 | query += ';' |
---|
| 137 | try : |
---|
[2741] | 138 | before = time.time() |
---|
[2639] | 139 | self.tool.logdebug("QUERY : %s" % query) |
---|
| 140 | self.cursor.execute(query) |
---|
| 141 | except self.database.Error, msg : |
---|
[2773] | 142 | self.tool.logdebug("Query failed : %s" % repr(msg)) |
---|
[2639] | 143 | raise PyKotaStorageError, str(msg) |
---|
[2741] | 144 | else : |
---|
| 145 | after = time.time() |
---|
[3183] | 146 | #self.tool.logdebug("Query Duration : %.4f seconds" % (after - before)) |
---|
[2639] | 147 | |
---|
| 148 | def doQuote(self, field) : |
---|
| 149 | """Quotes a field for use as a string in SQL queries.""" |
---|
| 150 | if type(field) == type(0.0) : |
---|
| 151 | return field |
---|
| 152 | elif type(field) == type(0) : |
---|
| 153 | return field |
---|
| 154 | elif type(field) == type(0L) : |
---|
| 155 | return field |
---|
| 156 | elif field is not None : |
---|
[2862] | 157 | newfield = self.database.string_literal(field) |
---|
| 158 | try : |
---|
| 159 | return newfield.encode("UTF-8") |
---|
[2863] | 160 | except : |
---|
[2862] | 161 | return newfield |
---|
[2639] | 162 | else : |
---|
[2644] | 163 | self.tool.logdebug("WARNING: field has no type, returning NULL") |
---|
[2639] | 164 | return "NULL" |
---|
| 165 | |
---|
| 166 | def prepareRawResult(self, result) : |
---|
| 167 | """Prepares a raw result by including the headers.""" |
---|
| 168 | if result : |
---|
| 169 | entries = [tuple([f[0] for f in self.cursor.description])] |
---|
[2644] | 170 | for entry in result : |
---|
[2639] | 171 | row = [] |
---|
| 172 | for value in entry : |
---|
| 173 | try : |
---|
| 174 | value = value.encode("UTF-8") |
---|
| 175 | except : |
---|
| 176 | pass |
---|
| 177 | row.append(value) |
---|
| 178 | entries.append(tuple(row)) |
---|
| 179 | return entries |
---|