Django

Code

Changeset 4724

Show
Ignore:
Timestamp:
03/14/07 07:08:19 (1 year ago)
Author:
mtredinnick
Message:

Fixed #2635 -- Added improved MySQL backend support from Andy Dustman. Also
added database.txt documentation; currently only describing Django-related
features of MySQL versions.

As of this commit, there are four known test failures due to (a) no transaction
support with MyISAM storage engine and (b) MySQLdb automatically creating
decimal types for Django's "float" field.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/backends/mysql/base.py

    r4691 r4724  
    1111    from django.core.exceptions import ImproperlyConfigured 
    1212    raise ImproperlyConfigured, "Error loading MySQLdb module: %s" % e 
     13if Database.version_info < (1,2,1,'final',2): 
     14    raise ImportError, "MySQLdb-1.2.1p2 or newer is required; you have %s" % MySQLdb.__version__ 
     15 
    1316from MySQLdb.converters import conversions 
    1417from MySQLdb.constants import FIELD_TYPE 
     
    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}) 
     
    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 
    39  
    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) 
     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. 
    5946 
    6047try: 
     
    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: 
     
    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 
  • django/trunk/django/db/backends/mysql/client.py

    r4265 r4724  
    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) 
  • django/trunk/django/db/models/fields/__init__.py

    r4717 r4724  
    827827            # MySQL will throw a warning if microseconds are given, because it 
    828828            # doesn't support microseconds. 
    829             if settings.DATABASE_ENGINE == 'mysql'
     829            if settings.DATABASE_ENGINE == 'mysql' and hasattr(value, 'microsecond')
    830830                value = value.replace(microsecond=0) 
    831831            value = str(value) 
  • django/trunk/tests/regressiontests/serializers_regress/tests.py

    r4719 r4724  
    5555    instance = klass.objects.get(id=pk) 
    5656    testcase.assertEqual(data, instance.data,  
    57                          "Objects with PK=%d not equal; expected '%s', got '%s'" % (pk,data,instance.data)) 
     57                         "Objects with PK=%d not equal; expected '%s' (%s), got '%s' (%s)" % (pk,data, type(data), instance.data, type(instance.data))) 
    5858 
    5959def fk_compare(testcase, pk, klass, data):