Django

Code

Changeset 5949

Show
Ignore:
Timestamp:
08/19/07 16:30:57 (1 year ago)
Author:
adrian
Message:

Refactored all database backends to inherit from a common base class to remove quite a bit of duplicated code. Thanks for the patch, Brian Harring. Refs #5106

Files:

Legend:

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

    r5519 r5949  
    55""" 
    66 
    7 from django.db.backends import util 
     7from django.db.backends import BaseDatabaseWrapper, util 
    88try: 
    99    import adodbapi as Database 
     
    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 
     51class DatabaseWrapper(BaseDatabaseWrapper): 
     52    def _cursor(self, settings): 
    6553        if self.connection is None: 
    6654            if settings.DATABASE_NAME == '' or settings.DATABASE_USER == '': 
    6755                from django.core.exceptions import ImproperlyConfigured 
    68                 raise ImproperlyConfigured, "You need to specify both DATABASE_NAME and DATABASE_USER in your Django settings file." 
     56                raise ImproperlyConfigured("You need to specify both DATABASE_NAME and DATABASE_USER in your Django settings file.") 
    6957            if not settings.DATABASE_HOST: 
    7058                settings.DATABASE_HOST = "127.0.0.1" 
     
    7260            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) 
    7361            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 
     62        return self.connection.cursor() 
    9163 
    9264allows_group_by_ordinal = True 
  • django/trunk/django/db/backends/__init__.py

    r4265 r5949  
     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 
     8class BaseDatabaseWrapper(local): 
     9    def __init__(self, **kwargs): 
     10        self.connection = None 
     11        self.queries = [] 
     12        self.options = kwargs 
     13 
     14    def _commit(self): 
     15        if self.connection is not None: 
     16            return self.connection.commit() 
     17 
     18    def _rollback(self): 
     19        if self.connection is not None: 
     20            return self.connection.rollback() 
     21 
     22    def close(self): 
     23        if self.connection is not None: 
     24            self.connection.close() 
     25            self.connection = None 
     26 
     27    def cursor(self): 
     28        from django.conf import settings 
     29        cursor = self._cursor(settings) 
     30        if settings.DEBUG: 
     31            return self.make_debug_cursor(cursor) 
     32        return cursor 
     33 
     34    def make_debug_cursor(self, cursor): 
     35        from django.db.backends import util 
     36        return util.CursorDebugWrapper(cursor, self) 
  • django/trunk/django/db/backends/mysql/base.py

    r5609 r5949  
    55""" 
    66 
    7 from django.db.backends import util 
     7from django.db.backends import BaseDatabaseWrapper, util 
    88try: 
    99    import MySQLdb as Database 
     
    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): 
     56class DatabaseWrapper(BaseDatabaseWrapper): 
    6457    def __init__(self, **kwargs): 
    65         self.connection = None 
    66         self.queries = [] 
     58        super(DatabaseWrapper, self).__init__(**kwargs) 
    6759        self.server_version = None 
    68         self.options = kwargs 
    6960 
    7061    def _valid_connection(self): 
     
    7869        return False 
    7970 
    80     def cursor(self): 
    81         from django.conf import settings 
     71    def _cursor(self, settings): 
    8272        from warnings import filterwarnings 
    8373        if not self._valid_connection(): 
     
    10191            kwargs.update(self.options) 
    10292            self.connection = Database.connect(**kwargs) 
    103             cursor = self.connection.cursor() 
    104         else: 
    105             cursor = self.connection.cursor() 
     93        cursor = self.connection.cursor() 
    10694        if settings.DEBUG: 
    10795            filterwarnings("error", category=Database.Warning) 
    108             return util.CursorDebugWrapper(cursor, self) 
    10996        return cursor 
    11097 
    111     def _commit(self): 
    112         if self.connection is not None: 
    113             self.connection.commit() 
    114  
    11598    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 
     99        try: 
     100            BaseDatabaseWrapper._rollback(self) 
     101        except Database.NotSupportedError: 
     102            pass 
    126103 
    127104    def get_server_version(self): 
  • django/trunk/django/db/backends/mysql_old/base.py

    r5876 r5949  
    55""" 
    66 
    7 from django.db.backends import util 
     7from django.db.backends import BaseDatabaseWrapper, util 
    88from django.utils.encoding import force_unicode 
    99try: 
     
    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 
    72  
    73 class DatabaseWrapper(local): 
     66class DatabaseWrapper(BaseDatabaseWrapper): 
    7467    def __init__(self, **kwargs): 
    75         self.connection = None 
    76         self.queries = [] 
     68        super(DatabaseWrapper, self).__init__(**kwargs) 
    7769        self.server_version = None 
    78         self.options = kwargs 
    7970 
    8071    def _valid_connection(self): 
     
    8879        return False 
    8980 
    90     def cursor(self): 
    91         from django.conf import settings 
     81    def _cursor(self, settings): 
    9282        if not self._valid_connection(): 
    9383            kwargs = { 
     
    120110        else: 
    121111            cursor = self.connection.cursor() 
    122         if settings.DEBUG: 
    123             return util.CursorDebugWrapper(MysqlDebugWrapper(cursor), self) 
    124112        return cursor 
    125113 
    126     def _commit(self): 
    127         if self.connection is not None: 
    128             self.connection.commit() 
     114    def make_debug_cursor(self, cursor): 
     115        return BaseDatabaseWrapper.make_debug_cursor(self, MysqlDebugWrapper(cursor)) 
    129116 
    130117    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 
     118        try: 
     119            BaseDatabaseWrapper._rollback(self) 
     120        except Database.NotSupportedError: 
     121            pass 
    141122 
    142123    def get_server_version(self): 
  • django/trunk/django/db/backends/oracle/base.py

    r5943 r5949  
    55""" 
    66 
    7 from django.conf import settings 
    8 from django.db.backends import util 
     7from django.db.backends import BaseDatabaseWrapper, util 
    98from django.utils.datastructures import SortedDict 
    109from django.utils.encoding import smart_str, force_unicode 
     
    2019    raise ImproperlyConfigured, "Error loading cx_Oracle module: %s" % e 
    2120 
    22  
    2321DatabaseError = Database.Error 
    2422IntegrityError = Database.IntegrityError 
    2523 
    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 
    32  
    33 class DatabaseWrapper(local): 
    34     def __init__(self, **kwargs): 
    35         self.connection = None 
    36         self.queries = [] 
    37         self.options = kwargs 
    38  
     24class DatabaseWrapper(BaseDatabaseWrapper): 
    3925    def _valid_connection(self): 
    4026        return self.connection is not None 
    4127 
    42     def cursor(self): 
     28    def _cursor(self, settings): 
    4329        if not self._valid_connection(): 
    4430            if len(settings.DATABASE_HOST.strip()) == 0: 
     
    5642        cursor.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD'") 
    5743        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) 
    6044        return cursor 
    61  
    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 
    7445 
    7546allows_group_by_ordinal = False 
  • django/trunk/django/db/backends/postgresql/base.py

    r5834 r5949  
    66 
    77from django.utils.encoding import smart_str, smart_unicode 
    8 from django.db.backends import util 
     8from django.db.backends import BaseDatabaseWrapper, util 
    99try: 
    1010    import psycopg as Database 
     
    1515DatabaseError = Database.DatabaseError 
    1616IntegrityError = Database.IntegrityError 
    17  
    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 
    2417 
    2518class UnicodeCursorWrapper(object): 
     
    6558postgres_version = None 
    6659 
    67 class DatabaseWrapper(local): 
    68     def __init__(self, **kwargs): 
    69         self.connection = None 
    70         self.queries = [] 
    71         self.options = kwargs 
    72  
    73     def cursor(self): 
    74         from django.conf import settings 
     60class DatabaseWrapper(BaseDatabaseWrapper): 
     61    def _cursor(self, settings): 
    7562        set_tz = False 
    7663        if self.connection is None: 
     
    9986            cursor.execute("SELECT version()") 
    10087            postgres_version = [int(val) for val in cursor.fetchone()[0].split()[1].split('.')] 
    101         if settings.DEBUG: 
    102             return util.CursorDebugWrapper(cursor, self) 
    10388        return cursor 
    104  
    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 
    11789 
    11890allows_group_by_ordinal = True 
  • django/trunk/django/db/backends/postgresql_psycopg2/base.py

    r5609 r5949  
    55""" 
    66 
    7 from django.db.backends import util 
     7from django.db.backends import BaseDatabaseWrapper, util 
    88try: 
    99    import psycopg2 as Database 
     
    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 
    24  
    2518psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) 
    2619 
    2720postgres_version = None 
    2821 
    29 class DatabaseWrapper(local): 
    30     def __init__(self, **kwargs): 
    31         self.connection = None 
    32         self.queries = [] 
    33         self.options = kwargs 
    34  
    35     def cursor(self): 
    36         from django.conf import settings 
     22class DatabaseWrapper(BaseDatabaseWrapper): 
     23    def _cursor(self, settings): 
    3724        set_tz = False 
    3825        if self.connection is None: 
     
    6148            cursor.execute("SELECT version()") 
    6249            postgres_version = [int(val) for val in cursor.fetchone()[0].split()[1].split('.')] 
    63         if settings.DEBUG: 
    64             return util.CursorDebugWrapper(cursor, self) 
    6550        return cursor 
    66  
    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 
    7951 
    8052allows_group_by_ordinal = True 
  • django/trunk/django/db/backends/sqlite3/base.py

    r5609 r5949  
    33""" 
    44 
    5 from django.db.backends import util 
     5from django.db.backends import BaseDatabaseWrapper, util 
    66try: 
    77    try: 
     
    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 
     37class DatabaseWrapper(BaseDatabaseWrapper): 
     38    def _cursor(self, settings): 
    5239        if self.connection is None: 
    5340            kwargs = { 
     
    6148            self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc) 
    6249            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() 
     50        return self.connection.cursor(factory=SQLiteCursorWrapper) 
    7651 
    7752    def close(self): 
    7853        from django.conf import settings 
    7954        # If database is in memory, closing the connection destroys the 
    80         # database. To prevent accidental data loss, ignore close requests on 
     55        # database. To prevent accidental data loss, ignore close requests on 
    8156        # an in-memory db. 
    82         if self.connection is not None and settings.DATABASE_NAME != ":memory:": 
    83             self.connection.close() 
    84             self.connection = None 
     57        if settings.DATABASE_NAME != ":memory:": 
     58            BaseDatabaseWrapper.close(self) 
    8559 
    8660class SQLiteCursorWrapper(Database.Cursor):