Django

Code

Changeset 5623

Show
Ignore:
Timestamp:
07/06/07 03:04:04 (1 year ago)
Author:
mtredinnick
Message:

Fixed #4770 -- Fixed some Unicode conversion problems in the mysql_old backend
with old MySQLdb versions. Tested against 1.2.0, 1.2.1 and 1.2.1p2 with only
expected failures.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/backends/mysql_old/base.py

    r5609 r5623  
    66 
    77from django.db.backends import util 
     8from django.utils.encoding import force_unicode 
    89try: 
    910    import MySQLdb as Database 
     
    2627    FIELD_TYPE.TIME: util.typecast_time, 
    2728    FIELD_TYPE.DECIMAL: util.typecast_decimal, 
     29    FIELD_TYPE.STRING: force_unicode, 
     30    FIELD_TYPE.VAR_STRING: force_unicode, 
     31    # Note: We don't add a convertor for BLOB here. Doesn't seem to be required. 
    2832}) 
    2933 
     
    8892        if not self._valid_connection(): 
    8993            kwargs = { 
     94                # Note: use_unicode intentonally not set to work around some 
     95                # backwards-compat issues. We do it manually. 
    9096                'user': settings.DATABASE_USER, 
    9197                'db': settings.DATABASE_NAME, 
    9298                'passwd': settings.DATABASE_PASSWORD, 
    9399                'conv': django_conversions, 
    94                 'use_unicode': True, 
    95100            } 
    96101            if settings.DATABASE_HOST.startswith('/'): 
     
    103108            self.connection = Database.connect(**kwargs) 
    104109            cursor = self.connection.cursor() 
    105             if self.connection.get_server_info() >= '4.1': 
    106                 cursor.execute("SET NAMES 'utf8'") 
    107                 cursor.execute("SET CHARACTER SET 'utf8'") 
     110            if self.connection.get_server_info() >= '4.1' and not self.connection.character_set_name().startswith('utf8'): 
     111                if hasattr(self.connection, 'charset'): 
     112                    # MySQLdb < 1.2.1 backwards-compat hacks. 
     113                    conn = self.connection 
     114                    cursor.execute("SET NAMES 'utf8'") 
     115                    cursor.execute("SET CHARACTER SET 'utf8'") 
     116                    to_str = lambda u, dummy=None, c=conn: c.literal(u.encode('utf-8')) 
     117                    conn.converter[unicode] = to_str 
     118                else: 
     119                    self.connection.set_character_set('utf8') 
    108120        else: 
    109121            cursor = self.connection.cursor()