Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #4770 -- Fixed some Unicode conversion problems in the mysql_ol…

…d backend

with old MySQLdb versions. Tested against 1.2.0, 1.2.1 and 1.2.1p2 with only
expected failures.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@5623 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 671fb8aa53163aafcf26912b4490c467ed65afae 1 parent 8f96bd2
Malcolm Tredinnick authored July 06, 2007

Showing 1 changed file with 16 additions and 4 deletions. Show diff stats Hide diff stats

  1. 20  django/db/backends/mysql_old/base.py
20  django/db/backends/mysql_old/base.py
@@ -5,6 +5,7 @@
5 5
 """
6 6
 
7 7
 from django.db.backends import util
  8
+from django.utils.encoding import force_unicode
8 9
 try:
9 10
     import MySQLdb as Database
10 11
 except ImportError, e:
@@ -25,6 +26,9 @@
25 26
     FIELD_TYPE.DATE: util.typecast_date,
26 27
     FIELD_TYPE.TIME: util.typecast_time,
27 28
     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.
28 32
 })
29 33
 
30 34
 # This should match the numerical portion of the version numbers (we can treat
@@ -87,11 +91,12 @@ def cursor(self):
87 91
         from django.conf import settings
88 92
         if not self._valid_connection():
89 93
             kwargs = {
  94
+                # Note: use_unicode intentonally not set to work around some
  95
+                # backwards-compat issues. We do it manually.
90 96
                 'user': settings.DATABASE_USER,
91 97
                 'db': settings.DATABASE_NAME,
92 98
                 'passwd': settings.DATABASE_PASSWORD,
93 99
                 'conv': django_conversions,
94  
-                'use_unicode': True,
95 100
             }
96 101
             if settings.DATABASE_HOST.startswith('/'):
97 102
                 kwargs['unix_socket'] = settings.DATABASE_HOST
@@ -102,9 +107,16 @@ def cursor(self):
102 107
             kwargs.update(self.options)
103 108
             self.connection = Database.connect(**kwargs)
104 109
             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')
108 120
         else:
109 121
             cursor = self.connection.cursor()
110 122
         if settings.DEBUG:

0 notes on commit 671fb8a

Please sign in to comment.
Something went wrong with that request. Please try again.