Ticket #2635: django-mysql.2.patch

File django-mysql.2.patch, 6.0 KB (added by Andy Dustman <farcepest@…>, 18 years ago)

Found a small bug in the last patch dealing with Django's overloading of DATABASE_HOST

  • django/db/backends/mysql/base.py

     
    1010except ImportError, e:
    1111    from django.core.exceptions import ImproperlyConfigured
    1212    raise ImproperlyConfigured, "Error loading MySQLdb module: %s" % e
     13if 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
    1316from MySQLdb.converters import conversions
    1417from MySQLdb.constants import FIELD_TYPE
    1518import types
     
    1720
    1821DatabaseError = Database.DatabaseError
    1922
     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.
    2029django_conversions = conversions.copy()
    2130django_conversions.update({
    22     types.BooleanType: util.rev_typecast_boolean,
    23     FIELD_TYPE.DATETIME: util.typecast_timestamp,
    24     FIELD_TYPE.DATE: util.typecast_date,
    2531    FIELD_TYPE.TIME: util.typecast_time,
    2632})
    2733
     
    3137# http://dev.mysql.com/doc/refman/5.0/en/news.html .
    3238server_version_re = re.compile(r'(\d{1,2})\.(\d{1,2})\.(\d{1,2})')
    3339
    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.
    3946
    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 
    6047try:
    6148    # Only exists in Python 2.4+
    6249    from threading import local
     
    8370
    8471    def cursor(self):
    8572        from django.conf import settings
     73        from warnings import filterwarnings
    8674        if not self._valid_connection():
    8775            kwargs = {
    88                 'user': settings.DATABASE_USER,
    89                 'db': settings.DATABASE_NAME,
    90                 'passwd': settings.DATABASE_PASSWORD,
    9176                'conv': django_conversions,
    9277            }
     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
    9384            if settings.DATABASE_HOST.startswith('/'):
    9485                kwargs['unix_socket'] = settings.DATABASE_HOST
    95             else:
     86            elif settings.DATABASE_HOST:
    9687                kwargs['host'] = settings.DATABASE_HOST
    9788            if settings.DATABASE_PORT:
    9889                kwargs['port'] = int(settings.DATABASE_PORT)
    9990            kwargs.update(self.options)
    10091            self.connection = Database.connect(**kwargs)
    10192            cursor = self.connection.cursor()
    102             if self.connection.get_server_info() >= '4.1':
    103                 cursor.execute("SET NAMES 'utf8'")
    10493        else:
    10594            cursor = self.connection.cursor()
    10695        if settings.DEBUG:
    107             return util.CursorDebugWrapper(MysqlDebugWrapper(cursor), self)
     96            filterwarnings("error", category=Database.Warning)
     97            return util.CursorDebugWrapper(cursor, self)
    10898        return cursor
    10999
    110100    def _commit(self):
  • django/db/backends/mysql/client.py

     
    33
    44def runshell():
    55    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
    1427    os.execvp('mysql', args)
Back to Top