2009-12-22 23:18:51 +08:00
|
|
|
import os
|
2011-12-16 21:40:19 +08:00
|
|
|
from threading import local
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
from django.utils.importlib import import_module
|
2012-07-20 20:22:00 +08:00
|
|
|
from django.utils import six
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2010-12-09 02:31:57 +08:00
|
|
|
|
2010-01-22 22:30:06 +08:00
|
|
|
DEFAULT_DB_ALIAS = 'default'
|
|
|
|
|
2010-01-29 23:45:55 +08:00
|
|
|
# Define some exceptions that mirror the PEP249 interface.
|
|
|
|
# We will rethrow any backend-specific errors using these
|
|
|
|
# common wrappers
|
|
|
|
class DatabaseError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class IntegrityError(DatabaseError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def load_backend(backend_name):
|
2011-03-31 01:59:44 +08:00
|
|
|
# Look for a fully qualified database backend name
|
2009-12-22 23:18:51 +08:00
|
|
|
try:
|
2011-03-31 01:59:44 +08:00
|
|
|
return import_module('.base', backend_name)
|
2012-04-29 00:09:37 +08:00
|
|
|
except ImportError as e_user:
|
2011-03-31 01:59:44 +08:00
|
|
|
# The database backend wasn't found. Display a helpful error message
|
|
|
|
# listing all possible (built-in) database backends.
|
|
|
|
backend_dir = os.path.join(os.path.dirname(__file__), 'backends')
|
2009-12-22 23:18:51 +08:00
|
|
|
try:
|
2011-03-31 01:59:44 +08:00
|
|
|
available_backends = [f for f in os.listdir(backend_dir)
|
|
|
|
if os.path.isdir(os.path.join(backend_dir, f))
|
|
|
|
and not f.startswith('.')]
|
|
|
|
except EnvironmentError:
|
|
|
|
available_backends = []
|
2011-12-25 21:24:39 +08:00
|
|
|
full_notation = backend_name.startswith('django.db.backends.')
|
|
|
|
if full_notation:
|
2011-03-31 01:59:44 +08:00
|
|
|
backend_name = backend_name[19:] # See #15621.
|
|
|
|
if backend_name not in available_backends:
|
2011-12-25 21:24:39 +08:00
|
|
|
backend_reprs = map(repr, sorted(available_backends))
|
|
|
|
error_msg = ("%r isn't an available database backend.\n"
|
|
|
|
"Try using django.db.backends.XXX, where XXX "
|
|
|
|
"is one of:\n %s\nError was: %s" %
|
|
|
|
(backend_name, ", ".join(backend_reprs), e_user))
|
|
|
|
raise ImproperlyConfigured(error_msg)
|
|
|
|
elif not full_notation:
|
|
|
|
# user tried to use the old notation for the database backend
|
|
|
|
error_msg = ("%r isn't an available database backend.\n"
|
|
|
|
"Try using django.db.backends.%s instead.\n"
|
|
|
|
"Error was: %s" %
|
|
|
|
(backend_name, backend_name, e_user))
|
2011-03-31 01:59:44 +08:00
|
|
|
raise ImproperlyConfigured(error_msg)
|
|
|
|
else:
|
2011-12-25 21:24:39 +08:00
|
|
|
# If there's some other error, this must be an error in Django
|
|
|
|
raise
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2010-01-29 23:45:55 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
class ConnectionDoesNotExist(Exception):
|
|
|
|
pass
|
|
|
|
|
2010-01-29 23:45:55 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
class ConnectionHandler(object):
|
|
|
|
def __init__(self, databases):
|
|
|
|
self.databases = databases
|
2011-12-16 21:40:19 +08:00
|
|
|
self._connections = local()
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
def ensure_defaults(self, alias):
|
|
|
|
"""
|
|
|
|
Puts the defaults into the settings dictionary for a given connection
|
|
|
|
where no settings is provided.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
conn = self.databases[alias]
|
|
|
|
except KeyError:
|
|
|
|
raise ConnectionDoesNotExist("The connection %s doesn't exist" % alias)
|
2010-01-22 22:30:06 +08:00
|
|
|
|
2009-12-24 13:57:43 +08:00
|
|
|
conn.setdefault('ENGINE', 'django.db.backends.dummy')
|
2010-01-11 08:54:27 +08:00
|
|
|
if conn['ENGINE'] == 'django.db.backends.' or not conn['ENGINE']:
|
2009-12-24 13:57:43 +08:00
|
|
|
conn['ENGINE'] = 'django.db.backends.dummy'
|
2009-12-22 23:18:51 +08:00
|
|
|
conn.setdefault('OPTIONS', {})
|
2011-11-18 21:01:06 +08:00
|
|
|
conn.setdefault('TIME_ZONE', 'UTC' if settings.USE_TZ else settings.TIME_ZONE)
|
2011-03-31 01:59:44 +08:00
|
|
|
for setting in ['NAME', 'USER', 'PASSWORD', 'HOST', 'PORT']:
|
2009-12-22 23:18:51 +08:00
|
|
|
conn.setdefault(setting, '')
|
2011-03-31 01:59:44 +08:00
|
|
|
for setting in ['TEST_CHARSET', 'TEST_COLLATION', 'TEST_NAME', 'TEST_MIRROR']:
|
|
|
|
conn.setdefault(setting, None)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
def __getitem__(self, alias):
|
2011-12-16 21:40:19 +08:00
|
|
|
if hasattr(self._connections, alias):
|
|
|
|
return getattr(self._connections, alias)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
self.ensure_defaults(alias)
|
|
|
|
db = self.databases[alias]
|
|
|
|
backend = load_backend(db['ENGINE'])
|
|
|
|
conn = backend.DatabaseWrapper(db, alias)
|
2011-12-16 21:40:19 +08:00
|
|
|
setattr(self._connections, alias, conn)
|
2009-12-22 23:18:51 +08:00
|
|
|
return conn
|
|
|
|
|
2011-12-16 21:40:19 +08:00
|
|
|
def __setitem__(self, key, value):
|
|
|
|
setattr(self._connections, key, value)
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.databases)
|
|
|
|
|
|
|
|
def all(self):
|
|
|
|
return [self[alias] for alias in self]
|
2010-01-22 22:30:06 +08:00
|
|
|
|
2010-01-29 23:45:55 +08:00
|
|
|
|
2010-01-22 22:30:06 +08:00
|
|
|
class ConnectionRouter(object):
|
|
|
|
def __init__(self, routers):
|
|
|
|
self.routers = []
|
|
|
|
for r in routers:
|
2012-07-20 20:22:00 +08:00
|
|
|
if isinstance(r, six.string_types):
|
2010-01-27 15:57:18 +08:00
|
|
|
try:
|
|
|
|
module_name, klass_name = r.rsplit('.', 1)
|
|
|
|
module = import_module(module_name)
|
2012-04-29 00:09:37 +08:00
|
|
|
except ImportError as e:
|
2010-01-27 15:57:18 +08:00
|
|
|
raise ImproperlyConfigured('Error importing database router %s: "%s"' % (klass_name, e))
|
2010-01-28 13:55:44 +08:00
|
|
|
try:
|
2010-10-08 21:56:54 +08:00
|
|
|
router_class = getattr(module, klass_name)
|
2010-01-27 15:57:18 +08:00
|
|
|
except AttributeError:
|
2010-01-28 13:55:44 +08:00
|
|
|
raise ImproperlyConfigured('Module "%s" does not define a database router name "%s"' % (module, klass_name))
|
2010-10-08 21:56:54 +08:00
|
|
|
else:
|
|
|
|
router = router_class()
|
2010-01-22 22:30:06 +08:00
|
|
|
else:
|
|
|
|
router = r
|
|
|
|
self.routers.append(router)
|
|
|
|
|
|
|
|
def _router_func(action):
|
|
|
|
def _route_db(self, model, **hints):
|
|
|
|
chosen_db = None
|
|
|
|
for router in self.routers:
|
2010-01-27 15:56:53 +08:00
|
|
|
try:
|
2010-12-09 02:31:57 +08:00
|
|
|
method = getattr(router, action)
|
2010-01-27 15:56:53 +08:00
|
|
|
except AttributeError:
|
|
|
|
# If the router doesn't have a method, skip to the next one.
|
|
|
|
pass
|
2010-12-09 02:31:57 +08:00
|
|
|
else:
|
2010-12-09 02:37:00 +08:00
|
|
|
chosen_db = method(model, **hints)
|
2010-12-09 02:31:57 +08:00
|
|
|
if chosen_db:
|
|
|
|
return chosen_db
|
2010-01-22 22:30:06 +08:00
|
|
|
try:
|
|
|
|
return hints['instance']._state.db or DEFAULT_DB_ALIAS
|
|
|
|
except KeyError:
|
|
|
|
return DEFAULT_DB_ALIAS
|
|
|
|
return _route_db
|
|
|
|
|
|
|
|
db_for_read = _router_func('db_for_read')
|
|
|
|
db_for_write = _router_func('db_for_write')
|
|
|
|
|
|
|
|
def allow_relation(self, obj1, obj2, **hints):
|
|
|
|
for router in self.routers:
|
2010-01-27 15:56:53 +08:00
|
|
|
try:
|
2010-12-09 02:31:57 +08:00
|
|
|
method = router.allow_relation
|
2010-01-27 15:56:53 +08:00
|
|
|
except AttributeError:
|
|
|
|
# If the router doesn't have a method, skip to the next one.
|
|
|
|
pass
|
2010-12-09 02:31:57 +08:00
|
|
|
else:
|
|
|
|
allow = method(obj1, obj2, **hints)
|
|
|
|
if allow is not None:
|
|
|
|
return allow
|
2010-01-22 22:30:06 +08:00
|
|
|
return obj1._state.db == obj2._state.db
|
2010-01-25 20:23:30 +08:00
|
|
|
|
|
|
|
def allow_syncdb(self, db, model):
|
|
|
|
for router in self.routers:
|
2010-01-27 15:56:53 +08:00
|
|
|
try:
|
2010-12-09 02:31:57 +08:00
|
|
|
method = router.allow_syncdb
|
2010-01-27 15:56:53 +08:00
|
|
|
except AttributeError:
|
|
|
|
# If the router doesn't have a method, skip to the next one.
|
|
|
|
pass
|
2010-12-09 02:31:57 +08:00
|
|
|
else:
|
|
|
|
allow = method(db, model)
|
|
|
|
if allow is not None:
|
|
|
|
return allow
|
2010-01-25 20:23:30 +08:00
|
|
|
return True
|