Django

Code

Ticket #6064: 6064.diff

File 6064.diff, 5.6 kB (added by floguy, 1 year ago)

Patch to add CONNECTION_INIT_SQL setting and use it on connection initialization.

  • django/db/backends/ado_mssql/base.py

    old new  
    109109            # TODO: Handle DATABASE_PORT. 
    110110            conn_string = "PROVIDER=SQLOLEDB;DATA SOURCE=%s;UID=%s;PWD=%s;DATABASE=%s" % (settings.DATABASE_HOST, settings.DATABASE_USER, settings.DATABASE_PASSWORD, settings.DATABASE_NAME) 
    111111            self.connection = Database.connect(conn_string) 
    112         return self.connection.cursor() 
     112        cursor = self.connection.cursor() 
     113        for init_query in settings.CONNECTION_INIT_SQL: 
     114            cursor.execute(init_query) 
     115        return cursor 
  • django/db/backends/mysql_old/base.py

    old new  
    194194                    self.connection.set_character_set('utf8') 
    195195        else: 
    196196            cursor = self.connection.cursor() 
     197        for init_query in settings.CONNECTION_INIT_SQL: 
     198            cursor.execute(init_query) 
    197199        return cursor 
    198200 
    199201    def make_debug_cursor(self, cursor): 
  • django/db/backends/postgresql/base.py

    old new  
    100100        cursor = self.connection.cursor() 
    101101        if set_tz: 
    102102            cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE]) 
     103        for init_query in settings.CONNECTION_INIT_SQL: 
     104            cursor.execute(init_query) 
    103105        cursor.execute("SET client_encoding to 'UNICODE'") 
    104106        cursor = UnicodeCursorWrapper(cursor, 'utf-8') 
    105107        return cursor 
  • django/db/backends/sqlite3/base.py

    old new  
    112112            self.connection.create_function("django_extract", 2, _sqlite_extract) 
    113113            self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc) 
    114114            self.connection.create_function("regexp", 2, _sqlite_regexp) 
    115         return self.connection.cursor(factory=SQLiteCursorWrapper) 
     115        cursor = self.connection.cursor(factory=SQLiteCursorWrapper) 
     116        for init_query in settings.CONNECTION_INIT_SQL: 
     117            cursor.execute(init_query) 
     118        return cursor 
    116119 
    117120    def close(self): 
    118121        from django.conf import settings 
  • django/db/backends/mysql/base.py

    old new  
    181181            kwargs.update(self.options) 
    182182            self.connection = Database.connect(**kwargs) 
    183183        cursor = self.connection.cursor() 
     184        for init_query in settings.CONNECTION_INIT_SQL: 
     185            cursor.execute(init_query) 
    184186        return cursor 
    185187 
    186188    def _rollback(self): 
  • django/db/backends/oracle/base.py

    old new  
    432432        # Set oracle date to ansi date format. 
    433433        cursor.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD'") 
    434434        cursor.execute("ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'") 
     435        for init_query in settings.CONNECTION_INIT_SQL: 
     436            cursor.execute(init_query) 
    435437        return cursor 
    436438 
    437439class FormatStylePlaceholderCursor(Database.Cursor): 
  • django/db/backends/postgresql_psycopg2/base.py

    old new  
    7373        cursor.tzinfo_factory = None 
    7474        if set_tz: 
    7575            cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE]) 
     76        for init_query in settings.CONNECTION_INIT_SQL: 
     77            cursor.execute(init_query) 
    7678        return cursor 
  • django/conf/global_settings.py

    old new  
    120120DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3. 
    121121DATABASE_OPTIONS = {}          # Set to empty dictionary for default. 
    122122 
     123# List of sql statements to be executed at the beginning of every database connection. 
     124CONNECTION_INIT_SQL = ( 
     125    # 'SET search_path TO example1, example2', 
     126) 
     127 
    123128# Host for sending e-mail. 
    124129EMAIL_HOST = 'localhost' 
    125130 
  • docs/settings.txt

    old new  
    269269The default number of seconds to cache a page when the caching middleware or 
    270270``cache_page()`` decorator is used. 
    271271 
     272CONNECTION_INIT_SQL 
     273------------------- 
     274 
     275Default: ``()`` (Empty tuple) 
     276 
     277A list of sql statements to be executed at the beginning of every database connection. 
     278 
     279For example, to set the database schema to 'django_application_schema' in postgresql, you 
     280could set: 
     281 
     282    CONNECTION_INIT_SQL = ( 
     283        'SET search_path TO django_application_schema', 
     284    ) 
     285 
    272286DATABASE_ENGINE 
    273287--------------- 
    274288