[SQL-CVS] r2596 - SQLObject/trunk/sqlobject/mysql
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
|
From: <sub...@co...> - 2007-04-25 14:56:55
|
Author: phd
Date: 2007-04-25 08:56:49 -0600 (Wed, 25 Apr 2007)
New Revision: 2596
Modified:
SQLObject/trunk/sqlobject/mysql/mysqlconnection.py
Log:
Applied the patch 1653898: properly initialize MySQL connection encoding.
Modified: SQLObject/trunk/sqlobject/mysql/mysqlconnection.py
===================================================================
--- SQLObject/trunk/sqlobject/mysql/mysqlconnection.py 2007-04-25 14:56:28 UTC (rev 2595)
+++ SQLObject/trunk/sqlobject/mysql/mysqlconnection.py 2007-04-25 14:56:49 UTC (rev 2596)
@@ -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:
|