3
3
from django.core import signals
4
4
from django.core.exceptions import ImproperlyConfigured
5
5
from django.utils.functional import curry
6
from django.utils.importlib import import_module
7
8
__all__ = ('backend', 'connection', 'DatabaseError', 'IntegrityError')
9
10
if not settings.DATABASE_ENGINE:
10
11
settings.DATABASE_ENGINE = 'dummy'
13
# Most of the time, the database backend will be one of the official
14
# backends that ships with Django, so look there first.
15
_import_path = 'django.db.backends.'
16
backend = __import__('%s%s.base' % (_import_path, settings.DATABASE_ENGINE), {}, {}, [''])
17
except ImportError, e:
18
# If the import failed, we might be looking for a database backend
19
# distributed external to Django. So we'll try that next.
13
def load_backend(backend_name):
22
backend = __import__('%s.base' % settings.DATABASE_ENGINE, {}, {}, [''])
23
except ImportError, e_user:
24
# The database backend wasn't found. Display a helpful error message
25
# listing all possible (built-in) database backends.
26
backend_dir = os.path.join(__path__[0], 'backends')
15
# Most of the time, the database backend will be one of the official
16
# backends that ships with Django, so look there first.
17
return import_module('.base', 'django.db.backends.%s' % backend_name)
18
except ImportError, e:
19
# If the import failed, we might be looking for a database backend
20
# distributed external to Django. So we'll try that next.
28
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')]
29
except EnvironmentError:
30
available_backends = []
31
available_backends.sort()
32
if settings.DATABASE_ENGINE not in available_backends:
33
raise ImproperlyConfigured, "%r isn't an available database backend. Available options are: %s\nError was: %s" % \
34
(settings.DATABASE_ENGINE, ", ".join(map(repr, available_backends)), e_user)
36
raise # If there's some other error, this must be an error in Django itself.
38
# Convenient aliases for backend bits.
39
connection = backend.DatabaseWrapper(**settings.DATABASE_OPTIONS)
22
return import_module('.base', backend_name)
23
except ImportError, e_user:
24
# The database backend wasn't found. Display a helpful error message
25
# listing all possible (built-in) database backends.
26
backend_dir = os.path.join(__path__[0], 'backends')
28
available_backends = [f for f in os.listdir(backend_dir)
29
if os.path.isdir(os.path.join(backend_dir, f))
30
and not f.startswith('.')]
31
except EnvironmentError:
32
available_backends = []
33
available_backends.sort()
34
if backend_name not in available_backends:
35
error_msg = "%r isn't an available database backend. Available options are: %s\nError was: %s" % \
36
(backend_name, ", ".join(map(repr, available_backends)), e_user)
37
raise ImproperlyConfigured(error_msg)
39
raise # If there's some other error, this must be an error in Django itself.
41
backend = load_backend(settings.DATABASE_ENGINE)
43
# `connection`, `DatabaseError` and `IntegrityError` are convenient aliases
46
# DatabaseWrapper.__init__() takes a dictionary, not a settings module, so
47
# we manually create the dictionary from the settings, passing only the
48
# settings that the database backends care about. Note that TIME_ZONE is used
49
# by the PostgreSQL backends.
50
connection = backend.DatabaseWrapper({
51
'DATABASE_HOST': settings.DATABASE_HOST,
52
'DATABASE_NAME': settings.DATABASE_NAME,
53
'DATABASE_OPTIONS': settings.DATABASE_OPTIONS,
54
'DATABASE_PASSWORD': settings.DATABASE_PASSWORD,
55
'DATABASE_PORT': settings.DATABASE_PORT,
56
'DATABASE_USER': settings.DATABASE_USER,
57
'TIME_ZONE': settings.TIME_ZONE,
40
59
DatabaseError = backend.DatabaseError
41
60
IntegrityError = backend.IntegrityError