Share

SQLObject

Tracker: Patches

5 proper init mysql conn encoding - ID: 1653898
Last Update: Comment added ( phd )

Current implementation of the mysqlconnection.py accepts
'sqlobject_encoding' and 'charset' parameters but doesn't quite properly
use them.

In particular, MySQLdb does query.encode(charset) call in execute() where
charset is taken from conn.get_character_name(). The problem is - SQLObject
never initializes it properly.

This one-line patch fixes it by adding a call to conn.set_character_set()
in makeConnection() helper.

Index: sqlobject/mysql/mysqlconnection.py
===================================================================
--- sqlobject/mysql/mysqlconnection.py (revision 2269)
+++ sqlobject/mysql/mysqlconnection.py (working copy)
@@ -62,6 +62,8 @@
if hasattr(conn, 'autocommit'):
conn.autocommit(bool(self.autoCommit))

+ conn.set_character_set(self.encoding)
+
return conn

def _setAutoCommit(self, conn, auto):


Maxim F. Ischenko ( mfi ) - 2007-02-07 07:57

5

Closed

Accepted

Oleg Broytman

None

None

Public


Comments ( 7 )




Date: 2008-03-03 14:48
Sender: phdProject Admin


Some parts of the patch have been applied, some have not; SQLObject
doesn't use "sqlobject_encoding" since 0.7.10, e.g.


Date: 2007-04-25 14:57
Sender: phdProject Admin


I applied a pert of the patch, but I didn't apply the
UnicodeStringLikeConvertor (I have to think about it a bit more to find
problems with the part) and dbEncoding/sqlobject_encoding unification.
Committed in the revisions 2593-2597 (0.7, 0.8, 0.9, trunk and docs). Thank
you!


Date: 2007-02-07 14:54
Sender: mfi


Note that I also had to tweak UnicodeStringLikeConvertor so that unicode
parameters are encoded into utf8. Without this tweak I got errors like this
one when unicode parameters are passed into queries built with sqlbuilder:

File "/home/max/oss-projects/SQLObject-0.7/sqlobject/sqlbuilder.py",
line 237, in __sqlrepr__
return "(%s %s %s)" % (s1, self.op, s2)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 19:
ordinal not in range(128)

The converter trick is controversial but I haven't found a better
solution. ;(



Date: 2007-02-07 14:46
Sender: mfi


It was tested against MySQL 4.1 (5.x should work while 4.0 shouldn't) and
MySQLdb driver versions 1.2.0 and 1.2.1. I only tested it with utf8
encoding, the other ones like latin1 should work though I don't know why
would you want to use them.


Date: 2007-02-07 14:44
Sender: mfi


Please ignore that one-line fix; it won't work with MySQLdb 1.2.0 and
there are other issues as I have discovered during testing. I attached new
patch which is quite more involved. ;)


Date: 2007-02-07 14:42
Sender: mfi


File Added: mysql_charset.patch


Date: 2007-02-07 08:34
Sender: phdProject Admin


Thank you. Does all MySQLdb versions have the call or have I test "if
hasattr(conn, 'set_character_set')"?


Log in to comment.




Attached File ( 1 )

Filename Description Download
mysql_charset.patch new version of the patch Download

Changes ( 7 )

Field Old Value Date By
close_date - 2008-03-03 14:48 phd
status_id Open 2008-03-03 14:48 phd
resolution_id None 2007-04-25 14:57 phd
File Deleted 214845: 2007-02-07 15:08 phd
File Added 214888: mysql_charset.patch 2007-02-07 14:42 mfi
assigned_to nobody 2007-02-07 08:34 phd
File Added 214845: mysql_charset.patch 2007-02-07 07:57 mfi