Ticket #4741: mysql_old_base2.diff
File mysql_old_base2.diff, 3.2 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 def __init__(self, cursor): 69 self.cursor = cursor 70 71 def _decode_results(self, result_raw): 72 """ 73 decode all byte string to unicode with the server encoding. 74 """ 75 if not result_raw: 76 return result_raw 77 result = [] 78 for item in result_raw: 79 if isinstance(item, str): 80 item = item.decode(self.cursor.server_encoding) 81 result.append(item) 82 83 return tuple(result) 84 85 def _decode_lines(self, result_raw): 86 """ 87 decode every line in a resultset. 88 """ 89 result = [] 90 for line in result_raw: 91 result.append(self._decode_results(line)) 92 return tuple(result) 93 94 def fetchone(self): 95 result_raw = self.cursor.fetchone() 96 return self._decode_results(result_raw) 97 98 def fetchall(self): 99 result_raw = self.cursor.fetchall() 100 return self._decode_lines(result_raw) 101 102 def fetchmany(self, *args): 103 result_raw = self.cursor.fetchmany(*args) 104 return self._decode_lines(result_raw) 105 return result_raw 106 65 107 66 108 class DatabaseFeatures(BaseDatabaseFeatures): 67 109 autoindexes_primary_keys = False … … 193 235 self.connection.set_character_set('utf8') 194 236 else: 195 237 cursor = self.connection.cursor() 196 return cursor 238 239 cursor.execute("SHOW VARIABLES LIKE %s;", ("character_set_server",)) 240 cursor.server_encoding = cursor.fetchone()[1] 241 242 return MysqlUnicodeWrapper(cursor) 197 243 198 244 def make_debug_cursor(self, cursor): 199 return BaseDatabaseWrapper.make_debug_cursor(self, MysqlDebugWrapper(cursor)) 245 cursor = MysqlDebugWrapper(cursor) 246 cursor = MysqlUnicodeWrapper(cursor) 247 return BaseDatabaseWrapper.make_debug_cursor(self, cursor) 200 248 201 249 def _rollback(self): 202 250 try: