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'
|
|
|
|
|
|
|
|
try:
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
# Most of the time, the database backend will be one of the official
|
2007-09-16 03:25:20 +08:00
|
|
|
# backends that ships with Django, so look there first.
|
|
|
|
_import_path = 'django.db.backends.'
|
|
|
|
backend = __import__('%s%s.base' % (_import_path, settings.DATABASE_ENGINE), {}, {}, [''])
|
2006-05-02 09:31:56 +08:00
|
|
|
except ImportError, e:
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
# If the import failed, we might be looking for a database backend
|
2007-09-16 03:25:20 +08:00
|
|
|
# distributed external to Django. So we'll try that next.
|
|
|
|
try:
|
|
|
|
_import_path = ''
|
|
|
|
backend = __import__('%s.base' % settings.DATABASE_ENGINE, {}, {}, [''])
|
|
|
|
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')
|
2008-08-18 01:32:31 +08:00
|
|
|
try:
|
2009-03-01 15:12:38 +08:00
|
|
|
available_backends = [f for f in os.listdir(backend_dir)
|
|
|
|
if os.path.isdir(os.path.join(backend_dir, f))]
|
2008-08-18 01:32:31 +08:00
|
|
|
except EnvironmentError:
|
|
|
|
available_backends = []
|
2007-09-16 03:25:20 +08:00
|
|
|
available_backends.sort()
|
|
|
|
if settings.DATABASE_ENGINE not in available_backends:
|
2008-08-11 20:11:25 +08:00
|
|
|
raise ImproperlyConfigured, "%r isn't an available database backend. Available options are: %s\nError was: %s" % \
|
2008-08-11 21:11:25 +08:00
|
|
|
(settings.DATABASE_ENGINE, ", ".join(map(repr, available_backends)), e_user)
|
2007-09-16 03:25:20 +08:00
|
|
|
else:
|
|
|
|
raise # If there's some other error, this must be an error in Django itself.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2007-09-16 03:25:20 +08:00
|
|
|
# Convenient aliases for backend bits.
|
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
|
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)
|