Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@malcolmt malcolmt authored
Showing with 16 additions and 4 deletions.
  1. +16 −4 django/db/backends/mysql_old/base.py
View
20 django/db/backends/mysql_old/base.py
@@ -5,6 +5,7 @@
"""
from django.db.backends import util
+from django.utils.encoding import force_unicode
try:
import MySQLdb as Database
except ImportError, e:
@@ -25,6 +26,9 @@
FIELD_TYPE.DATE: util.typecast_date,
FIELD_TYPE.TIME: util.typecast_time,
FIELD_TYPE.DECIMAL: util.typecast_decimal,
+ FIELD_TYPE.STRING: force_unicode,
+ FIELD_TYPE.VAR_STRING: force_unicode,
+ # Note: We don't add a convertor for BLOB here. Doesn't seem to be required.
})
# This should match the numerical portion of the version numbers (we can treat
@@ -87,11 +91,12 @@ def cursor(self):
from django.conf import settings
if not self._valid_connection():
kwargs = {
+ # Note: use_unicode intentonally not set to work around some
+ # backwards-compat issues. We do it manually.
'user': settings.DATABASE_USER,
'db': settings.DATABASE_NAME,
'passwd': settings.DATABASE_PASSWORD,
'conv': django_conversions,
- 'use_unicode': True,
}
if settings.DATABASE_HOST.startswith('/'):
kwargs['unix_socket'] = settings.DATABASE_HOST
@@ -102,9 +107,16 @@ def cursor(self):
kwargs.update(self.options)
self.connection = Database.connect(**kwargs)
cursor = self.connection.cursor()
- if self.connection.get_server_info() >= '4.1':
- cursor.execute("SET NAMES 'utf8'")
- cursor.execute("SET CHARACTER SET 'utf8'")
+ if self.connection.get_server_info() >= '4.1' and not self.connection.character_set_name().startswith('utf8'):
+ if hasattr(self.connection, 'charset'):
+ # MySQLdb < 1.2.1 backwards-compat hacks.
+ conn = self.connection
+ cursor.execute("SET NAMES 'utf8'")
+ cursor.execute("SET CHARACTER SET 'utf8'")
+ to_str = lambda u, dummy=None, c=conn: c.literal(u.encode('utf-8'))
+ conn.converter[unicode] = to_str
+ else:
+ self.connection.set_character_set('utf8')
else:
cursor = self.connection.cursor()
if settings.DEBUG:
Please sign in to comment.
Something went wrong with that request. Please try again.