Code

Ticket #4770: mysql_old.diff

File mysql_old.diff, 1.9 KB (added by mtredinnick, 7 years ago)

Broken patch that might act as inspiration.

Line 
1Index: db/backends/mysql_old/base.py
2===================================================================
3--- db/backends/mysql_old/base.py       (revision 5614)
4+++ db/backends/mysql_old/base.py       (working copy)
5@@ -14,6 +14,7 @@
6 from MySQLdb.constants import FIELD_TYPE
7 import types
8 import re
9+import weakref
10 
11 DatabaseError = Database.DatabaseError
12 IntegrityError = Database.IntegrityError
13@@ -102,9 +103,23 @@
14             kwargs.update(self.options)
15             self.connection = Database.connect(**kwargs)
16             cursor = self.connection.cursor()
17-            if self.connection.get_server_info() >= '4.1':
18-                cursor.execute("SET NAMES 'utf8'")
19-                cursor.execute("SET CHARACTER SET 'utf8'")
20+            if self.connection.get_server_info() >= '4.1' and not self.connection.character_set_name().startswith('utf8'):
21+                if hasattr(self.connection, 'charset'):
22+                    # MySQLdb prior to 1.2.1p2 backwards-compat hacks.
23+                    conn = self.connection
24+                    cursor.execute("SET NAMES 'utf8'")
25+                    cursor.execute("SET CHARACTER SET 'utf8'")
26+                    # XXX: At this point, conn.charset is still wrong, so we
27+                    # need to fix the converters. Writing to conn.charset gives
28+                    # the same errors as the following code, btw.
29+                    to_uni = lambda u, dummy=None, c=conn: c.literal(u.encode('utf-8'))
30+                    from_uni = lambda u: u.decode('utf-8')
31+                    conn.converter[unicode] = to_uni
32+                    django_conversions[FIELD_TYPE.STRING] = from_uni
33+                    django_conversions[FIELD_TYPE.VAR_STRING] = from_uni
34+                    # Note: we don't handle FIELD_TYPE.BLOB here.
35+                else:
36+                    self.connection.set_character_set('utf8')
37         else:
38             cursor = self.connection.cursor()
39         if settings.DEBUG: