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

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5949 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2007-08-19 21:30:57 +00:00
parent 77a9b0cb1d
commit 7c41b19c8a
8 changed files with 76 additions and 221 deletions

View File

@ -0,0 +1,36 @@
try:
# Only exists in Python 2.4+
from threading import local
except ImportError:
# Import copy of _thread_local.py from Python 2.4
from django.utils._threading_local import local
class BaseDatabaseWrapper(local):
def __init__(self, **kwargs):
self.connection = None
self.queries = []
self.options = kwargs
def _commit(self):
if self.connection is not None:
return self.connection.commit()
def _rollback(self):
if self.connection is not None:
return self.connection.rollback()
def close(self):
if self.connection is not None:
self.connection.close()
self.connection = None
def cursor(self):
from django.conf import settings
cursor = self._cursor(settings)
if settings.DEBUG:
return self.make_debug_cursor(cursor)
return cursor
def make_debug_cursor(self, cursor):
from django.db.backends import util
return util.CursorDebugWrapper(cursor, self)

View File

@ -4,7 +4,7 @@ ADO MSSQL database backend for Django.
Requires adodbapi 2.0.1: http://adodbapi.sourceforge.net/ Requires adodbapi 2.0.1: http://adodbapi.sourceforge.net/
""" """
from django.db.backends import util from django.db.backends import BaseDatabaseWrapper, util
try: try:
import adodbapi as Database import adodbapi as Database
except ImportError, e: except ImportError, e:
@ -48,46 +48,18 @@ def variantToPython(variant, adType):
return res return res
Database.convertVariantToPython = variantToPython Database.convertVariantToPython = variantToPython
try: class DatabaseWrapper(BaseDatabaseWrapper):
# Only exists in Python 2.4+ def _cursor(self, settings):
from threading import local
except ImportError:
# Import copy of _thread_local.py from Python 2.4
from django.utils._threading_local import local
class DatabaseWrapper(local):
def __init__(self, **kwargs):
self.connection = None
self.queries = []
def cursor(self):
from django.conf import settings
if self.connection is None: if self.connection is None:
if settings.DATABASE_NAME == '' or settings.DATABASE_USER == '': if settings.DATABASE_NAME == '' or settings.DATABASE_USER == '':
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
raise ImproperlyConfigured, "You need to specify both DATABASE_NAME and DATABASE_USER in your Django settings file." raise ImproperlyConfigured("You need to specify both DATABASE_NAME and DATABASE_USER in your Django settings file.")
if not settings.DATABASE_HOST: if not settings.DATABASE_HOST:
settings.DATABASE_HOST = "127.0.0.1" settings.DATABASE_HOST = "127.0.0.1"
# TODO: Handle DATABASE_PORT. # TODO: Handle DATABASE_PORT.
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) 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)
self.connection = Database.connect(conn_string) self.connection = Database.connect(conn_string)
cursor = self.connection.cursor() return self.connection.cursor()
if settings.DEBUG:
return util.CursorDebugWrapper(cursor, self)
return cursor
def _commit(self):
if self.connection is not None:
return self.connection.commit()
def _rollback(self):
if self.connection is not None:
return self.connection.rollback()
def close(self):
if self.connection is not None:
self.connection.close()
self.connection = None
allows_group_by_ordinal = True allows_group_by_ordinal = True
allows_unique_and_pk = True allows_unique_and_pk = True

View File

