Ticket #2635: django-mysql.2.patch
File django-mysql.2.patch, 6.0 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 … … 17 20 18 21 DatabaseError = Database.DatabaseError 19 22 23 # MySQLdb-1.2.1 supports the Python boolean type, and only uses datetime 24 # module for time-related columns; older versions could have used mx.DateTime 25 # or strings if there were no datetime module. However, MySQLdb still returns 26 # TIME columns as timedelta -- they are more like timedelta in terms of actual 27 # behavior as they are signed and include days -- and Django expects time, so 28 # we still need to override that. 20 29 django_conversions = conversions.copy() 21 30 django_conversions.update({ 22 types.BooleanType: util.rev_typecast_boolean,23 FIELD_TYPE.DATETIME: util.typecast_timestamp,24 FIELD_TYPE.DATE: util.typecast_date,25 31 FIELD_TYPE.TIME: util.typecast_time, 26 32 }) 27 33 … … 31 37 # http://dev.mysql.com/doc/refman/5.0/en/news.html . 32 38 server_version_re = re.compile(r'(\d{1,2})\.(\d{1,2})\.(\d{1,2})') 33 39 34 # This is an extra debug layer over MySQL queries, to display warnings. 35 # It's only used when DEBUG=True. 36 class MysqlDebugWrapper: 37 def __init__(self, cursor): 38 self.cursor = cursor 40 # MySQLdb-1.2.1 and newer automatically makes use of SHOW WARNINGS on 41 # MySQL-4.1 and newer, so the MysqlDebugWrapper is unnecessary. Since the 42 # point is to raise Warnings as exceptions, this can be done with the Python 43 # warning module, and this is setup when the connection is created, and the 44 # standard util.CursorDebugWrapper can be used. Also, using sql_mode 45 # TRADITIONAL will automatically cause most warnings to be treated as errors. 39 46 40 def execute(self, sql, params=()):41 try:42 return self.cursor.execute(sql, params)43 except Database.Warning, w:44 self.cursor.execute("SHOW WARNINGS")45 raise Database.Warning, "%s: %s" % (w, self.cursor.fetchall())46 47 def executemany(self, sql, param_list):48 try:49 return self.cursor.executemany(sql, param_list)50 except Database.Warning, w:51 self.cursor.execute("SHOW WARNINGS")52 raise Database.Warning, "%s: %s" % (w, self.cursor.fetchall())53 54 def __getattr__(self, attr):55 if self.__dict__.has_key(attr):56 return self.__dict__[attr]57 else:58 return getattr(self.cursor, attr)59 60 47 try: 61 48 # Only exists in Python 2.4+ 62 49 from threading import local … … 83 70 84 71 def cursor(self): 85 72 from django.conf import settings 73 from warnings import filterwarnings 86 74 if not self._valid_connection(): 87 75 kwargs = { 88 'user': settings.DATABASE_USER,89 'db': settings.DATABASE_NAME,90 'passwd': settings.DATABASE_PASSWORD,91 76 'conv': django_conversions, 92 77 } 78 if settings.DATABASE_USER: 79 kwargs['user'] = settings.DATABASE_USER 80 if settings.DATABASE_NAME: 81 kwargs['db'] = settings.DATABASE_NAME 82 if settings.DATABASE_PASSWORD: 83 kwargs['passwd'] = settings.DATABASE_PASSWORD 93 84 if settings.DATABASE_HOST.startswith('/'): 94 85 kwargs['unix_socket'] = settings.DATABASE_HOST 95 el se:86 elif settings.DATABASE_HOST: 96 87 kwargs['host'] = settings.DATABASE_HOST 97 88 if settings.DATABASE_PORT: 98 89 kwargs['port'] = int(settings.DATABASE_PORT) 99 90 kwargs.update(self.options) 100 91 self.connection = Database.connect(**kwargs) 101 92 cursor = self.connection.cursor() 102 if self.connection.get_server_info() >= '4.1':103 cursor.execute("SET NAMES 'utf8'")104 93 else: 105 94 cursor = self.connection.cursor() 106 95 if settings.DEBUG: 107 return util.CursorDebugWrapper(MysqlDebugWrapper(cursor), self) 96 filterwarnings("error", category=Database.Warning) 97 return util.CursorDebugWrapper(cursor, self) 108 98 return cursor 109 99 110 100 def _commit(self): -
django/db/backends/mysql/client.py
3 3 4 4 def runshell(): 5 5 args = [''] 6 args += ["--user=%s" % settings.DATABASE_USER] 7 if settings.DATABASE_PASSWORD: 8 args += ["--password=%s" % settings.DATABASE_PASSWORD] 9 if settings.DATABASE_HOST: 10 args += ["--host=%s" % settings.DATABASE_HOST] 11 if settings.DATABASE_PORT: 12 args += ["--port=%s" % settings.DATABASE_PORT] 13 args += [settings.DATABASE_NAME] 6 db = settings.DATABASE_OPTIONS.get('db', settings.DATABASE_NAME) 7 user = settings.DATABASE_OPTIONS.get('user', settings.DATABASE_USER) 8 passwd = settings.DATABASE_OPTIONS.get('passwd', settings.DATABASE_PASSWORD) 9 host = settings.DATABASE_OPTIONS.get('host', settings.DATABASE_HOST) 10 port = settings.DATABASE_OPTIONS.get('port', settings.DATABASE_PORT) 11 defaults_file = settings.DATABASE_OPTIONS.get('read_default_file') 12 # Seems to be no good way to set sql_mode with CLI 13 14 if defaults_file: 15 args += ["--defaults-file=%s" % defaults_file] 16 if user: 17 args += ["--user=%s" % user] 18 if passwd: 19 args += ["--password=%s" % passwd] 20 if host: 21 args += ["--host=%s" % host] 22 if port: 23 args += ["--port=%s" % port] 24 if db: 25 args += [db] 26 14 27 os.execvp('mysql', args)