Ticket #5106: backend-refactoring.patch

File backend-refactoring.patch, 15.5 KB (added by (removed), 17 years ago)
  • django/db/backends/__init__.py

    === modified file 'django/db/backends/__init__.py'
     
     1try:
     2    # Only exists in Python 2.4+
     3    from threading import local
     4except ImportError:
     5    # Import copy of _thread_local.py from Python 2.4
     6    from django.utils._threading_local import local
     7
     8
     9class BaseDatabaseWrapper(local):
     10
     11    def __init__(self, **kwargs):
     12        self.connection = None
     13        self.queries = []
     14        self.options = kwargs
     15
     16    def _commit(self):
     17        if self.connection is not None:
     18            return self.connection.commit()
     19
     20    def _rollback(self):
     21        if self.connection is not None:
     22            return self.connection.rollback()
     23
     24    def close(self):
     25        if self.connection is not None:
     26            self.connection.close()
     27            self.connection = None
     28
     29    def cursor(self):
     30        from django.conf import settings
     31        cursor = self._cursor(settings)
     32        if settings.DEBUG:
     33            return self.make_debug_cursor(cursor)
     34        return cursor
     35
     36    def make_debug_cursor(self, cursor):
     37        from django.db.backends import util
     38        return util.CursorDebugWrapper(cursor, self)
  • django/db/backends/ado_mssql/base.py

    === modified file 'django/db/backends/ado_mssql/base.py'
     
    4848    return res
    4949Database.convertVariantToPython = variantToPython
    5050
    51 try:
    52     # Only exists in Python 2.4+
    53     from threading import local
    54 except ImportError:
    55     # Import copy of _thread_local.py from Python 2.4
    56     from django.utils._threading_local import local
    57 
    58 class DatabaseWrapper(local):
    59     def __init__(self, **kwargs):
    60         self.connection = None
    61         self.queries = []
    62 
    63     def cursor(self):
    64         from django.conf import settings
     51from django.db.backends import BaseDatabaseWrapper
     52
     53class DatabaseWrapper(BaseDatabaseWrapper):
     54
     55    def _cursor(self, settings):
    6556        if self.connection is None:
    6657            if settings.DATABASE_NAME == '' or settings.DATABASE_USER == '':
    6758                from django.core.exceptions import ImproperlyConfigured
     
    7162            # TODO: Handle DATABASE_PORT.
    7263            conn_string = "PROVIDER=SQLOLEDB;DATA SOURCE=%s;UID=%s;PWD=%s;DATABASE=%s" % (settings.DATABASE_HOST, settings.DATABASE_USER, settings.DATABASE_PASSWORD, settings.DATABASE_NAME)
    7364            self.connection = Database.connect(conn_string)
    74         cursor = self.connection.cursor()
    75         if settings.DEBUG:
    76             return util.CursorDebugWrapper(cursor, self)
    77         return cursor
    78 
    79     def _commit(self):
    80         if self.connection is not None:
    81             return self.connection.commit()
    82 
    83     def _rollback(self):
    84         if self.connection is not None:
    85             return self.connection.rollback()
    86 
    87     def close(self):
    88         if self.connection is not None:
    89             self.connection.close()
    90             self.connection = None
     65        return self.connection.cursor()
     66
    9167
    9268allows_group_by_ordinal = True
    9369allows_unique_and_pk = True
  • django/db/backends/mysql/base.py

    === modified file 'django/db/backends/mysql/base.py'
     
    5353# standard util.CursorDebugWrapper can be used. Also, using sql_mode
    5454# TRADITIONAL will automatically cause most warnings to be treated as errors.
    5555
    56 try:
    57     # Only exists in Python 2.4+
    58     from threading import local
    59 except ImportError:
    60     # Import copy of _thread_local.py from Python 2.4
    61     from django.utils._threading_local import local
    62 
    63 class DatabaseWrapper(local):
     56
     57from django.db.backends import BaseDatabaseWrapper
     58
     59class DatabaseWrapper(BaseDatabaseWrapper):
     60
    6461    def __init__(self, **kwargs):
    65         self.connection = None
    66         self.queries = []
     62        super(DatabaseWrapper, self).__init__(**kwargs)
    6763        self.server_version = None
    68         self.options = kwargs
    6964
    7065    def _valid_connection(self):
    7166        if self.connection is not None:
     
    7772                self.connection = None
    7873        return False
    7974
    80     def cursor(self):
    81         from django.conf import settings
     75    def _cursor(self, settings):
    8276        from warnings import filterwarnings
    8377        if not self._valid_connection():
    8478            kwargs = {
     
    10094                kwargs['port'] = int(settings.DATABASE_PORT)
    10195            kwargs.update(self.options)
    10296            self.connection = Database.connect(**kwargs)
    103             cursor = self.connection.cursor()
    104         else:
    105             cursor = self.connection.cursor()
     97        cursor = self.connection.cursor()
    10698        if settings.DEBUG:
    10799            filterwarnings("error", category=Database.Warning)
    108             return util.CursorDebugWrapper(cursor, self)
    109100        return cursor
    110101
    111     def _commit(self):
    112         if self.connection is not None:
    113             self.connection.commit()
    114 
    115102    def _rollback(self):
    116         if self.connection is not None:
    117             try:
    118                 self.connection.rollback()
    119             except Database.NotSupportedError:
    120                 pass
    121 
    122     def close(self):
    123         if self.connection is not None:
    124             self.connection.close()
    125             self.connection = None
     103        try:
     104            BaseDatabaseWrapper._rollback(self)
     105        except Database.NotSupportedError:
     106            pass
    126107
    127108    def get_server_version(self):
    128109        if not self.server_version:
  • django/db/backends/mysql_old/base.py

    === modified file 'django/db/backends/mysql_old/base.py'
     
    6363        else:
    6464            return getattr(self.cursor, attr)
    6565
    66 try:
    67     # Only exists in Python 2.4+
    68     from threading import local
    69 except ImportError:
    70     # Import copy of _thread_local.py from Python 2.4
    71     from django.utils._threading_local import local
     66from django.db.backends import BaseDatabaseWrapper
    7267
    73 class DatabaseWrapper(local):
     68class DatabaseWrapper(BaseDatabaseWrapper):
    7469    def __init__(self, **kwargs):
    75         self.connection = None
    76         self.queries = []
     70        super(DatabaseWrapper, self).__init__(**kwargs)
    7771        self.server_version = None
    78         self.options = kwargs
    7972
    8073    def _valid_connection(self):
    8174        if self.connection is not None:
     
    8780                self.connection = None
    8881        return False
    8982
    90     def cursor(self):
    91         from django.conf import settings
     83    def _cursor(self, settings):
    9284        if not self._valid_connection():
    9385            kwargs = {
    9486                # Note: use_unicode intentonally not set to work around some
     
    119111                    self.connection.set_character_set('utf8')
    120112        else:
    121113            cursor = self.connection.cursor()
    122         if settings.DEBUG:
    123             return util.CursorDebugWrapper(MysqlDebugWrapper(cursor), self)
     114       
    124115        return cursor
    125116
    126     def _commit(self):
    127         if self.connection is not None:
    128             self.connection.commit()
     117    def make_debug_cursor(self, cursor):
     118        return BaseDatabaseWrapper.make_debug_cursor(self, MysqlDebugWrapper(cursor))
    129119
    130120    def _rollback(self):
    131         if self.connection is not None:
    132             try:
    133                 self.connection.rollback()
    134             except Database.NotSupportedError:
    135                 pass
    136 
    137     def close(self):
    138         if self.connection is not None:
    139             self.connection.close()
    140             self.connection = None
     121        try:
     122            BaseDatabaseWrapper._rollback(self)
     123        except Database.NotSupportedError:
     124            pass
    141125
    142126    def get_server_version(self):
    143127        if not self.server_version:
  • django/db/backends/oracle/base.py

    === modified file 'django/db/backends/oracle/base.py'
     
    2323DatabaseError = Database.Error
    2424IntegrityError = Database.IntegrityError
    2525
    26 try:
    27     # Only exists in Python 2.4+
    28     from threading import local
    29 except ImportError:
    30     # Import copy of _thread_local.py from Python 2.4
    31     from django.utils._threading_local import local
     26from django.db.backends import BaseDatabaseWrapper
    3227
    33 class DatabaseWrapper(local):
    34     def __init__(self, **kwargs):
    35         self.connection = None
    36         self.queries = []
    37         self.options = kwargs
     28class DatabaseWrapper(BaseDatabaseWrapper):
    3829
    3930    def _valid_connection(self):
    4031        return self.connection is not None
    4132
    42     def cursor(self):
     33    def _cursor(self, settings):
    4334        if not self._valid_connection():
    4435            if len(settings.DATABASE_HOST.strip()) == 0:
    4536                settings.DATABASE_HOST = 'localhost'
     
    5546        # Set oracle date to ansi date format.
    5647        cursor.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD'")
    5748        cursor.execute("ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'")
    58         if settings.DEBUG:
    59             return util.CursorDebugWrapper(cursor, self)
    6049        return cursor
    6150
    62     def _commit(self):
    63         if self.connection is not None:
    64             return self.connection.commit()
    65 
    66     def _rollback(self):
    67         if self.connection is not None:
    68             return self.connection.rollback()
    69 
    70     def close(self):
    71         if self.connection is not None:
    72             self.connection.close()
    73             self.connection = None
    7451
    7552allows_group_by_ordinal = False
    7653allows_unique_and_pk = False        # Suppress UNIQUE/PK for Oracle (ORA-02259)
  • django/db/backends/postgresql/base.py

    === modified file 'django/db/backends/postgresql/base.py'
     
    1515DatabaseError = Database.DatabaseError
    1616IntegrityError = Database.IntegrityError
    1717
    18 try:
    19     # Only exists in Python 2.4+
    20     from threading import local
    21 except ImportError:
    22     # Import copy of _thread_local.py from Python 2.4
    23     from django.utils._threading_local import local
     18from django.db.backends import BaseDatabaseWrapper
    2419
    2520class UnicodeCursorWrapper(object):
    2621    """
     
    6459
    6560postgres_version = None
    6661
    67 class DatabaseWrapper(local):
    68     def __init__(self, **kwargs):
    69         self.connection = None
    70         self.queries = []
    71         self.options = kwargs
     62class DatabaseWrapper(BaseDatabaseWrapper):
    7263
    73     def cursor(self):
    74         from django.conf import settings
     64    def _cursor(self, settings):
    7565        set_tz = False
    7666        if self.connection is None:
    7767            set_tz = True
     
    9888        if not postgres_version:
    9989            cursor.execute("SELECT version()")
    10090            postgres_version = [int(val) for val in cursor.fetchone()[0].split()[1].split('.')]
    101         if settings.DEBUG:
    102             return util.CursorDebugWrapper(cursor, self)
    10391        return cursor
    10492
    105     def _commit(self):
    106         if self.connection is not None:
    107             return self.connection.commit()
    108 
    109     def _rollback(self):
    110         if self.connection is not None:
    111             return self.connection.rollback()
    112 
    113     def close(self):
    114         if self.connection is not None:
    115             self.connection.close()
    116             self.connection = None
    117 
    11893allows_group_by_ordinal = True
    11994allows_unique_and_pk = True
    12095autoindexes_primary_keys = True
  • django/db/backends/postgresql_psycopg2/base.py

    === modified file 'django/db/backends/postgresql_psycopg2/base.py'
     
    55"""
    66
    77from django.db.backends import util
     8from django.db.backends import BaseDatabaseWrapper
     9
    810try:
    911    import psycopg2 as Database
    1012    import psycopg2.extensions
     
    1517DatabaseError = Database.DatabaseError
    1618IntegrityError = Database.IntegrityError
    1719
    18 try:
    19     # Only exists in Python 2.4+
    20     from threading import local
    21 except ImportError:
    22     # Import copy of _thread_local.py from Python 2.4
    23     from django.utils._threading_local import local
    24 
    2520psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
    2621
    2722postgres_version = None
    2823
    29 class DatabaseWrapper(local):
    30     def __init__(self, **kwargs):
    31         self.connection = None
    32         self.queries = []
    33         self.options = kwargs
     24class DatabaseWrapper(BaseDatabaseWrapper):
    3425
    35     def cursor(self):
    36         from django.conf import settings
     26    def _cursor(self, settings):
    3727        set_tz = False
    3828        if self.connection is None:
    3929            set_tz = True
     
    6050        if not postgres_version:
    6151            cursor.execute("SELECT version()")
    6252            postgres_version = [int(val) for val in cursor.fetchone()[0].split()[1].split('.')]
    63         if settings.DEBUG:
    64             return util.CursorDebugWrapper(cursor, self)
    6553        return cursor
    6654
    67     def _commit(self):
    68         if self.connection is not None:
    69             return self.connection.commit()
    70 
    71     def _rollback(self):
    72         if self.connection is not None:
    73             return self.connection.rollback()
    74 
    75     def close(self):
    76         if self.connection is not None:
    77             self.connection.close()
    78             self.connection = None
    7955
    8056allows_group_by_ordinal = True
    8157allows_unique_and_pk = True
  • django/db/backends/sqlite3/base.py

    === modified file 'django/db/backends/sqlite3/base.py'
     
    3434Database.register_converter("decimal", util.typecast_decimal)
    3535Database.register_adapter(decimal.Decimal, util.rev_typecast_decimal)
    3636
    37 try:
    38     # Only exists in Python 2.4+
    39     from threading import local
    40 except ImportError:
    41     # Import copy of _thread_local.py from Python 2.4
    42     from django.utils._threading_local import local
    43 
    44 class DatabaseWrapper(local):
    45     def __init__(self, **kwargs):
    46         self.connection = None
    47         self.queries = []
    48         self.options = kwargs
    49 
    50     def cursor(self):
    51         from django.conf import settings
     37from django.db.backends import BaseDatabaseWrapper
     38
     39class DatabaseWrapper(BaseDatabaseWrapper):
     40
     41    def _cursor(self, settings):
    5242        if self.connection is None:
    5343            kwargs = {
    5444                'database': settings.DATABASE_NAME,
     
    6050            self.connection.create_function("django_extract", 2, _sqlite_extract)
    6151            self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
    6252            self.connection.create_function("regexp", 2, _sqlite_regexp)
    63         cursor = self.connection.cursor(factory=SQLiteCursorWrapper)
    64         if settings.DEBUG:
    65             return util.CursorDebugWrapper(cursor, self)
    66         else:
    67             return cursor
    68 
    69     def _commit(self):
    70         if self.connection is not None:
    71             self.connection.commit()
    72 
    73     def _rollback(self):
    74         if self.connection is not None:
    75             self.connection.rollback()
     53        return self.connection.cursor(factory=SQLiteCursorWrapper)
    7654
    7755    def close(self):
    7856        from django.conf import settings
    7957        # If database is in memory, closing the connection destroys the
    8058        # database.  To prevent accidental data loss, ignore close requests on
    8159        # an in-memory db.
    82         if self.connection is not None and settings.DATABASE_NAME != ":memory:":
    83             self.connection.close()
    84             self.connection = None
     60        if settings.DATABASE_NAME != ':memory:':
     61            BaseDatabaseWrapper.close(self)
     62
    8563
    8664class SQLiteCursorWrapper(Database.Cursor):
    8765    """
Back to Top