2007-09-16 03:25:20 +08:00
|
|
|
import os
|
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
|
|
|
|
from django.utils.functional import curry
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2007-04-25 18:18:56 +08:00
|
|
|
__all__ = ('backend', 'connection', 'DatabaseError', 'IntegrityError')
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
if not settings.DATABASE_ENGINE:
|
|
|
|
settings.DATABASE_ENGINE = 'dummy'
|
|
|
|
|
2009-03-13 12:41:45 +08:00
|
|
|
def load_backend(backend_name):
|
2007-09-16 03:25:20 +08:00
|
|
|
try:
|
2009-03-13 12:41:45 +08:00
|
|
|
# Most of the time, the database backend will be one of the official
|
|
|
|
# backends that ships with Django, so look there first.
|
|
|
|
return __import__('django.db.backends.%s.base' % backend_name, {}, {}, [''])
|
|
|
|
except ImportError, e:
|
|
|
|
# If the import failed, we might be looking for a database backend
|
|
|
|
# distributed external to Django. So we'll try that next.
|
2008-08-18 01:32:31 +08:00
|
|
|
try:
|
2009-03-13 12:41:45 +08:00
|
|
|
return __import__('%s.base' % backend_name, {}, {}, [''])
|
|
|
|
except ImportError, e_user:
|
|
|
|
# The database backend wasn't found. Display a helpful error message
|
|
|
|
# listing all possible (built-in) database backends.
|
|
|
|
backend_dir = os.path.join(__path__[0], 'backends')
|
|
|
|
try:
|
|
|
|
available_backends = [f for f in os.listdir(backend_dir)
|
2009-03-13 12:45:11 +08:00
|
|
|
if os.path.isdir(os.path.join(backend_dir, f))
|
|
|
|
and not f.startswith('.')]
|
2009-03-13 12:41:45 +08:00
|
|
|
except EnvironmentError:
|
|
|
|
available_backends = []
|
|
|
|
available_backends.sort()
|
|
|
|
if backend_name not in available_backends:
|
|
|
|
error_msg = "%r isn't an available database backend. Available options are: %s\nError was: %s" % \
|
|
|
|
(backend_name, ", ".join(map(repr, available_backends)), e_user)
|
|
|
|
raise ImproperlyConfigured(error_msg)
|
|
|
|
else:
|
|
|
|
raise # If there's some other error, this must be an error in Django itself.
|
|
|
|
|
|
|
|
backend = load_backend(settings.DATABASE_ENGINE)
|
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.
|
|
|
|
connection = backend.DatabaseWrapper({
|
|
|
|
'DATABASE_HOST': settings.DATABASE_HOST,
|
|
|
|
'DATABASE_NAME': settings.DATABASE_NAME,
|
|
|
|
'DATABASE_OPTIONS': settings.DATABASE_OPTIONS,
|
|
|
|
'DATABASE_PASSWORD': settings.DATABASE_PASSWORD,
|
|
|
|
'DATABASE_PORT': settings.DATABASE_PORT,
|
|
|
|
'DATABASE_USER': settings.DATABASE_USER,
|
|
|
|
'TIME_ZONE': settings.TIME_ZONE,
|
|
|
|
})
|
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):
|
2006-05-02 09:31:56 +08:00
|
|
|
connection.queries = []
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.request_started.connect(reset_queries)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Register an event that rolls back the connection
|
|
|
|
# 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
|
2008-08-16 23:58:30 +08:00
|
|
|
try:
|
|
|
|
transaction.rollback_unless_managed()
|
|
|
|
except DatabaseError:
|
|
|
|
pass
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.got_request_exception.connect(_rollback_on_exception)
|