From 315145f7ca682f8361d956e985f533a7fb421cde Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Wed, 11 Mar 2009 03:39:34 +0000 Subject: Fixed #10459 -- Refactored the internals of database connection objects so that connections know their own settings and pass around settings as dictionaries instead of passing around the Django settings module itself. This will make it easier for multiple database support. Thanks to Alex Gaynor for the initial patch. This is backwards-compatible but will likely break third-party database backends. Specific API changes are: * BaseDatabaseWrapper.__init__() now takes a settings_dict instead of a settings module. It's called settings_dict to disambiguate, and for easy grepability. This should be a dictionary containing DATABASE_NAME, etc. * BaseDatabaseWrapper has a settings_dict attribute instead of an options attribute. BaseDatabaseWrapper.options is now BaseDatabaseWrapper['DATABASE_OPTIONS'] * BaseDatabaseWrapper._cursor() no longer takes a settings argument. * BaseDatabaseClient.__init__() now takes a connection argument (a DatabaseWrapper instance) instead of no arguments. git-svn-id: http://code.djangoproject.com/svn/django/trunk@10026 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/backends/sqlite3/base.py | 14 +++++++------- django/db/backends/sqlite3/client.py | 3 +-- 2 files changed, 8 insertions(+), 9 deletions(-) (limited to 'django/db/backends/sqlite3') diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index ba0ef16b61..b0c087d1cd 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -149,21 +149,22 @@ class DatabaseWrapper(BaseDatabaseWrapper): self.features = DatabaseFeatures() self.ops = DatabaseOperations() - self.client = DatabaseClient() + self.client = DatabaseClient(self) self.creation = DatabaseCreation(self) self.introspection = DatabaseIntrospection(self) self.validation = BaseDatabaseValidation() - def _cursor(self, settings): + def _cursor(self): if self.connection is None: - if not settings.DATABASE_NAME: + settings_dict = self.settings_dict + if not settings_dict['DATABASE_NAME']: from django.core.exceptions import ImproperlyConfigured raise ImproperlyConfigured, "Please fill out DATABASE_NAME in the settings module before using the database." kwargs = { - 'database': settings.DATABASE_NAME, + 'database': settings_dict['DATABASE_NAME'], 'detect_types': Database.PARSE_DECLTYPES | Database.PARSE_COLNAMES, } - kwargs.update(self.options) + kwargs.update(settings_dict['DATABASE_OPTIONS']) self.connection = Database.connect(**kwargs) # Register extract, date_trunc, and regexp functions. self.connection.create_function("django_extract", 2, _sqlite_extract) @@ -172,11 +173,10 @@ class DatabaseWrapper(BaseDatabaseWrapper): return self.connection.cursor(factory=SQLiteCursorWrapper) def close(self): - from django.conf import settings # If database is in memory, closing the connection destroys the # database. To prevent accidental data loss, ignore close requests on # an in-memory db. - if settings.DATABASE_NAME != ":memory:": + if self.settings_dict['DATABASE_NAME'] != ":memory:": BaseDatabaseWrapper.close(self) class SQLiteCursorWrapper(Database.Cursor): diff --git a/django/db/backends/sqlite3/client.py b/django/db/backends/sqlite3/client.py index 239e72f1e9..0b65444d74 100644 --- a/django/db/backends/sqlite3/client.py +++ b/django/db/backends/sqlite3/client.py @@ -1,10 +1,9 @@ from django.db.backends import BaseDatabaseClient -from django.conf import settings import os class DatabaseClient(BaseDatabaseClient): executable_name = 'sqlite3' def runshell(self): - args = ['', settings.DATABASE_NAME] + args = ['', self.connection.settings_dict['DATABASE_NAME']] os.execvp(self.executable_name, args) -- cgit v1.3