2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core import signals
|
|
|
|
from django.dispatch import dispatcher
|
|
|
|
|
|
|
|
__all__ = ('backend', 'connection', 'DatabaseError')
|
|
|
|
|
|
|
|
if not settings.DATABASE_ENGINE:
|
|
|
|
settings.DATABASE_ENGINE = 'dummy'
|
|
|
|
|
|
|
|
try:
|
2006-10-31 04:50:27 +08:00
|
|
|
backend = __import__('django.db.backends.%s.base' % settings.DATABASE_ENGINE, {}, {}, [''])
|
2006-05-02 09:31:56 +08:00
|
|
|
except ImportError, e:
|
|
|
|
# The database backend wasn't found. Display a helpful error message
|
|
|
|
# listing all possible database backends.
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
import os
|
|
|
|
backend_dir = os.path.join(__path__[0], 'backends')
|
|
|
|
available_backends = [f for f in os.listdir(backend_dir) if not f.startswith('_') and not f.startswith('.') and not f.endswith('.py') and not f.endswith('.pyc')]
|
|
|
|
available_backends.sort()
|
2006-05-27 03:02:23 +08:00
|
|
|
if settings.DATABASE_ENGINE not in available_backends:
|
2006-11-13 13:02:45 +08:00
|
|
|
raise ImproperlyConfigured, "%r isn't an available database backend. Available options are: %s" % \
|
2006-05-27 03:02:23 +08:00
|
|
|
(settings.DATABASE_ENGINE, ", ".join(map(repr, available_backends)))
|
|
|
|
else:
|
|
|
|
raise # If there's some other error, this must be an error in Django itself.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-10-31 04:50:27 +08:00
|
|
|
get_introspection_module = lambda: __import__('django.db.backends.%s.introspection' % settings.DATABASE_ENGINE, {}, {}, [''])
|
|
|
|
get_creation_module = lambda: __import__('django.db.backends.%s.creation' % settings.DATABASE_ENGINE, {}, {}, [''])
|
|
|
|
runshell = lambda: __import__('django.db.backends.%s.client' % settings.DATABASE_ENGINE, {}, {}, ['']).runshell()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-11-07 13:17:38 +08:00
|
|
|
connection = backend.DatabaseWrapper(**settings.DATABASE_OPTIONS)
|
2006-05-02 09:31:56 +08:00
|
|
|
DatabaseError = backend.DatabaseError
|
|
|
|
|
|
|
|
# Register an event that closes the database connection
|
|
|
|
# when a Django request is finished.
|
|
|
|
dispatcher.connect(connection.close, signal=signals.request_finished)
|
|
|
|
|
|
|
|
# Register an event that resets connection.queries
|
|
|
|
# when a Django request is started.
|
|
|
|
def reset_queries():
|
|
|
|
connection.queries = []
|
|
|
|
dispatcher.connect(reset_queries, signal=signals.request_started)
|
|
|
|
|
|
|
|
# Register an event that rolls back the connection
|
|
|
|
# when a Django request has an exception.
|
|
|
|
def _rollback_on_exception():
|
|
|
|
from django.db import transaction
|
|
|
|
transaction.rollback_unless_managed()
|
|
|
|
dispatcher.connect(_rollback_on_exception, signal=signals.got_request_exception)
|