Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#84 proper init mysql conn encoding

closed-accepted
Oleg Broytman
None
5
2008-03-03
2007-02-07
No

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):

Discussion

1 2 > >> (Page 1 of 2)
  • Oleg Broytman
    Oleg Broytman
    2007-02-07

    Logged In: YES
    user_id=4799
    Originator: NO

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

     
  • Oleg Broytman
    Oleg Broytman
    2007-02-07

    • assigned_to: nobody --> phd
     
  • Logged In: YES
    user_id=113894
    Originator: YES

    File Added: mysql_charset.patch

     
  • new version of the patch

     
    Attachments
  • Logged In: YES
    user_id=113894
    Originator: YES

    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. ;)

     
  • Logged In: YES
    user_id=113894
    Originator: YES

    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.

     
  • Logged In: YES
    user_id=113894
    Originator: YES

    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. ;(

     
  • Oleg Broytman
    Oleg Broytman
    2007-04-25

    • status: open --> open-accepted
     
  • Oleg Broytman
    Oleg Broytman
    2007-04-25

    Logged In: YES
    user_id=4799
    Originator: NO

    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!

     
  • Oleg Broytman
    Oleg Broytman
    2008-03-03

    • status: open-accepted --> closed-accepted
     
1 2 > >> (Page 1 of 2)