summaryrefslogtreecommitdiff
path: root/django/db/utils.py
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2009-12-22 15:18:51 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2009-12-22 15:18:51 +0000
commitff60c5f9de3e8690d1e86f3e9e3f7248a15397c8 (patch)
treea4cb0ebdd55fcaf8c8855231b6ad3e1a7bf45bee /django/db/utils.py
parent7ef212af149540aa2da577a960d0d87029fd1514 (diff)
Fixed #1142 -- Added multiple database support.
This monster of a patch is the result of Alex Gaynor's 2009 Google Summer of Code project. Congratulations to Alex for a job well done. Big thanks also go to: * Justin Bronn for keeping GIS in line with the changes, * Karen Tracey and Jani Tiainen for their help testing Oracle support * Brett Hoerner, Jon Loyens, and Craig Kimmerer for their feedback. * Malcolm Treddinick for his guidance during the GSoC submission process. * Simon Willison for driving the original design process * Cal Henderson for complaining about ponies he wanted. ... and everyone else too numerous to mention that helped to bring this feature into fruition. git-svn-id: http://code.djangoproject.com/svn/django/trunk@11952 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/db/utils.py')
-rw-r--r--django/db/utils.py82
1 files changed, 82 insertions, 0 deletions
diff --git a/django/db/utils.py b/django/db/utils.py
new file mode 100644
index 0000000000..8c0b677156
--- /dev/null
+++ b/django/db/utils.py
@@ -0,0 +1,82 @@
+import inspect
+import os
+
+from django.conf import settings
+from django.core.exceptions import ImproperlyConfigured
+from django.utils.importlib import import_module
+
+def load_backend(backend_name):
+ try:
+ module = import_module('.base', 'django.db.backends.%s' % backend_name)
+ import warnings
+ warnings.warn(
+ "Short names for DATABASE_ENGINE are deprecated; prepend with 'django.db.backends.'",
+ PendingDeprecationWarning
+ )
+ return module
+ except ImportError, e:
+ # Look for a fully qualified database backend name
+ try:
+ return import_module('.base', backend_name)
+ 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(os.path.dirname(__file__), 'backends')
+ try:
+ 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 = []
+ available_backends.sort()
+ if backend_name not in 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\n" +
+ "Error was: %s") % \
+ (backend_name, ", ".join(map(repr, available_backends)), e_user)
+ raise ImproperlyConfigured(error_msg)
+ else:
+ raise # If there's some other error, this must be an error in Django itself.
+
+class ConnectionDoesNotExist(Exception):
+ pass
+
+class ConnectionHandler(object):
+ def __init__(self, databases):
+ self.databases = databases
+ self._connections = {}
+
+ 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)
+ conn.setdefault('ENGINE', 'dummy')
+ conn.setdefault('OPTIONS', {})
+ conn.setdefault('TEST_CHARSET', None)
+ conn.setdefault('TEST_COLLATION', None)
+ conn.setdefault('TEST_NAME', None)
+ conn.setdefault('TIME_ZONE', settings.TIME_ZONE)
+ for setting in ('NAME', 'USER', 'PASSWORD', 'HOST', 'PORT'):
+ conn.setdefault(setting, '')
+
+ def __getitem__(self, alias):
+ if alias in self._connections:
+ return self._connections[alias]
+
+ self.ensure_defaults(alias)
+ db = self.databases[alias]
+ backend = load_backend(db['ENGINE'])
+ conn = backend.DatabaseWrapper(db, alias)
+ self._connections[alias] = conn
+ return conn
+
+ def __iter__(self):
+ return iter(self.databases)
+
+ def all(self):
+ return [self[alias] for alias in self]