Author: phd
Date: 2007-04-25 08:56:28 -0600 (Wed, 25 Apr 2007)
New Revision: 2595
Modified:
SQLObject/branches/0.9/docs/News.txt
SQLObject/branches/0.9/sqlobject/mysql/mysqlconnection.py
Log:
Applied the patch 1653898: properly initialize MySQL connection encoding.
Modified: SQLObject/branches/0.9/docs/News.txt
===================================================================
--- SQLObject/branches/0.9/docs/News.txt 2007-04-25 14:55:51 UTC (rev 2594)
+++ SQLObject/branches/0.9/docs/News.txt 2007-04-25 14:56:28 UTC (rev 2595)
@@ -215,6 +215,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.9/sqlobject/mysql/mysqlconnection.py
===================================================================
--- SQLObject/branches/0.9/sqlobject/mysql/mysqlconnection.py 2007-04-25 14:55:51 UTC (rev 2594)
+++ SQLObject/branches/0.9/sqlobject/mysql/mysqlconnection.py 2007-04-25 14:56:28 UTC (rev 2595)
@@ -57,6 +57,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)
@@ -71,6 +78,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):
@@ -92,13 +105,11 @@
# done by calling ping(True) on the connection.
for count in range(3):
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] in (MySQLdb.constants.CR.SERVER_GONE_ERROR, MySQLdb.constants.CR.SERVER_LOST):
if count == 2:
|