web/lib/django/db/__init__.py
changeset 0 0d40e90630ef
child 29 cc9b7e14412b
equal deleted inserted replaced
-1:000000000000 0:0d40e90630ef
       
     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.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:
       
    11     settings.DATABASE_ENGINE = 'dummy'
       
    12 
       
    13 def load_backend(backend_name):
       
    14     try:
       
    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.
       
    21         try:
       
    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')
       
    27             try:
       
    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)
       
    38             else:
       
    39                 raise # If there's some other error, this must be an error in Django itself.
       
    40 
       
    41 backend = load_backend(settings.DATABASE_ENGINE)
       
    42 
       
    43 # `connection`, `DatabaseError` and `IntegrityError` are convenient aliases
       
    44 # for backend bits.
       
    45 
       
    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,
       
    58 })
       
    59 DatabaseError = backend.DatabaseError
       
    60 IntegrityError = backend.IntegrityError
       
    61 
       
    62 # Register an event that closes the database connection
       
    63 # when a Django request is finished.
       
    64 def close_connection(**kwargs):
       
    65     connection.close()
       
    66 signals.request_finished.connect(close_connection)
       
    67 
       
    68 # Register an event that resets connection.queries
       
    69 # when a Django request is started.
       
    70 def reset_queries(**kwargs):
       
    71     connection.queries = []
       
    72 signals.request_started.connect(reset_queries)
       
    73 
       
    74 # Register an event that rolls back the connection
       
    75 # when a Django request has an exception.
       
    76 def _rollback_on_exception(**kwargs):
       
    77     from django.db import transaction
       
    78     try:
       
    79         transaction.rollback_unless_managed()
       
    80     except DatabaseError:
       
    81         pass
       
    82 signals.got_request_exception.connect(_rollback_on_exception)