Ticket #4741: mysql_old_base.diff
File mysql_old_base.diff, 2.6 KB (added by , 17 years ago) |
---|
-
./django/db/backends/mysql_old/base.py
37 37 # http://dev.mysql.com/doc/refman/5.0/en/news.html . 38 38 server_version_re = re.compile(r'(\d{1,2})\.(\d{1,2})\.(\d{1,2})') 39 39 40 # This is an extra debug layer over MySQL queries, to display warnings. 41 # It's only used when DEBUG=True. 42 class MysqlDebugWrapper: 40 class MysqlWrapper: 43 41 def __init__(self, cursor): 44 42 self.cursor = cursor 45 43 44 def __getattr__(self, attr): 45 if attr in self.__dict__: 46 return self.__dict__[attr] 47 else: 48 return getattr(self.cursor, attr) 49 50 # This is an extra debug layer over MySQL queries, to display warnings. 51 # It's only used when DEBUG=True. 52 class MysqlDebugWrapper(MysqlWrapper): 46 53 def execute(self, sql, params=()): 47 54 try: 48 55 return self.cursor.execute(sql, params) … … 57 64 self.cursor.execute("SHOW WARNINGS") 58 65 raise Database.Warning("%s: %s" % (w, self.cursor.fetchall())) 59 66 60 def __getattr__(self, attr): 61 if attr in self.__dict__: 62 return self.__dict__[attr] 63 else: 64 return getattr(self.cursor, attr) 67 class MysqlUnicodeWrapper(MysqlWrapper): 68 69 def _decode_results(self, result_raw): 70 """ 71 decode all byte string to unicode with the server encoding. 72 """ 73 result = [] 74 for item in result_raw: 75 if isinstance(item, str): 76 item = item.decode(self.cursor.server_encoding) 77 result.append(item) 78 return tuple(result) 79 80 def fetchone(self): 81 result_raw = self.cursor.fetchone() 82 return self._decode_results(result_raw) 83 84 def fetchall(self): 85 result_raw = self.cursor.fetchall() 86 result = [] 87 for line in result_raw: 88 result.append(self._decode_results(line)) 89 return tuple(result) 90 65 91 66 92 class DatabaseFeatures(BaseDatabaseFeatures): 67 93 autoindexes_primary_keys = False … … 193 219 self.connection.set_character_set('utf8') 194 220 else: 195 221 cursor = self.connection.cursor() 196 return cursor 222 223 cursor.execute("SHOW VARIABLES LIKE %s;", ("character_set_server",)) 224 cursor.server_encoding = cursor.fetchone()[1] 225 226 return MysqlUnicodeWrapper(cursor) 197 227 198 228 def make_debug_cursor(self, cursor): 199 229 return BaseDatabaseWrapper.make_debug_cursor(self, MysqlDebugWrapper(cursor))