2006-05-02 09:31:56 +08:00
|
|
|
from django.core import signals
|
2014-06-06 19:10:20 +08:00
|
|
|
from django.db.utils import (DEFAULT_DB_ALIAS, DJANGO_VERSION_PICKLE_KEY,
|
|
|
|
DataError, OperationalError, IntegrityError, InternalError, ProgrammingError,
|
|
|
|
NotSupportedError, DatabaseError, InterfaceError, Error, ConnectionHandler,
|
|
|
|
ConnectionRouter)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'backend', 'connection', 'connections', 'router', 'DatabaseError',
|
|
|
|
'IntegrityError', 'InternalError', 'ProgrammingError', 'DataError',
|
|
|
|
'NotSupportedError', 'Error', 'InterfaceError', 'OperationalError',
|
2014-06-06 19:10:20 +08:00
|
|
|
'DEFAULT_DB_ALIAS', 'DJANGO_VERSION_PICKLE_KEY'
|
2013-07-08 08:39:54 +08:00
|
|
|
]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-05-21 17:35:05 +08:00
|
|
|
connections = ConnectionHandler()
|
2009-03-13 12:41:45 +08:00
|
|
|
|
2013-05-21 18:21:31 +08:00
|
|
|
router = ConnectionRouter()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-03-11 11:39:34 +08:00
|
|
|
# `connection`, `DatabaseError` and `IntegrityError` are convenient aliases
|
|
|
|
# for backend bits.
|
|
|
|
|
2015-05-03 03:56:53 +08:00
|
|
|
# DatabaseWrapper.__init__() takes a dictionary, not a settings module, so we
|
|
|
|
# manually create the dictionary from the settings, passing only the settings
|
|
|
|
# that the database backends care about.
|
2011-12-16 21:40:19 +08:00
|
|
|
# We load all these up for backwards compatibility, you should use
|
2009-12-22 23:18:51 +08:00
|
|
|
# connections['default'] instead.
|
2011-12-16 21:40:19 +08:00
|
|
|
class DefaultConnectionProxy(object):
|
|
|
|
"""
|
|
|
|
Proxy for accessing the default DatabaseWrapper object's attributes. If you
|
|
|
|
need to access the DatabaseWrapper object itself, use
|
|
|
|
connections[DEFAULT_DB_ALIAS] instead.
|
|
|
|
"""
|
|
|
|
def __getattr__(self, item):
|
|
|
|
return getattr(connections[DEFAULT_DB_ALIAS], item)
|
|
|
|
|
|
|
|
def __setattr__(self, name, value):
|
|
|
|
return setattr(connections[DEFAULT_DB_ALIAS], name, value)
|
|
|
|
|
2013-03-01 00:05:25 +08:00
|
|
|
def __delattr__(self, name):
|
|
|
|
return delattr(connections[DEFAULT_DB_ALIAS], name)
|
|
|
|
|
2013-07-08 08:32:04 +08:00
|
|
|
def __eq__(self, other):
|
|
|
|
return connections[DEFAULT_DB_ALIAS] == other
|
|
|
|
|
|
|
|
def __ne__(self, other):
|
|
|
|
return connections[DEFAULT_DB_ALIAS] != other
|
|
|
|
|
2011-12-16 21:40:19 +08:00
|
|
|
connection = DefaultConnectionProxy()
|
2013-05-21 17:11:05 +08:00
|
|
|
|
2013-07-08 08:32:04 +08:00
|
|
|
|
2013-02-18 18:37:26 +08:00
|
|
|
# Register an event to reset saved queries when a Django request is started.
|
2008-08-06 23:32:46 +08:00
|
|
|
def reset_queries(**kwargs):
|
2009-12-28 13:11:57 +08:00
|
|
|
for conn in connections.all():
|
2014-06-07 20:09:27 +08:00
|
|
|
conn.queries_log.clear()
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.request_started.connect(reset_queries)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2013-02-18 18:37:26 +08:00
|
|
|
# Register an event to reset transaction state and close connections past
|
2014-03-21 21:21:43 +08:00
|
|
|
# their lifetime.
|
2013-02-18 18:37:26 +08:00
|
|
|
def close_old_connections(**kwargs):
|
|
|
|
for conn in connections.all():
|
|
|
|
conn.close_if_unusable_or_obsolete()
|
|
|
|
signals.request_started.connect(close_old_connections)
|
|
|
|
signals.request_finished.connect(close_old_connections)
|