Changeset 3413 for pykota/trunk/pykota/storages/mysqlstorage.py
- Timestamp:
- 09/27/08 22:02:37 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/storages/mysqlstorage.py
r3411 r3413 8 8 # the Free Software Foundation, either version 3 of the License, or 9 9 # (at your option) any later version. 10 # 10 # 11 11 # This program is distributed in the hope that it will be useful, 12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 14 # GNU General Public License for more details. 15 # 15 # 16 16 # You should have received a copy of the GNU General Public License 17 17 # along with this program. If not, see <http://www.gnu.org/licenses/>. … … 29 29 try : 30 30 import MySQLdb 31 except ImportError : 31 except ImportError : 32 32 import sys 33 33 # TODO : to translate or not to translate ? … … 41 41 (host, port) = host.split(":") 42 42 port = int(port) 43 except ValueError : 43 except ValueError : 44 44 port = 3306 # Use the default MySQL port 45 45 46 46 self.tool.logdebug("Trying to open database (host=%s, port=%s, dbname=%s, user=%s)..." % (host, port, dbname, user)) 47 47 try : 48 48 self.database = MySQLdb.connect(host=host, port=port, db=dbname, user=user, passwd=passwd, charset="utf8") 49 except TypeError : 49 except TypeError : 50 50 self.tool.logdebug("'charset' argument not allowed with this version of python-mysqldb, retrying without...") 51 51 self.database = MySQLdb.connect(host=host, port=port, db=dbname, user=user, passwd=passwd) 52 52 53 53 try : 54 54 self.database.autocommit(1) 55 except AttributeError : 55 except AttributeError : 56 56 raise PyKotaStorageError, _("Your version of python-mysqldb is too old. Please install a newer release.") 57 57 self.cursor = self.database.cursor() … … 62 62 try : 63 63 # Here we try to select a string (an é) which is 64 # already encoded in UTF-8. If python-mysqldb suffers from 64 # already encoded in UTF-8. If python-mysqldb suffers from 65 65 # the double encoding problem, we will catch the exception 66 66 # and activate a workaround. 67 67 self.cursor.execute("SELECT '%s';" % (chr(0xc3) + chr(0xa9))) # é in UTF-8 68 68 self.cursor.fetchall() 69 except UnicodeDecodeError : 69 except UnicodeDecodeError : 70 70 self.needsworkaround = True 71 71 self.tool.logdebug("Database needs encoding workaround.") … … 73 73 self.needsworkaround = False 74 74 self.tool.logdebug("Database doesn't need encoding workaround.") 75 76 def close(self) : 75 76 def close(self) : 77 77 """Closes the database connection.""" 78 78 if not self.closed : … … 81 81 self.closed = True 82 82 self.tool.logdebug("Database closed.") 83 84 def beginTransaction(self) : 83 84 def beginTransaction(self) : 85 85 """Starts a transaction.""" 86 86 self.cursor.execute("BEGIN;") 87 87 self.tool.logdebug("Transaction begins...") 88 89 def commitTransaction(self) : 88 89 def commitTransaction(self) : 90 90 """Commits a transaction.""" 91 91 self.database.commit() 92 92 self.tool.logdebug("Transaction committed.") 93 94 def rollbackTransaction(self) : 93 94 def rollbackTransaction(self) : 95 95 """Rollbacks a transaction.""" 96 96 self.database.rollback() 97 97 self.tool.logdebug("Transaction aborted.") 98 98 99 99 def doRawSearch(self, query) : 100 100 """Does a raw search query.""" 101 query = query.strip() 102 if not query.endswith(';') : 101 query = query.strip() 102 if not query.endswith(';') : 103 103 query += ';' 104 104 self.querydebug("QUERY : %s" % query) 105 if self.needsworkaround : 105 if self.needsworkaround : 106 106 query = query.decode("UTF-8") 107 107 try : 108 108 self.cursor.execute(query) 109 except self.database.Error, msg : 109 except self.database.Error, msg : 110 110 raise PyKotaStorageError, repr(msg) 111 else : 111 else : 112 112 # This returns a list of lists. Integers are returned as longs. 113 113 return self.cursor.fetchall() 114 115 def doSearch(self, query) : 114 115 def doSearch(self, query) : 116 116 """Does a search query.""" 117 117 result = self.doRawSearch(query) … … 131 131 def doModify(self, query) : 132 132 """Does a (possibly multiple) modify query.""" 133 query = query.strip() 134 if not query.endswith(';') : 133 query = query.strip() 134 if not query.endswith(';') : 135 135 query += ';' 136 136 self.querydebug("QUERY : %s" % query) 137 if self.needsworkaround : 137 if self.needsworkaround : 138 138 query = query.decode("UTF-8") 139 139 try : 140 140 self.cursor.execute(query) 141 except self.database.Error, msg : 141 except self.database.Error, msg : 142 142 self.tool.logdebug("Query failed : %s" % repr(msg)) 143 143 raise PyKotaStorageError, repr(msg) 144 144 145 145 def doQuote(self, field) : 146 146 """Quotes a field for use as a string in SQL queries."""