1
1
"""
2
2
MS SQL Server database backend for Django.
3
3
"""
4
- import sys
4
+ import datetime
5
5
import os
6
6
import re
7
- import datetime
7
+ import sys
8
8
9
9
from django .core .exceptions import ImproperlyConfigured
10
10
26
26
if DjangoVersion [:2 ] >= (1 ,5 ):
27
27
_DJANGO_VERSION = 15
28
28
elif DjangoVersion [:2 ] == (1 ,4 ):
29
- # Django version 1.4 adds a backwards incompatible change to
30
- # DatabaseOperations
31
29
_DJANGO_VERSION = 14
32
30
elif DjangoVersion [:2 ] == (1 ,3 ):
33
31
_DJANGO_VERSION = 13
@@ -66,8 +64,8 @@ class DatabaseFeatures(BaseDatabaseFeatures):
66
64
#uses_savepoints = True
67
65
68
66
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
71
69
72
70
class DatabaseWrapper (BaseDatabaseWrapper ):
73
71
_DJANGO_VERSION = _DJANGO_VERSION
@@ -78,7 +76,6 @@ class DatabaseWrapper(BaseDatabaseWrapper):
78
76
unicode_results = False
79
77
datefirst = 7
80
78
use_legacy_datetime = False
81
- create_new_test_db = True
82
79
connection_recovery_interval_msec = 0.0
83
80
84
81
# Collations: http://msdn2.microsoft.com/en-us/library/ms184391.aspx
@@ -153,12 +150,10 @@ def __init__(self, *args, **kwargs):
153
150
for op in self .operators :
154
151
sql = self .operators [op ]
155
152
if sql .startswith ('LIKE ' ):
156
- ops [op ] = sql + ' COLLATE ' + self .collation
153
+ ops [op ] = '%s COLLATE %s' % ( sql , self .collation )
157
154
self .operators .update (ops )
158
155
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 )
162
157
163
158
if _DJANGO_VERSION >= 13 :
164
159
self .features = DatabaseFeatures (self )
@@ -206,15 +201,20 @@ def _cursor(self):
206
201
else :
207
202
driver = 'FreeTDS'
208
203
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
209
209
ms_drivers = re .compile ('.*SQL (Server$|(Server )?Native Client)' )
210
210
if 'dsn' in options :
211
211
cstr_parts .append ('DSN=%s' % options ['dsn' ])
212
212
else :
213
213
# Only append DRIVER if DATABASE_ODBC_DSN hasn't been set
214
214
cstr_parts .append ('DRIVER={%s}' % driver )
215
215
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 ):
218
218
if port_str :
219
219
host_str += ';PORT=%s' % port_str
220
220
cstr_parts .append ('SERVER=%s' % host_str )
@@ -292,7 +292,7 @@ def _cursor(self):
292
292
293
293
return CursorWrapper (cursor , self )
294
294
295
- def _execute_on_tables (self , sql , table_names = None ):
295
+ def _execute_foreach (self , sql , table_names = None ):
296
296
cursor = self .cursor ()
297
297
if not table_names :
298
298
table_names = self .introspection .get_table_list (cursor )
@@ -312,12 +312,12 @@ def _on_error(self, e):
312
312
time .sleep (self .connection_recovery_interval_msec )
313
313
314
314
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 )
316
316
317
317
def disable_constraint_checking (self ):
318
318
# Windows Azure SQL Database doesn't support sp_msforeachtable
319
319
#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' )
321
321
return True
322
322
323
323
def enable_constraint_checking (self ):
0 commit comments