Ticket #2635: django-mysql5.patch
File django-mysql5.patch, 4.7 KB (added by , 18 years ago) |
---|
-
django/db/backends/mysql/base.py
10 10 except ImportError, e: 11 11 from django.core.exceptions import ImproperlyConfigured 12 12 raise ImproperlyConfigured, "Error loading MySQLdb module: %s" % e 13 if Database.version_info < (1,2,1,'final',1): 14 raise ImportError, "MySQLdb-1.2.1 or newer is required; you have %s" % MySQLdb.__version__ 15 13 16 from MySQLdb.converters import conversions 14 17 from MySQLdb.constants import FIELD_TYPE 15 18 import types 16 19 17 20 DatabaseError = Database.DatabaseError 18 21 22 # MySQLdb-1.2.1 supports the Python boolean type, and only uses datetime 23 # module for time-related columns; older versions could have used mx.DateTime 24 # or strings if there were no datetime module. However, MySQLdb still 25 # returns TIME columns as timedelta -- they are more like timedelta in 26 # terms of actual behavior as they are signed and include days -- and Django 27 # expects time, so we still need to override that. 28 19 29 django_conversions = conversions.copy() 20 30 django_conversions.update({ 21 types.BooleanType: util.rev_typecast_boolean,22 FIELD_TYPE.DATETIME: util.typecast_timestamp,23 FIELD_TYPE.DATE: util.typecast_date,24 31 FIELD_TYPE.TIME: util.typecast_time, 25 })32 }) 26 33 27 # This is an extra debug layer over MySQL queries, to display warnings. 28 # It's only used when DEBUG=True. 29 class MysqlDebugWrapper: 30 def __init__(self, cursor): 31 self.cursor = cursor 34 # MySQLdb-1.2.1 and newer automatically makes use of SHOW WARNINGS 35 # on MySQL-4.1 and newer, so the MysqlDebugWrapper is unnecessary. 36 # Since the point is to raise Warnings as exceptions, this can be 37 # done with the Python warning module, and this is setup when the 38 # connection is created, and the standard util.CursorDebugWrapper 39 # can be used. 32 40 33 def execute(self, sql, params=()):34 try:35 return self.cursor.execute(sql, params)36 except Database.Warning, w:37 self.cursor.execute("SHOW WARNINGS")38 raise Database.Warning, "%s: %s" % (w, self.cursor.fetchall())39 40 def executemany(self, sql, param_list):41 try:42 return self.cursor.executemany(sql, param_list)43 except Database.Warning, w:44 self.cursor.execute("SHOW WARNINGS")45 raise Database.Warning, "%s: %s" % (w, self.cursor.fetchall())46 47 def __getattr__(self, attr):48 if self.__dict__.has_key(attr):49 return self.__dict__[attr]50 else:51 return getattr(self.cursor, attr)52 53 41 try: 54 42 # Only exists in Python 2.4+ 55 43 from threading import local … … 74 62 75 63 def cursor(self): 76 64 from django.conf import settings 65 from warnings import filterwarnings 77 66 if not self._valid_connection(): 78 67 kwargs = { 79 68 'user': settings.DATABASE_USER, 80 69 'db': settings.DATABASE_NAME, 81 70 'passwd': settings.DATABASE_PASSWORD, 82 71 'conv': django_conversions, 72 'use_unicode': True, 73 'charset': 'utf8', 74 'sql_mode': "ANSI,TRADITIONAL", 83 75 } 84 76 if settings.DATABASE_HOST.startswith('/'): 85 77 kwargs['unix_socket'] = settings.DATABASE_HOST … … 89 81 kwargs['port'] = int(settings.DATABASE_PORT) 90 82 self.connection = Database.connect(**kwargs) 91 83 cursor = self.connection.cursor() 92 if self.connection.get_server_info() >= '4.1':93 cursor.execute("SET NAMES 'utf8'")94 84 if settings.DEBUG: 95 return util.CursorDebugWrapper(MysqlDebugWrapper(cursor), self) 85 filterwarnings("error", category=Database.Warning) 86 return util.CursorDebugWrapper(cursor, self) 96 87 return cursor 97 88 98 89 def _commit(self): … … 113 104 supports_constraints = True 114 105 115 106 def quote_name(name): 116 if name.startswith( "`") and name.endswith("`"):107 if name.startswith('"') and name.endswith('"'): 117 108 return name # Quoting once is enough. 118 return "`%s`"% name109 return '"%s"' % name 119 110 120 111 dictfetchone = util.dictfetchone 121 112 dictfetchmany = util.dictfetchmany -
django/db/backends/mysql/introspection.py
92 92 FIELD_TYPE.LONG_BLOB: 'TextField', 93 93 FIELD_TYPE.VAR_STRING: 'CharField', 94 94 } 95 96 # these are only in MySQLdb-1.2.2b1 and newer 97 try: 98 DATA_TYPES_REVERSE[FIELD_TYPE.BIT] = 'CharField' 99 DATA_TYPES_REVERSE[FIELD_TYPE.VARCHAR] = 'CharField' 100 except AttributeError: 101 pass 102