88from django .core .exceptions import ImproperlyConfigured
99from django import VERSION
1010
11- if VERSION [:3 ] < (2 ,1 , 0 ) or VERSION [:2 ] >= (2 ,2 ):
11+ if VERSION [:3 ] < (2 , 1 , 0 ) or VERSION [:2 ] >= (2 , 2 ):
1212 raise ImproperlyConfigured ("Django %d.%d.%d is not supported." % VERSION [:3 ])
1313
1414try :
1515 import pyodbc as Database
1616except ImportError as e :
1717 raise ImproperlyConfigured ("Error loading pyodbc module: %s" % e )
1818
19- from django .utils .version import get_version_tuple
19+ from django .utils .version import get_version_tuple # noqa
2020
2121pyodbc_ver = get_version_tuple (Database .version )
22- if pyodbc_ver < (3 ,0 ):
22+ if pyodbc_ver < (3 , 0 ):
2323 raise ImproperlyConfigured ("pyodbc 3.0 or newer is required; you have %s" % Database .version )
2424
25- from django .conf import settings
26- from django .db import NotSupportedError
27- from django .db .backends .base .base import BaseDatabaseWrapper
28- from django .db .backends .base .validation import BaseDatabaseValidation
29- from django .utils .encoding import smart_str
30- from django .utils .functional import cached_property
31- from django .utils .timezone import utc
25+ from django .conf import settings # noqa
26+ from django .db import NotSupportedError # noqa
27+ from django .db .backends .base .base import BaseDatabaseWrapper # noqa
28+ from django .utils .encoding import smart_str # noqa
29+ from django .utils .functional import cached_property # noqa
3230
3331if hasattr (settings , 'DATABASE_CONNECTION_POOLING' ):
3432 if not settings .DATABASE_CONNECTION_POOLING :
3533 Database .pooling = False
3634
37- from .client import DatabaseClient
38- from .creation import DatabaseCreation
39- from .features import DatabaseFeatures
40- from .introspection import DatabaseIntrospection
41- from .operations import DatabaseOperations
42- from .schema import DatabaseSchemaEditor
35+ from .client import DatabaseClient # noqa
36+ from .creation import DatabaseCreation # noqa
37+ from .features import DatabaseFeatures # noqa
38+ from .introspection import DatabaseIntrospection # noqa
39+ from .operations import DatabaseOperations # noqa
40+ from .schema import DatabaseSchemaEditor # noqa
4341
4442EDITION_AZURE_SQL_DB = 5
4543
@@ -57,6 +55,7 @@ def encode_connection_string(fields):
5755 for k , v in fields .items ()
5856 )
5957
58+
6059def encode_value (v ):
6160 """If the value contains a semicolon, or starts with a left curly brace,
6261 then enclose it in curly braces and escape all right curly braces.
@@ -65,6 +64,7 @@ def encode_value(v):
6564 return '{%s}' % (v .replace ('}' , '}}' ),)
6665 return v
6766
67+
6868class DatabaseWrapper (BaseDatabaseWrapper ):
6969 vendor = 'microsoft'
7070 display_name = 'SQL Server'
@@ -73,31 +73,31 @@ class DatabaseWrapper(BaseDatabaseWrapper):
7373 # be interpolated against the values of Field.__dict__ before being output.
7474 # If a column type is set to None, it won't be included in the output.
7575 data_types = {
76- 'AutoField' : 'int IDENTITY (1, 1)' ,
77- 'BigAutoField' : 'bigint IDENTITY (1, 1)' ,
78- 'BigIntegerField' : 'bigint' ,
79- 'BinaryField' : 'varbinary(max)' ,
80- 'BooleanField' : 'bit' ,
81- 'CharField' : 'nvarchar(%(max_length)s)' ,
82- 'DateField' : 'date' ,
83- 'DateTimeField' : 'datetime2' ,
84- 'DecimalField' : 'numeric(%(max_digits)s, %(decimal_places)s)' ,
85- 'DurationField' : 'bigint' ,
86- 'FileField' : 'nvarchar(%(max_length)s)' ,
87- 'FilePathField' : 'nvarchar(%(max_length)s)' ,
88- 'FloatField' : 'double precision' ,
89- 'IntegerField' : 'int' ,
90- 'IPAddressField' : 'nvarchar(15)' ,
76+ 'AutoField' : 'int IDENTITY (1, 1)' ,
77+ 'BigAutoField' : 'bigint IDENTITY (1, 1)' ,
78+ 'BigIntegerField' : 'bigint' ,
79+ 'BinaryField' : 'varbinary(max)' ,
80+ 'BooleanField' : 'bit' ,
81+ 'CharField' : 'nvarchar(%(max_length)s)' ,
82+ 'DateField' : 'date' ,
83+ 'DateTimeField' : 'datetime2' ,
84+ 'DecimalField' : 'numeric(%(max_digits)s, %(decimal_places)s)' ,
85+ 'DurationField' : 'bigint' ,
86+ 'FileField' : 'nvarchar(%(max_length)s)' ,
87+ 'FilePathField' : 'nvarchar(%(max_length)s)' ,
88+ 'FloatField' : 'double precision' ,
89+ 'IntegerField' : 'int' ,
90+ 'IPAddressField' : 'nvarchar(15)' ,
9191 'GenericIPAddressField' : 'nvarchar(39)' ,
92- 'NullBooleanField' : 'bit' ,
93- 'OneToOneField' : 'int' ,
92+ 'NullBooleanField' : 'bit' ,
93+ 'OneToOneField' : 'int' ,
9494 'PositiveIntegerField' : 'int' ,
9595 'PositiveSmallIntegerField' : 'smallint' ,
96- 'SlugField' : 'nvarchar(%(max_length)s)' ,
96+ 'SlugField' : 'nvarchar(%(max_length)s)' ,
9797 'SmallIntegerField' : 'smallint' ,
98- 'TextField' : 'nvarchar(max)' ,
99- 'TimeField' : 'time' ,
100- 'UUIDField' : 'char(32)' ,
98+ 'TextField' : 'nvarchar(max)' ,
99+ 'TimeField' : 'time' ,
100+ 'UUIDField' : 'char(32)' ,
101101 }
102102 data_type_check_constraints = {
103103 'PositiveIntegerField' : '[%(column)s] >= 0' ,
@@ -324,7 +324,7 @@ def init_connection_state(self):
324324 if ver < (0 , 95 ):
325325 raise ImproperlyConfigured (
326326 "FreeTDS 0.95 or newer is required." )
327- except :
327+ except Exception :
328328 # unknown driver version
329329 pass
330330
@@ -380,7 +380,7 @@ def sql_server_version(self, _known_versions={}):
380380 cursor .execute ("SELECT CAST(SERVERPROPERTY('ProductVersion') AS varchar)" )
381381 ver = cursor .fetchone ()[0 ]
382382 ver = int (ver .split ('.' )[0 ])
383- if not ver in self ._sql_server_versions :
383+ if ver not in self ._sql_server_versions :
384384 raise NotSupportedError ('SQL Server v%d is not supported.' % ver )
385385 _known_versions [self .alias ] = self ._sql_server_versions [ver ]
386386 return _known_versions [self .alias ]
@@ -419,7 +419,7 @@ def _on_error(self, e):
419419 self .close ()
420420 # wait a moment for recovery from network error
421421 time .sleep (self .connection_recovery_interval_msec )
422- except :
422+ except Exception :
423423 pass
424424 self .connection = None
425425
@@ -458,14 +458,14 @@ def check_constraints(self, table_names=None):
458458
459459 def disable_constraint_checking (self ):
460460 # Azure SQL Database doesn't support sp_msforeachtable
461- #cursor.execute('EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT ALL"')
461+ # cursor.execute('EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT ALL"')
462462 if not self .needs_rollback :
463463 self ._execute_foreach ('ALTER TABLE %s NOCHECK CONSTRAINT ALL' )
464464 return not self .needs_rollback
465465
466466 def enable_constraint_checking (self ):
467467 # Azure SQL Database doesn't support sp_msforeachtable
468- #cursor.execute('EXEC sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL"')
468+ # cursor.execute('EXEC sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL"')
469469 if not self .needs_rollback :
470470 self .check_constraints ()
471471
@@ -475,6 +475,7 @@ class CursorWrapper(object):
475475 A wrapper around the pyodbc's cursor that takes in account a) some pyodbc
476476 DB-API 2.0 implementation and b) some common ODBC driver particularities.
477477 """
478+
478479 def __init__ (self , cursor , connection ):
479480 self .active = True
480481 self .cursor = cursor
0 commit comments