Fixed #2866: Added DATABASE_OPTIONS setting which gets passed as extra kwargs to the backend's connect method. Thanks, ymasuda.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4048 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jacob Kaplan-Moss 2006-11-07 05:17:38 +00:00
parent afb2366430
commit fef89a01c5
10 changed files with 37 additions and 14 deletions

View File

@ -101,6 +101,7 @@ DATABASE_USER = '' # Not used with sqlite3.
DATABASE_PASSWORD = '' # Not used with sqlite3. DATABASE_PASSWORD = '' # Not used with sqlite3.
DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3. DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3. DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.
DATABASE_OPTIONS = {} # Set to empy dictionary for default.
# Host for sending e-mail. # Host for sending e-mail.
EMAIL_HOST = 'localhost' EMAIL_HOST = 'localhost'

View File

@ -6,6 +6,8 @@ __all__ = ('backend', 'connection', 'DatabaseError')
if not settings.DATABASE_ENGINE: if not settings.DATABASE_ENGINE:
settings.DATABASE_ENGINE = 'dummy' settings.DATABASE_ENGINE = 'dummy'
if not settings.DATABASE_OPTIONS:
settings.DATABASE_OPTIONS = {}
try: try:
backend = __import__('django.db.backends.%s.base' % settings.DATABASE_ENGINE, {}, {}, ['']) backend = __import__('django.db.backends.%s.base' % settings.DATABASE_ENGINE, {}, {}, [''])
@ -27,7 +29,7 @@ get_introspection_module = lambda: __import__('django.db.backends.%s.introspecti
get_creation_module = lambda: __import__('django.db.backends.%s.creation' % settings.DATABASE_ENGINE, {}, {}, ['']) get_creation_module = lambda: __import__('django.db.backends.%s.creation' % settings.DATABASE_ENGINE, {}, {}, [''])
runshell = lambda: __import__('django.db.backends.%s.client' % settings.DATABASE_ENGINE, {}, {}, ['']).runshell() runshell = lambda: __import__('django.db.backends.%s.client' % settings.DATABASE_ENGINE, {}, {}, ['']).runshell()
connection = backend.DatabaseWrapper() connection = backend.DatabaseWrapper(**settings.DATABASE_OPTIONS)
DatabaseError = backend.DatabaseError DatabaseError = backend.DatabaseError
# Register an event that closes the database connection # Register an event that closes the database connection

View File

@ -55,7 +55,7 @@ except ImportError:
from django.utils._threading_local import local from django.utils._threading_local import local
class DatabaseWrapper(local): class DatabaseWrapper(local):
def __init__(self): def __init__(self, **kwargs):
self.connection = None self.connection = None
self.queries = [] self.queries = []

View File

@ -20,6 +20,9 @@ class DatabaseWrapper:
_commit = complain _commit = complain
_rollback = complain _rollback = complain
def __init__(self, **kwargs):
pass
def close(self): def close(self):
pass # close() pass # close()

View File

@ -65,10 +65,11 @@ except ImportError:
from django.utils._threading_local import local from django.utils._threading_local import local
class DatabaseWrapper(local): class DatabaseWrapper(local):
def __init__(self): def __init__(self, **kwargs):
self.connection = None self.connection = None
self.queries = [] self.queries = []
self.server_version = None self.server_version = None
self.options = kwargs
def _valid_connection(self): def _valid_connection(self):
if self.connection is not None: if self.connection is not None:
@ -95,6 +96,7 @@ class DatabaseWrapper(local):
kwargs['host'] = settings.DATABASE_HOST kwargs['host'] = settings.DATABASE_HOST
if settings.DATABASE_PORT: if settings.DATABASE_PORT:
kwargs['port'] = int(settings.DATABASE_PORT) kwargs['port'] = int(settings.DATABASE_PORT)
kwargs.update(self.options)
self.connection = Database.connect(**kwargs) self.connection = Database.connect(**kwargs)
cursor = self.connection.cursor() cursor = self.connection.cursor()
if self.connection.get_server_info() >= '4.1': if self.connection.get_server_info() >= '4.1':

View File

@ -21,9 +21,10 @@ except ImportError:
from django.utils._threading_local import local from django.utils._threading_local import local
class DatabaseWrapper(local): class DatabaseWrapper(local):
def __init__(self): def __init__(self, **kwargs):
self.connection = None self.connection = None
self.queries = [] self.queries = []
self.options = kwargs
def _valid_connection(self): def _valid_connection(self):
return self.connection is not None return self.connection is not None
@ -35,10 +36,10 @@ class DatabaseWrapper(local):
settings.DATABASE_HOST = 'localhost' settings.DATABASE_HOST = 'localhost'
if len(settings.DATABASE_PORT.strip()) != 0: if len(settings.DATABASE_PORT.strip()) != 0:
dsn = Database.makedsn(settings.DATABASE_HOST, int(settings.DATABASE_PORT), settings.DATABASE_NAME) dsn = Database.makedsn(settings.DATABASE_HOST, int(settings.DATABASE_PORT), settings.DATABASE_NAME)
self.connection = Database.connect(settings.DATABASE_USER, settings.DATABASE_PASSWORD, dsn) self.connection = Database.connect(settings.DATABASE_USER, settings.DATABASE_PASSWORD, dsn, **self.options)
else: else:
conn_string = "%s/%s@%s" % (settings.DATABASE_USER, settings.DATABASE_PASSWORD, settings.DATABASE_NAME) conn_string = "%s/%s@%s" % (settings.DATABASE_USER, settings.DATABASE_PASSWORD, settings.DATABASE_NAME)
self.connection = Database.connect(conn_string) self.connection = Database.connect(conn_string, **self.options)
return FormatStylePlaceholderCursor(self.connection) return FormatStylePlaceholderCursor(self.connection)
def _commit(self): def _commit(self):

View File

@ -21,9 +21,10 @@ except ImportError:
from django.utils._threading_local import local from django.utils._threading_local import local
class DatabaseWrapper(local): class DatabaseWrapper(local):
def __init__(self): def __init__(self, **kwargs):
self.connection = None self.connection = None
self.queries = [] self.queries = []
self.options = kwargs
def cursor(self): def cursor(self):
from django.conf import settings from django.conf import settings
@ -40,7 +41,7 @@ class DatabaseWrapper(local):
conn_string += " host=%s" % settings.DATABASE_HOST conn_string += " host=%s" % settings.DATABASE_HOST
if settings.DATABASE_PORT: if settings.DATABASE_PORT:
conn_string += " port=%s" % settings.DATABASE_PORT conn_string += " port=%s" % settings.DATABASE_PORT
self.connection = Database.connect(conn_string) self.connection = Database.connect(conn_string, **self.options)
self.connection.set_isolation_level(1) # make transactions transparent to all cursors self.connection.set_isolation_level(1) # make transactions transparent to all cursors
cursor = self.connection.cursor() cursor = self.connection.cursor()
cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE]) cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE])

