|
1 import os |
|
2 from django.conf import settings |
|
3 from django.core import signals |
|
4 from django.core.exceptions import ImproperlyConfigured |
|
5 from django.dispatch import dispatcher |
|
6 from django.utils.functional import curry |
|
7 |
|
8 __all__ = ('backend', 'connection', 'DatabaseError', 'IntegrityError') |
|
9 |
|
10 if not settings.DATABASE_ENGINE: |
|
11 settings.DATABASE_ENGINE = 'dummy' |
|
12 |
|
13 try: |
|
14 # Most of the time, the database backend will be one of the official |
|
15 # backends that ships with Django, so look there first. |
|
16 _import_path = 'django.db.backends.' |
|
17 backend = __import__('%s%s.base' % (_import_path, settings.DATABASE_ENGINE), {}, {}, ['']) |
|
18 creation = __import__('%s%s.creation' % (_import_path, settings.DATABASE_ENGINE), {}, {}, ['']) |
|
19 except ImportError, e: |
|
20 # If the import failed, we might be looking for a database backend |
|
21 # distributed external to Django. So we'll try that next. |
|
22 try: |
|
23 _import_path = '' |
|
24 backend = __import__('%s.base' % settings.DATABASE_ENGINE, {}, {}, ['']) |
|
25 creation = __import__('%s.creation' % settings.DATABASE_ENGINE, {}, {}, ['']) |
|
26 except ImportError, e_user: |
|
27 # The database backend wasn't found. Display a helpful error message |
|
28 # listing all possible (built-in) database backends. |
|
29 backend_dir = os.path.join(__path__[0], 'backends') |
|
30 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')] |
|
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" % \ |
|
34 (settings.DATABASE_ENGINE, ", ".join(map(repr, available_backends))) |
|
35 else: |
|
36 raise # If there's some other error, this must be an error in Django itself. |
|
37 |
|
38 def _import_database_module(import_path='', module_name=''): |
|
39 """Lazily import a database module when requested.""" |
|
40 return __import__('%s%s.%s' % (import_path, settings.DATABASE_ENGINE, module_name), {}, {}, ['']) |
|
41 |
|
42 # We don't want to import the introspect module unless someone asks for it, so |
|
43 # lazily load it on demmand. |
|
44 get_introspection_module = curry(_import_database_module, _import_path, 'introspection') |
|
45 |
|
46 def get_creation_module(): |
|
47 return creation |
|
48 |
|
49 # We want runshell() to work the same way, but we have to treat it a |
|
50 # little differently (since it just runs instead of returning a module like |
|
51 # the above) and wrap the lazily-loaded runshell() method. |
|
52 runshell = lambda: _import_database_module(_import_path, "client").runshell() |
|
53 |
|
54 # Convenient aliases for backend bits. |
|
55 connection = backend.DatabaseWrapper(**settings.DATABASE_OPTIONS) |
|
56 DatabaseError = backend.DatabaseError |
|
57 IntegrityError = backend.IntegrityError |
|
58 |
|
59 # Register an event that closes the database connection |
|
60 # when a Django request is finished. |
|
61 dispatcher.connect(connection.close, signal=signals.request_finished) |
|
62 |
|
63 # Register an event that resets connection.queries |
|
64 # when a Django request is started. |
|
65 def reset_queries(): |
|
66 connection.queries = [] |
|
67 dispatcher.connect(reset_queries, signal=signals.request_started) |
|
68 |
|
69 # Register an event that rolls back the connection |
|
70 # when a Django request has an exception. |
|
71 def _rollback_on_exception(): |
|
72 from django.db import transaction |
|
73 transaction.rollback_unless_managed() |
|
74 dispatcher.connect(_rollback_on_exception, signal=signals.got_request_exception) |