[3489] | 1 | # -*- coding: utf-8 -*- |
---|
[2639] | 2 | # |
---|
[3260] | 3 | # PyKota : Print Quotas for CUPS |
---|
[2639] | 4 | # |
---|
[3561] | 5 | # (c) 2003-2013 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. |
---|
[3413] | 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. |
---|
[3413] | 15 | # |
---|
[2639] | 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 | |
---|
[3288] | 25 | from pykota.errors import PyKotaStorageError |
---|
| 26 | from pykota.storage import BaseStorage |
---|
[2639] | 27 | from pykota.storages.sql import SQLStorage |
---|
| 28 | |
---|
| 29 | try : |
---|
| 30 | import MySQLdb |
---|
[3413] | 31 | except ImportError : |
---|
[2639] | 32 | import sys |
---|
| 33 | # TODO : to translate or not to translate ? |
---|
| 34 | raise PyKotaStorageError, "This python version (%s) doesn't seem to have the MySQL module installed correctly." % sys.version.split()[0] |
---|
| 35 | |
---|
| 36 | class Storage(BaseStorage, SQLStorage) : |
---|
| 37 | def __init__(self, pykotatool, host, dbname, user, passwd) : |
---|
| 38 | """Opens the MySQL database connection.""" |
---|
| 39 | BaseStorage.__init__(self, pykotatool) |
---|
| 40 | try : |
---|
| 41 | (host, port) = host.split(":") |
---|
| 42 | port = int(port) |
---|
[3413] | 43 | except ValueError : |
---|
[2874] | 44 | port = 3306 # Use the default MySQL port |
---|
[3413] | 45 | |
---|
[3419] | 46 | self.tool.logdebug("Trying to open database (host=%s, port=%s, dbname=%s, user=%s)..." \ |
---|
| 47 | % (repr(host), |
---|
| 48 | repr(port), |
---|
| 49 | repr(dbname), |
---|
| 50 | repr(user))) |
---|
[3009] | 51 | try : |
---|
[3419] | 52 | self.database = MySQLdb.connect(host=host, |
---|
| 53 | port=port, |
---|
| 54 | db=dbname, |
---|
| 55 | user=user, |
---|
| 56 | passwd=passwd, |
---|
| 57 | charset="utf8") |
---|
[3413] | 58 | except TypeError : |
---|
[3138] | 59 | self.tool.logdebug("'charset' argument not allowed with this version of python-mysqldb, retrying without...") |
---|
[3419] | 60 | self.database = MySQLdb.connect(host=host, |
---|
| 61 | port=port, |
---|
| 62 | db=dbname, |
---|
| 63 | user=user, |
---|
| 64 | passwd=passwd) |
---|
[3413] | 65 | |
---|
[3138] | 66 | try : |
---|
[3009] | 67 | self.database.autocommit(1) |
---|
[3413] | 68 | except AttributeError : |
---|
[3010] | 69 | raise PyKotaStorageError, _("Your version of python-mysqldb is too old. Please install a newer release.") |
---|
[2644] | 70 | self.cursor = self.database.cursor() |
---|
[2954] | 71 | self.cursor.execute("SET NAMES 'utf8';") |
---|
[2821] | 72 | self.cursor.execute("SET TRANSACTION ISOLATION LEVEL READ COMMITTED;") # Same as PostgreSQL and Oracle's default |
---|
[3310] | 73 | self.closed = False |
---|
[3419] | 74 | self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" \ |
---|
| 75 | % (repr(host), |
---|
| 76 | repr(port), |
---|
| 77 | repr(dbname), |
---|
| 78 | repr(user))) |
---|
[3310] | 79 | try : |
---|
| 80 | # Here we try to select a string (an é) which is |
---|
[3413] | 81 | # already encoded in UTF-8. If python-mysqldb suffers from |
---|
[3310] | 82 | # the double encoding problem, we will catch the exception |
---|
| 83 | # and activate a workaround. |
---|
| 84 | self.cursor.execute("SELECT '%s';" % (chr(0xc3) + chr(0xa9))) # é in UTF-8 |
---|
| 85 | self.cursor.fetchall() |
---|
[3413] | 86 | except UnicodeDecodeError : |
---|
[3310] | 87 | self.needsworkaround = True |
---|
| 88 | self.tool.logdebug("Database needs encoding workaround.") |
---|
| 89 | else : |
---|
| 90 | self.needsworkaround = False |
---|
| 91 | self.tool.logdebug("Database doesn't need encoding workaround.") |
---|
[3413] | 92 | |
---|
| 93 | def close(self) : |
---|
[2639] | 94 | """Closes the database connection.""" |
---|
| 95 | if not self.closed : |
---|
[2644] | 96 | self.cursor.close() |
---|
[2639] | 97 | self.database.close() |
---|
[3310] | 98 | self.closed = True |
---|
[2639] | 99 | self.tool.logdebug("Database closed.") |
---|
[3413] | 100 | |
---|
| 101 | def beginTransaction(self) : |
---|
[2639] | 102 | """Starts a transaction.""" |
---|
[2646] | 103 | self.cursor.execute("BEGIN;") |
---|
[2639] | 104 | self.tool.logdebug("Transaction begins...") |
---|
[3413] | 105 | |
---|
| 106 | def commitTransaction(self) : |
---|
[2639] | 107 | """Commits a transaction.""" |
---|
| 108 | self.database.commit() |
---|
| 109 | self.tool.logdebug("Transaction committed.") |
---|
[3413] | 110 | |
---|
| 111 | def rollbackTransaction(self) : |
---|
[2639] | 112 | """Rollbacks a transaction.""" |
---|
| 113 | self.database.rollback() |
---|
| 114 | self.tool.logdebug("Transaction aborted.") |
---|
[3413] | 115 | |
---|
[2639] | 116 | def doRawSearch(self, query) : |
---|
| 117 | """Does a raw search query.""" |
---|
[3413] | 118 | query = query.strip() |
---|
| 119 | if not query.endswith(';') : |
---|
[2639] | 120 | query += ';' |
---|
[3310] | 121 | self.querydebug("QUERY : %s" % query) |
---|
[3413] | 122 | if self.needsworkaround : |
---|
[3310] | 123 | query = query.decode("UTF-8") |
---|
[2639] | 124 | try : |
---|
| 125 | self.cursor.execute(query) |
---|
[3413] | 126 | except self.database.Error, msg : |
---|
[3308] | 127 | raise PyKotaStorageError, repr(msg) |
---|
[3413] | 128 | else : |
---|
[2644] | 129 | # This returns a list of lists. Integers are returned as longs. |
---|
[3310] | 130 | return self.cursor.fetchall() |
---|
[3413] | 131 | |
---|
| 132 | def doSearch(self, query) : |
---|
[2639] | 133 | """Does a search query.""" |
---|
| 134 | result = self.doRawSearch(query) |
---|
[2644] | 135 | if result : |
---|
| 136 | rows = [] |
---|
[2639] | 137 | fields = {} |
---|
| 138 | for i in range(len(self.cursor.description)) : |
---|
[2644] | 139 | fields[i] = self.cursor.description[i][0] |
---|
| 140 | for row in result : |
---|
[2639] | 141 | rowdict = {} |
---|
| 142 | for field in fields.keys() : |
---|
[3310] | 143 | rowdict[fields[field]] = row[field] |
---|
[2644] | 144 | rows.append(rowdict) |
---|
| 145 | # returns a list of dicts |
---|
| 146 | return rows |
---|
[2639] | 147 | |
---|
| 148 | def doModify(self, query) : |
---|
| 149 | """Does a (possibly multiple) modify query.""" |
---|
[3413] | 150 | query = query.strip() |
---|
| 151 | if not query.endswith(';') : |
---|
[2639] | 152 | query += ';' |
---|
[3310] | 153 | self.querydebug("QUERY : %s" % query) |
---|
[3413] | 154 | if self.needsworkaround : |
---|
[3310] | 155 | query = query.decode("UTF-8") |
---|
[2639] | 156 | try : |
---|
| 157 | self.cursor.execute(query) |
---|
[3413] | 158 | except self.database.Error, msg : |
---|
[2773] | 159 | self.tool.logdebug("Query failed : %s" % repr(msg)) |
---|
[3308] | 160 | raise PyKotaStorageError, repr(msg) |
---|
[3413] | 161 | |
---|
[2639] | 162 | def doQuote(self, field) : |
---|
| 163 | """Quotes a field for use as a string in SQL queries.""" |
---|
| 164 | if type(field) == type(0.0) : |
---|
| 165 | return field |
---|
| 166 | elif type(field) == type(0) : |
---|
| 167 | return field |
---|
| 168 | elif type(field) == type(0L) : |
---|
| 169 | return field |
---|
| 170 | elif field is not None : |
---|
[3310] | 171 | return self.database.string_literal(field) |
---|
[2639] | 172 | else : |
---|
| 173 | return "NULL" |
---|
| 174 | |
---|
| 175 | def prepareRawResult(self, result) : |
---|
| 176 | """Prepares a raw result by including the headers.""" |
---|
| 177 | if result : |
---|
[3310] | 178 | return [tuple([f[0] for f in self.cursor.description])] \ |
---|
| 179 | + list(result) |
---|