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