View File

@ -21,9 +21,10 @@ except ImportError:
from django.utils._threading_local import local from django.utils._threading_local import local
class DatabaseWrapper(local): class DatabaseWrapper(local):
def __init__(self): def __init__(self, **kwargs):
self.connection = None self.connection = None
self.queries = [] self.queries = []
self.options = kwargs
def cursor(self): def cursor(self):
from django.conf import settings from django.conf import settings
@ -40,7 +41,7 @@ class DatabaseWrapper(local):
conn_string += " host=%s" % settings.DATABASE_HOST conn_string += " host=%s" % settings.DATABASE_HOST
if settings.DATABASE_PORT: if settings.DATABASE_PORT:
conn_string += " port=%s" % settings.DATABASE_PORT conn_string += " port=%s" % settings.DATABASE_PORT
self.connection = Database.connect(conn_string) self.connection = Database.connect(conn_string, **self.options)
self.connection.set_isolation_level(1) # make transactions transparent to all cursors self.connection.set_isolation_level(1) # make transactions transparent to all cursors
cursor = self.connection.cursor() cursor = self.connection.cursor()
cursor.tzinfo_factory = None cursor.tzinfo_factory = None

View File

@ -42,16 +42,20 @@ except ImportError:
from django.utils._threading_local import local from django.utils._threading_local import local
class DatabaseWrapper(local): class DatabaseWrapper(local):
def __init__(self): def __init__(self, **kwargs):
self.connection = None self.connection = None
self.queries = [] self.queries = []
self.options = kwargs
def cursor(self): def cursor(self):
from django.conf import settings from django.conf import settings
if self.connection is None: if self.connection is None:
self.connection = Database.connect(settings.DATABASE_NAME, kwargs = {
detect_types=Database.PARSE_DECLTYPES | Database.PARSE_COLNAMES) 'database': settings.DATABASE_NAME,
'detect_types': Database.PARSE_DECLTYPES | Database.PARSE_COLNAMES,
}
kwargs.update(self.options)
self.connection = Database.connect(**kwargs)
# Register extract and date_trunc functions. # Register extract and date_trunc functions.
self.connection.create_function("django_extract", 2, _sqlite_extract) self.connection.create_function("django_extract", 2, _sqlite_extract)
self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc) self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)

View File

@ -265,6 +265,14 @@ Default: ``''`` (Empty string)
The name of the database to use. For SQLite, it's the full path to the database The name of the database to use. For SQLite, it's the full path to the database
file. file.
DATABASE_OPTIONS
----------------
Default: ``{}`` (Empty dictionary)
Extra parameters to use when connecting to the database. Consult backend
module's document for available keywords.
DATABASE_PASSWORD DATABASE_PASSWORD
----------------- -----------------