Author: phd
Date: 2007-04-25 08:52:51 -0600 (Wed, 25 Apr 2007)
New Revision: 2593
Modified:
SQLObject/branches/0.7/docs/News.txt
SQLObject/branches/0.7/sqlobject/mysql/mysqlconnection.py
Log:
Applied the patch 1653898: properly initialize MySQL connection encoding.
Modified: SQLObject/branches/0.7/docs/News.txt
===================================================================
--- SQLObject/branches/0.7/docs/News.txt 2007-04-25 14:17:43 UTC (rev 2592)
+++ SQLObject/branches/0.7/docs/News.txt 2007-04-25 14:52:51 UTC (rev 2593)
@@ -23,6 +23,8 @@
their parameter must be a string; now you can pass an SQLExpression:
Table.q.name.contains(func.upper('a')), for example.
+* Yet another patch to properly initialize MySQL connection encoding.
+
SQLObject 0.7.5
===============
Modified: SQLObject/branches/0.7/sqlobject/mysql/mysqlconnection.py
===================================================================
--- SQLObject/branches/0.7/sqlobject/mysql/mysqlconnection.py 2007-04-25 14:17:43 UTC (rev 2592)
+++ SQLObject/branches/0.7/sqlobject/mysql/mysqlconnection.py 2007-04-25 14:52:51 UTC (rev 2593)
@@ -48,6 +48,13 @@
connectionFromURI = classmethod(connectionFromURI)
def makeConnection(self):
+ from MySQLdb.connections import Connection
+ if not hasattr(Connection, 'set_character_set'):
+ # monkeypatch pre MySQLdb 1.2.1
+ encoding = self.dbEncoding
+ def character_set_name(self):
+ return encoding + '_' + encoding
+ Connection.character_set_name = character_set_name
try:
conn = self.module.connect(host=self.host, port=self.port,
db=self.db, user=self.user, passwd=self.password, **self.kw)
@@ -62,6 +69,12 @@
if hasattr(conn, 'autocommit'):
conn.autocommit(bool(self.autoCommit))
+ if hasattr(conn, 'set_character_set'): # MySQLdb 1.2.1 and later
+ conn.set_character_set(self.dbEncoding)
+ else: # pre MySQLdb 1.2.1
+ # works along with monkeypatching code above
+ conn.query("SET NAMES %s" % self.dbEncoding)
+
return conn
def _setAutoCommit(self, conn, auto):
@@ -71,13 +84,11 @@
def _executeRetry(self, conn, cursor, query):
while 1:
try:
- if self.need_unicode:
- # For MysqlDB 1.2.1 and later, we go
- # encoding->unicode->charset (in the mysql db)
- myquery = unicode(query, self.encoding)
- return cursor.execute(myquery)
- else:
- return cursor.execute(query)
+ # For MySQLdb 1.2.1 and later, we go
+ # encoding->unicode->charset (in the mysql db)
+ if self.need_unicode and not isinstance(query, unicode):
+ query = unicode(query, self.encoding)
+ return cursor.execute(query)
except MySQLdb.OperationalError, e:
if e.args[0] == 2013: # SERVER_LOST error
if self.debug:
|