Fixed #6064 -- Added the `connection_created` signal for when a database connection is created.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10182 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Justin Bronn 2009-03-29 23:15:58 +00:00
parent c0ee4c5cac
commit da3b38cdda
7 changed files with 35 additions and 5 deletions

View File

@ -25,6 +25,7 @@ from MySQLdb.converters import conversions
from MySQLdb.constants import FIELD_TYPE, FLAG from MySQLdb.constants import FIELD_TYPE, FLAG
from django.db.backends import * from django.db.backends import *
from django.db.backends.signals import connection_created
from django.db.backends.mysql.client import DatabaseClient from django.db.backends.mysql.client import DatabaseClient
from django.db.backends.mysql.creation import DatabaseCreation from django.db.backends.mysql.creation import DatabaseCreation
from django.db.backends.mysql.introspection import DatabaseIntrospection from django.db.backends.mysql.introspection import DatabaseIntrospection
@ -277,6 +278,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
self.connection = Database.connect(**kwargs) self.connection = Database.connect(**kwargs)
self.connection.encoders[SafeUnicode] = self.connection.encoders[unicode] self.connection.encoders[SafeUnicode] = self.connection.encoders[unicode]
self.connection.encoders[SafeString] = self.connection.encoders[str] self.connection.encoders[SafeString] = self.connection.encoders[str]
connection_created.send(sender=self.__class__)
cursor = CursorWrapper(self.connection.cursor()) cursor = CursorWrapper(self.connection.cursor())
return cursor return cursor

View File

@ -25,6 +25,7 @@ except ImportError, e:
raise ImproperlyConfigured("Error loading cx_Oracle module: %s" % e) raise ImproperlyConfigured("Error loading cx_Oracle module: %s" % e)
from django.db.backends import * from django.db.backends import *
from django.db.backends.signals import connection_created
from django.db.backends.oracle import query from django.db.backends.oracle import query
from django.db.backends.oracle.client import DatabaseClient from django.db.backends.oracle.client import DatabaseClient
from django.db.backends.oracle.creation import DatabaseCreation from django.db.backends.oracle.creation import DatabaseCreation
@ -329,6 +330,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
# Django docs specify cx_Oracle version 4.3.1 or higher, but # Django docs specify cx_Oracle version 4.3.1 or higher, but
# stmtcachesize is available only in 4.3.2 and up. # stmtcachesize is available only in 4.3.2 and up.
pass pass
connection_created.send(sender=self.__class__)
if not cursor: if not cursor:
cursor = FormatStylePlaceholderCursor(self.connection) cursor = FormatStylePlaceholderCursor(self.connection)
return cursor return cursor

View File

@ -5,6 +5,7 @@ Requires psycopg 1: http://initd.org/projects/psycopg1
""" """
from django.db.backends import * from django.db.backends import *
from django.db.backends.signals import connection_created
from django.db.backends.postgresql.client import DatabaseClient from django.db.backends.postgresql.client import DatabaseClient
from django.db.backends.postgresql.creation import DatabaseCreation from django.db.backends.postgresql.creation import DatabaseCreation
from django.db.backends.postgresql.introspection import DatabaseIntrospection from django.db.backends.postgresql.introspection import DatabaseIntrospection
@ -114,6 +115,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
conn_string += " port=%s" % settings_dict['DATABASE_PORT'] conn_string += " port=%s" % settings_dict['DATABASE_PORT']
self.connection = Database.connect(conn_string, **settings_dict['DATABASE_OPTIONS']) self.connection = Database.connect(conn_string, **settings_dict['DATABASE_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
connection_created.send(sender=self.__class__)
cursor = self.connection.cursor() cursor = self.connection.cursor()
if set_tz: if set_tz:
cursor.execute("SET TIME ZONE %s", [settings_dict['TIME_ZONE']]) cursor.execute("SET TIME ZONE %s", [settings_dict['TIME_ZONE']])

View File

@ -6,6 +6,7 @@ Requires psycopg 2: http://initd.org/projects/psycopg2
from django.conf import settings from django.conf import settings
from django.db.backends import * from django.db.backends import *
from django.db.backends.signals import connection_created
from django.db.backends.postgresql.operations import DatabaseOperations as PostgresqlDatabaseOperations from django.db.backends.postgresql.operations import DatabaseOperations as PostgresqlDatabaseOperations
from django.db.backends.postgresql.client import DatabaseClient from django.db.backends.postgresql.client import DatabaseClient
from django.db.backends.postgresql.creation import DatabaseCreation from django.db.backends.postgresql.creation import DatabaseCreation
@ -96,6 +97,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
conn_params['port'] = settings_dict['DATABASE_PORT'] conn_params['port'] = settings_dict['DATABASE_PORT']
self.connection = Database.connect(**conn_params) self.connection = Database.connect(**conn_params)
self.connection.set_client_encoding('UTF8') self.connection.set_client_encoding('UTF8')
connection_created.send(sender=self.__class__)
cursor = self.connection.cursor() cursor = self.connection.cursor()
cursor.tzinfo_factory = None cursor.tzinfo_factory = None
if set_tz: if set_tz:

View File

@ -0,0 +1,6 @@
from django.dispatch import Signal
connection_created = Signal()
from django.dispatch import Signal
connection_created = Signal()

View File

@ -8,6 +8,7 @@ standard library.
""" """
from django.db.backends import * from django.db.backends import *
from django.db.backends.signals import connection_created
from django.db.backends.sqlite3.client import DatabaseClient from django.db.backends.sqlite3.client import DatabaseClient
from django.db.backends.sqlite3.creation import DatabaseCreation from django.db.backends.sqlite3.creation import DatabaseCreation
from django.db.backends.sqlite3.introspection import DatabaseIntrospection from django.db.backends.sqlite3.introspection import DatabaseIntrospection
@ -171,6 +172,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
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)
self.connection.create_function("regexp", 2, _sqlite_regexp) self.connection.create_function("regexp", 2, _sqlite_regexp)
connection_created.send(sender=self.__class__)
return self.connection.cursor(factory=SQLiteCursorWrapper) return self.connection.cursor(factory=SQLiteCursorWrapper)
def close(self): def close(self):

View File

@ -1,13 +1,10 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Unit and doctests for specific database backends.
# Unit tests for specific database backends.
import unittest import unittest
from django.db import connection from django.db import connection
from django.db.backends.signals import connection_created
from django.conf import settings from django.conf import settings
class Callproc(unittest.TestCase): class Callproc(unittest.TestCase):
def test_dbms_session(self): def test_dbms_session(self):
@ -21,6 +18,23 @@ class Callproc(unittest.TestCase):
else: else:
return True return True
def connection_created_test(sender, **kwargs):
print 'connection_created signal'
__test__ = {'API_TESTS': ''}
# Unfortunately with sqlite3 the in-memory test database cannot be
# closed, and so it cannot be re-opened during testing, and so we
# sadly disable this test for now.
if settings.DATABASE_ENGINE != 'sqlite3':
__test__['API_TESTS'] += """
>>> connection_created.connect(connection_created_test)
>>> connection.close() # Ensure the connection is closed
>>> cursor = connection.cursor()
connection_created signal
>>> connection_created.disconnect(connection_created_test)
>>> cursor = connection.cursor()
"""
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()