2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core import signals
|
2007-09-16 03:25:20 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.db.utils import ConnectionHandler, load_backend
|
2007-09-16 03:25:20 +08:00
|
|
|
from django.utils.functional import curry
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
__all__ = ('backend', 'connection', 'connections', 'DatabaseError',
|
|
|
|
'IntegrityError', 'DEFAULT_DB_ALIAS')
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
DEFAULT_DB_ALIAS = 'default'
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
# For backwards compatibility - Port any old database settings over to
|
|
|
|
# the new values.
|
|
|
|
if not settings.DATABASES:
|
|
|
|
import warnings
|
|
|
|
warnings.warn(
|
|
|
|
"settings.DATABASE_* is deprecated; use settings.DATABASES instead.",
|
|
|
|
PendingDeprecationWarning
|
|
|
|
)
|
|
|
|
|
|
|
|
settings.DATABASES[DEFAULT_DB_ALIAS] = {
|
|
|
|
'ENGINE': settings.DATABASE_ENGINE,
|
|
|
|
'HOST': settings.DATABASE_HOST,
|
|
|
|
'NAME': settings.DATABASE_NAME,
|
|
|
|
'OPTIONS': settings.DATABASE_OPTIONS,
|
|
|
|
'PASSWORD': settings.DATABASE_PASSWORD,
|
|
|
|
'PORT': settings.DATABASE_PORT,
|
|
|
|
'USER': settings.DATABASE_USER,
|
|
|
|
'TEST_CHARSET': settings.TEST_DATABASE_CHARSET,
|
|
|
|
'TEST_COLLATION': settings.TEST_DATABASE_COLLATION,
|
|
|
|
'TEST_NAME': settings.TEST_DATABASE_NAME,
|
|
|
|
}
|
|
|
|
|
|
|
|
if DEFAULT_DB_ALIAS not in settings.DATABASES:
|
|
|
|
raise ImproperlyConfigured("You must default a '%s' database" % DEFAULT_DB_ALIAS)
|
|
|
|
|
|
|
|
for alias, database in settings.DATABASES.items():
|
|
|
|
if database['ENGINE'] in ("postgresql", "postgresql_psycopg2", "sqlite3", "mysql", "oracle"):
|
|
|
|
import warnings
|
|
|
|
if 'django.contrib.gis' in settings.INSTALLED_APPS:
|
|
|
|
warnings.warn(
|
|
|
|
"django.contrib.gis is now implemented as a full database backend. "
|
|
|
|
"Modify ENGINE in the %s database configuration to select "
|
|
|
|
"a backend from 'django.contrib.gis.db.backends'" % alias,
|
|
|
|
PendingDeprecationWarning
|
|
|
|
)
|
|
|
|
if database['ENGINE'] == 'postgresql_psycopg2':
|
|
|
|
full_engine = 'django.contrib.gis.db.backends.postgis'
|
|
|
|
elif database['ENGINE'] == 'sqlite3':
|
|
|
|
full_engine = 'django.contrib.gis.db.backends.spatialite'
|
2009-03-13 12:41:45 +08:00
|
|
|
else:
|
2009-12-22 23:18:51 +08:00
|
|
|
full_engine = 'django.contrib.gis.db.backends.%s' % database['ENGINE']
|
|
|
|
else:
|
|
|
|
warnings.warn(
|
|
|
|
"Short names for ENGINE in database configurations are deprecated. "
|
|
|
|
"Prepend %s.ENGINE with 'django.db.backends.'" % alias,
|
|
|
|
PendingDeprecationWarning
|
|
|
|
)
|
|
|
|
full_engine = "django.db.backends.%s" % database['ENGINE']
|
|
|
|
database['ENGINE'] = full_engine
|
|
|
|
|
|
|
|
connections = ConnectionHandler(settings.DATABASES)
|
2009-03-13 12:41:45 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-03-11 11:39:34 +08:00
|
|
|
# `connection`, `DatabaseError` and `IntegrityError` are convenient aliases
|
|
|
|
# for backend bits.
|
|
|
|
|
|
|
|
# DatabaseWrapper.__init__() takes a dictionary, not a settings module, so
|
|
|
|
# we manually create the dictionary from the settings, passing only the
|
|
|
|
# settings that the database backends care about. Note that TIME_ZONE is used
|
|
|
|
# by the PostgreSQL backends.
|
2009-12-22 23:18:51 +08:00
|
|
|
# we load all these up for backwards compatibility, you should use
|
|
|
|
# connections['default'] instead.
|
|
|
|
connection = connections[DEFAULT_DB_ALIAS]
|
|
|
|
backend = load_backend(connection.settings_dict['ENGINE'])
|
2006-05-02 09:31:56 +08:00
|
|
|
DatabaseError = backend.DatabaseError
|
2007-04-25 18:18:56 +08:00
|
|
|
IntegrityError = backend.IntegrityError
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Register an event that closes the database connection
|
|
|
|
# when a Django request is finished.
|
2008-08-06 23:32:46 +08:00
|
|
|
def close_connection(**kwargs):
|
|
|
|
connection.close()
|
|
|
|
signals.request_finished.connect(close_connection)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Register an event that resets connection.queries
|
|
|
|
# when a Django request is started.
|
2008-08-06 23:32:46 +08:00
|
|
|
def reset_queries(**kwargs):
|
2009-12-22 23:18:51 +08:00
|
|
|
for connection in connections.all():
|
|
|
|
connection.queries = []
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.request_started.connect(reset_queries)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
# Register an event that rolls back the connections
|
2006-05-02 09:31:56 +08:00
|
|
|
# when a Django request has an exception.
|
2008-08-06 23:32:46 +08:00
|
|
|
def _rollback_on_exception(**kwargs):
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.db import transaction
|
2009-12-22 23:18:51 +08:00
|
|
|
for conn in connections:
|
|
|
|
try:
|
|
|
|
transaction.rollback_unless_managed(using=conn)
|
|
|
|
except DatabaseError:
|
|
|
|
pass
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.got_request_exception.connect(_rollback_on_exception)
|