11"""
22MS SQL Server database backend for Django.
33"""
4- import sys
4+ import datetime
55import os
66import re
7- import datetime
7+ import sys
88
99from django .core .exceptions import ImproperlyConfigured
1010
2626if DjangoVersion [:2 ] >= (1 ,5 ):
2727 _DJANGO_VERSION = 15
2828elif DjangoVersion [:2 ] == (1 ,4 ):
29- # Django version 1.4 adds a backwards incompatible change to
30- # DatabaseOperations
3129 _DJANGO_VERSION = 14
3230elif DjangoVersion [:2 ] == (1 ,3 ):
3331 _DJANGO_VERSION = 13
@@ -66,8 +64,8 @@ class DatabaseFeatures(BaseDatabaseFeatures):
6664 #uses_savepoints = True
6765
6866 def _supports_transactions (self ):
69- # for Django 1.3/ 1.4 compatibility
70- return True
67+ # keep it compatible with Django 1.3 and 1.4
68+ return self . supports_transactions
7169
7270class DatabaseWrapper (BaseDatabaseWrapper ):
7371 _DJANGO_VERSION = _DJANGO_VERSION
@@ -78,7 +76,6 @@ class DatabaseWrapper(BaseDatabaseWrapper):
7876 unicode_results = False
7977 datefirst = 7
8078 use_legacy_datetime = False
81- create_new_test_db = True
8279 connection_recovery_interval_msec = 0.0
8380
8481 # Collations: http://msdn2.microsoft.com/en-us/library/ms184391.aspx
@@ -153,12 +150,10 @@ def __init__(self, *args, **kwargs):
153150 for op in self .operators :
154151 sql = self .operators [op ]
155152 if sql .startswith ('LIKE ' ):
156- ops [op ] = sql + ' COLLATE ' + self .collation
153+ ops [op ] = '%s COLLATE %s' % ( sql , self .collation )
157154 self .operators .update (ops )
158155
159- # setting mainly for running tests with Windows Azure SQL Database
160- # not to be charged too much for creating new databases in every test
161- self .create_new_test_db = self .settings_dict .get ('TEST_CREATE' , True )
156+ self .test_create = self .settings_dict .get ('TEST_CREATE' , True )
162157
163158 if _DJANGO_VERSION >= 13 :
164159 self .features = DatabaseFeatures (self )
@@ -206,15 +201,20 @@ def _cursor(self):
206201 else :
207202 driver = 'FreeTDS'
208203
204+ # Microsoft driver names assumed here are:
205+ # * SQL Server
206+ # * SQL Native Client
207+ # * SQL Server Native Client 10.0/11.0
208+ # * ODBC Driver 11 for SQL Server
209209 ms_drivers = re .compile ('.*SQL (Server$|(Server )?Native Client)' )
210210 if 'dsn' in options :
211211 cstr_parts .append ('DSN=%s' % options ['dsn' ])
212212 else :
213213 # Only append DRIVER if DATABASE_ODBC_DSN hasn't been set
214214 cstr_parts .append ('DRIVER={%s}' % driver )
215215
216- if ms_drivers .match (driver ) or driver == 'FreeTDS' \
217- and options .get ('host_is_server' , False ):
216+ if ms_drivers .match (driver ) or driver == 'FreeTDS' and \
217+ options .get ('host_is_server' , False ):
218218 if port_str :
219219 host_str += ';PORT=%s' % port_str
220220 cstr_parts .append ('SERVER=%s' % host_str )
@@ -292,7 +292,7 @@ def _cursor(self):
292292
293293 return CursorWrapper (cursor , self )
294294
295- def _execute_on_tables (self , sql , table_names = None ):
295+ def _execute_foreach (self , sql , table_names = None ):
296296 cursor = self .cursor ()
297297 if not table_names :
298298 table_names = self .introspection .get_table_list (cursor )
@@ -312,12 +312,12 @@ def _on_error(self, e):
312312 time .sleep (self .connection_recovery_interval_msec )
313313
314314 def check_constraints (self , table_names = None ):
315- self ._execute_on_tables ('ALTER TABLE %s WITH CHECK CHECK CONSTRAINT ALL' , table_names )
315+ self ._execute_foreach ('ALTER TABLE %s WITH CHECK CHECK CONSTRAINT ALL' , table_names )
316316
317317 def disable_constraint_checking (self ):
318318 # Windows Azure SQL Database doesn't support sp_msforeachtable
319319 #cursor.execute('EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT ALL"')
320- self ._execute_on_tables ('ALTER TABLE %s NOCHECK CONSTRAINT ALL' )
320+ self ._execute_foreach ('ALTER TABLE %s NOCHECK CONSTRAINT ALL' )
321321 return True
322322
323323 def enable_constraint_checking (self ):
0 commit comments