105 | | if self.connection.get_server_info() >= '4.1': |
106 | | cursor.execute("SET NAMES 'utf8'") |
107 | | cursor.execute("SET CHARACTER SET 'utf8'") |
| 106 | if self.connection.get_server_info() >= '4.1' and not self.connection.character_set_name().startswith('utf8'): |
| 107 | if hasattr(self.connection, 'charset'): |
| 108 | # MySQLdb prior to 1.2.1p2 backwards-compat hacks. |
| 109 | conn = self.connection |
| 110 | cursor.execute("SET NAMES 'utf8'") |
| 111 | cursor.execute("SET CHARACTER SET 'utf8'") |
| 112 | # XXX: At this point, conn.charset is still wrong, so we |
| 113 | # need to fix the converters. Writing to conn.charset gives |
| 114 | # the same errors as the following code, btw. |
| 115 | to_uni = lambda u, dummy=None, c=conn: c.literal(u.encode('utf-8')) |
| 116 | from_uni = lambda u: u.decode('utf-8') |
| 117 | conn.converter[unicode] = to_uni |
| 118 | django_conversions[FIELD_TYPE.STRING] = from_uni |
| 119 | django_conversions[FIELD_TYPE.VAR_STRING] = from_uni |
| 120 | # Note: we don't handle FIELD_TYPE.BLOB here. |
| 121 | else: |
| 122 | self.connection.set_character_set('utf8') |