@ -4,7 +4,7 @@ MySQL database backend for Django.
Requires MySQLdb: http://sourceforge.net/projects/mysql-python Requires MySQLdb: http://sourceforge.net/projects/mysql-python
""" """
from django.db.backends import util from django.db.backends import BaseDatabaseWrapper, util
try: try:
import MySQLdb as Database import MySQLdb as Database
except ImportError, e: except ImportError, e:
@ -53,19 +53,10 @@ server_version_re = re.compile(r'(\d{1,2})\.(\d{1,2})\.(\d{1,2})')
# standard util.CursorDebugWrapper can be used. Also, using sql_mode # standard util.CursorDebugWrapper can be used. Also, using sql_mode
# TRADITIONAL will automatically cause most warnings to be treated as errors. # TRADITIONAL will automatically cause most warnings to be treated as errors.
try: class DatabaseWrapper(BaseDatabaseWrapper):
# Only exists in Python 2.4+
from threading import local
except ImportError:
# Import copy of _thread_local.py from Python 2.4
from django.utils._threading_local import local
class DatabaseWrapper(local):
def __init__(self, **kwargs): def __init__(self, **kwargs):
self.connection = None super(DatabaseWrapper, self).__init__(**kwargs)
self.queries = []
self.server_version = None self.server_version = None
self.options = kwargs
def _valid_connection(self): def _valid_connection(self):
if self.connection is not None: if self.connection is not None:
@ -77,8 +68,7 @@ class DatabaseWrapper(local):
self.connection = None self.connection = None
return False return False
def cursor(self): def _cursor(self, settings):
from django.conf import settings
from warnings import filterwarnings from warnings import filterwarnings
if not self._valid_connection(): if not self._valid_connection():
kwargs = { kwargs = {
@ -100,29 +90,16 @@ class DatabaseWrapper(local):
kwargs['port'] = int(settings.DATABASE_PORT) kwargs['port'] = int(settings.DATABASE_PORT)
kwargs.update(self.options) kwargs.update(self.options)
self.connection = Database.connect(**kwargs) self.connection = Database.connect(**kwargs)
cursor = self.connection.cursor() cursor = self.connection.cursor()
else:
cursor = self.connection.cursor()
if settings.DEBUG: if settings.DEBUG:
filterwarnings("error", category=Database.Warning) filterwarnings("error", category=Database.Warning)
return util.CursorDebugWrapper(cursor, self)
return cursor return cursor
def _commit(self):
if self.connection is not None:
self.connection.commit()
def _rollback(self): def _rollback(self):
if self.connection is not None: try:
try: BaseDatabaseWrapper._rollback(self)
self.connection.rollback() except Database.NotSupportedError:
except Database.NotSupportedError: pass
pass
def close(self):
if self.connection is not None:
self.connection.close()
self.connection = None
def get_server_version(self): def get_server_version(self):
if not self.server_version: if not self.server_version:

View File

@ -4,7 +4,7 @@ MySQL database backend for Django.
Requires MySQLdb: http://sourceforge.net/projects/mysql-python Requires MySQLdb: http://sourceforge.net/projects/mysql-python
""" """
from django.db.backends import util from django.db.backends import BaseDatabaseWrapper, util
from django.utils.encoding import force_unicode from django.utils.encoding import force_unicode
try: try:
import MySQLdb as Database import MySQLdb as Database
@ -63,19 +63,10 @@ class MysqlDebugWrapper:
else: else:
return getattr(self.cursor, attr) return getattr(self.cursor, attr)
try: class DatabaseWrapper(BaseDatabaseWrapper):
# Only exists in Python 2.4+
from threading import local
except ImportError:
# Import copy of _thread_local.py from Python 2.4
from django.utils._threading_local import local
class DatabaseWrapper(local):
def __init__(self, **kwargs): def __init__(self, **kwargs):
self.connection = None super(DatabaseWrapper, self).__init__(**kwargs)
self.queries = []
self.server_version = None self.server_version = None
self.options = kwargs
def _valid_connection(self): def _valid_connection(self):
if self.connection is not None: if self.connection is not None:
@ -87,8 +78,7 @@ class DatabaseWrapper(local):
self.connection = None self.connection = None
return False return False
def cursor(self): def _cursor(self, settings):
from django.conf import settings
if not self._valid_connection(): if not self._valid_connection():
kwargs = { kwargs = {
# Note: use_unicode intentonally not set to work around some # Note: use_unicode intentonally not set to work around some
@ -119,25 +109,16 @@ class DatabaseWrapper(local):
self.connection.set_character_set('utf8') self.connection.set_character_set('utf8')
else: else:
cursor = self.connection.cursor() cursor = self.connection.cursor()
if settings.DEBUG:
return util.CursorDebugWrapper(MysqlDebugWrapper(cursor), self)
return cursor return cursor
def _commit(self): def make_debug_cursor(self, cursor):
if self.connection is not None: return BaseDatabaseWrapper.make_debug_cursor(self, MysqlDebugWrapper(cursor))
self.connection.commit()
def _rollback(self): def _rollback(self):
if self.connection is not None: try:
try: BaseDatabaseWrapper._rollback(self)
self.connection.rollback() except Database.NotSupportedError:
except Database.NotSupportedError: pass
pass
def close(self):
if self.connection is not None:
self.connection.close()
self.connection = None
def get_server_version(self): def get_server_version(self):
if not self.server_version: if not self.server_version:

View File

@ -4,8 +4,7 @@ Oracle database backend for Django.
Requires cx_Oracle: http://www.python.net/crew/atuining/cx_Oracle/ Requires cx_Oracle: http://www.python.net/crew/atuining/cx_Oracle/
""" """
from django.conf import settings from django.db.backends import BaseDatabaseWrapper, util
from django.db.backends import util
from django.utils.datastructures import SortedDict from django.utils.datastructures import SortedDict
from django.utils.encoding import smart_str, force_unicode from django.utils.encoding import smart_str, force_unicode
import datetime import datetime
@ -19,27 +18,14 @@ except ImportError, e:
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
raise ImproperlyConfigured, "Error loading cx_Oracle module: %s" % e raise ImproperlyConfigured, "Error loading cx_Oracle module: %s" % e
DatabaseError = Database.Error DatabaseError = Database.Error
IntegrityError = Database.IntegrityError IntegrityError = Database.IntegrityError
try: class DatabaseWrapper(BaseDatabaseWrapper):
# Only exists in Python 2.4+
from threading import local
except ImportError:
# Import copy of _thread_local.py from Python 2.4
from django.utils._threading_local import local
class DatabaseWrapper(local):
def __init__(self, **kwargs):
self.connection = None
self.queries = []
self.options = kwargs
def _valid_connection(self): def _valid_connection(self):
return self.connection is not None return self.connection is not None
def cursor(self): def _cursor(self, settings):
if not self._valid_connection(): if not self._valid_connection():
if len(settings.DATABASE_HOST.strip()) == 0: if len(settings.DATABASE_HOST.strip()) == 0:
settings.DATABASE_HOST = 'localhost' settings.DATABASE_HOST = 'localhost'
@ -55,23 +41,8 @@ class DatabaseWrapper(local):
# Set oracle date to ansi date format. # Set oracle date to ansi date format.
cursor.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD'") cursor.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD'")
cursor.execute("ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'") cursor.execute("ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'")
if settings.DEBUG:
return util.CursorDebugWrapper(cursor, self)
return cursor return cursor
def _commit(self):
if self.connection is not None:
return self.connection.commit()
def _rollback(self):
if self.connection is not None:
return self.connection.rollback()
def close(self):
if self.connection is not None:
self.connection.close()
self.connection = None
allows_group_by_ordinal = False allows_group_by_ordinal = False
allows_unique_and_pk = False # Suppress UNIQUE/PK for Oracle (ORA-02259) allows_unique_and_pk = False # Suppress UNIQUE/PK for Oracle (ORA-02259)
autoindexes_primary_keys = True autoindexes_primary_keys = True

View File

@ -5,7 +5,7 @@ Requires psycopg 1: http://initd.org/projects/psycopg1
""" """
from django.utils.encoding import smart_str, smart_unicode from django.utils.encoding import smart_str, smart_unicode
from django.db.backends import util from django.db.backends import BaseDatabaseWrapper, util
try: try:
import psycopg as Database import psycopg as Database
except ImportError, e: except ImportError, e:
@ -15,13 +15,6 @@ except ImportError, e:
DatabaseError = Database.DatabaseError DatabaseError = Database.DatabaseError
IntegrityError = Database.IntegrityError IntegrityError = Database.IntegrityError
try:
# Only exists in Python 2.4+
from threading import local
except ImportError:
# Import copy of _thread_local.py from Python 2.4
from django.utils._threading_local import local
class UnicodeCursorWrapper(object): class UnicodeCursorWrapper(object):
""" """
A thin wrapper around psycopg cursors that allows them to accept Unicode A thin wrapper around psycopg cursors that allows them to accept Unicode
@ -64,14 +57,8 @@ class UnicodeCursorWrapper(object):
postgres_version = None postgres_version = None
class DatabaseWrapper(local): class DatabaseWrapper(BaseDatabaseWrapper):
def __init__(self, **kwargs): def _cursor(self, settings):
self.connection = None
self.queries = []
self.options = kwargs
def cursor(self):
from django.conf import settings
set_tz = False set_tz = False
if self.connection is None: if self.connection is None:
set_tz = True set_tz = True
@ -98,23 +85,8 @@ class DatabaseWrapper(local):
if not postgres_version: if not postgres_version:
cursor.execute("SELECT version()") cursor.execute("SELECT version()")
postgres_version = [int(val) for val in cursor.fetchone()[0].split()[1].split('.')] postgres_version = [int(val) for val in cursor.fetchone()[0].split()[1].split('.')]
if settings.DEBUG:
return util.CursorDebugWrapper(cursor, self)
return cursor return cursor
def _commit(self):
if self.connection is not None:
return self.connection.commit()
def _rollback(self):
if self.connection is not None:
return self.connection.rollback()
def close(self):
if self.connection is not None:
self.connection.close()
self.connection = None
allows_group_by_ordinal = True allows_group_by_ordinal = True
allows_unique_and_pk = True allows_unique_and_pk = True
autoindexes_primary_keys = True autoindexes_primary_keys = True

View File

@ -4,7 +4,7 @@ PostgreSQL database backend for Django.
Requires psycopg 2: http://initd.org/projects/psycopg2 Requires psycopg 2: http://initd.org/projects/psycopg2
""" """
from django.db.backends import util from django.db.backends import BaseDatabaseWrapper, util
try: try:
import psycopg2 as Database import psycopg2 as Database
import psycopg2.extensions import psycopg2.extensions
@ -15,25 +15,12 @@ except ImportError, e:
DatabaseError = Database.DatabaseError DatabaseError = Database.DatabaseError
IntegrityError = Database.IntegrityError IntegrityError = Database.IntegrityError
try:
# Only exists in Python 2.4+
from threading import local
except ImportError:
# Import copy of _thread_local.py from Python 2.4
from django.utils._threading_local import local
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
postgres_version = None postgres_version = None
class DatabaseWrapper(local): class DatabaseWrapper(BaseDatabaseWrapper):
def __init__(self, **kwargs): def _cursor(self, settings):
self.connection = None
self.queries = []
self.options = kwargs
def cursor(self):
from django.conf import settings
set_tz = False set_tz = False
if self.connection is None: if self.connection is None:
set_tz = True set_tz = True
@ -60,23 +47,8 @@ class DatabaseWrapper(local):
if not postgres_version: if not postgres_version:
cursor.execute("SELECT version()") cursor.execute("SELECT version()")
postgres_version = [int(val) for val in cursor.fetchone()[0].split()[1].split('.')] postgres_version = [int(val) for val in cursor.fetchone()[0].split()[1].split('.')]
if settings.DEBUG:
return util.CursorDebugWrapper(cursor, self)
return cursor return cursor
def _commit(self):
if self.connection is not None:
return self.connection.commit()
def _rollback(self):
if self.connection is not None:
return self.connection.rollback()
def close(self):
if self.connection is not None:
self.connection.close()
self.connection = None
allows_group_by_ordinal = True allows_group_by_ordinal = True
allows_unique_and_pk = True allows_unique_and_pk = True
autoindexes_primary_keys = True autoindexes_primary_keys = True

View File

@ -2,7 +2,7 @@
SQLite3 backend for django. Requires pysqlite2 (http://pysqlite.org/). SQLite3 backend for django. Requires pysqlite2 (http://pysqlite.org/).
""" """
from django.db.backends import util from django.db.backends import BaseDatabaseWrapper, util
try: try:
try: try:
from sqlite3 import dbapi2 as Database from sqlite3 import dbapi2 as Database
@ -34,21 +34,8 @@ Database.register_converter("TIMESTAMP", util.typecast_timestamp)
Database.register_converter("decimal", util.typecast_decimal) Database.register_converter("decimal", util.typecast_decimal)
Database.register_adapter(decimal.Decimal, util.rev_typecast_decimal) Database.register_adapter(decimal.Decimal, util.rev_typecast_decimal)
try: class DatabaseWrapper(BaseDatabaseWrapper):
# Only exists in Python 2.4+ def _cursor(self, settings):
from threading import local
except ImportError:
# Import copy of _thread_local.py from Python 2.4
from django.utils._threading_local import local
class DatabaseWrapper(local):
def __init__(self, **kwargs):
self.connection = None
self.queries = []
self.options = kwargs
def cursor(self):
from django.conf import settings
if self.connection is None: if self.connection is None:
kwargs = { kwargs = {
'database': settings.DATABASE_NAME, 'database': settings.DATABASE_NAME,
@ -60,28 +47,15 @@ class DatabaseWrapper(local):
self.connection.create_function("django_extract", 2, _sqlite_extract) self.connection.create_function("django_extract", 2, _sqlite_extract)
self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc) self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
self.connection.create_function("regexp", 2, _sqlite_regexp) self.connection.create_function("regexp", 2, _sqlite_regexp)
cursor = self.connection.cursor(factory=SQLiteCursorWrapper) return self.connection.cursor(factory=SQLiteCursorWrapper)
if settings.DEBUG:
return util.CursorDebugWrapper(cursor, self)
else:
return cursor
def _commit(self):
if self.connection is not None:
self.connection.commit()
def _rollback(self):
if self.connection is not None:
self.connection.rollback()
def close(self): def close(self):
from django.conf import settings from django.conf import settings
# If database is in memory, closing the connection destroys the # If database is in memory, closing the connection destroys the
# database. To prevent accidental data loss, ignore close requests on # database. To prevent accidental data loss, ignore close requests on
# an in-memory db. # an in-memory db.
if self.connection is not None and settings.DATABASE_NAME != ":memory:": if settings.DATABASE_NAME != ":memory:":
self.connection.close() BaseDatabaseWrapper.close(self)
self.connection = None
class SQLiteCursorWrapper(Database.Cursor): class SQLiteCursorWrapper(Database.Cursor):
""" """