Skip to content

Commit dba58e0

Browse files
committed
Fix iso_year, Mod function, Autofield.
1 parent 3dc0113 commit dba58e0

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

sql_server/pyodbc/base.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,28 @@
1212
except ImportError as e:
1313
raise ImproperlyConfigured("Error loading pyodbc module: %s" % e)
1414

15-
from django.utils.version import get_version_tuple # noqa
15+
from django.utils.version import get_version_tuple # noqa
1616

1717
pyodbc_ver = get_version_tuple(Database.version)
1818
if pyodbc_ver < (3, 0):
1919
raise ImproperlyConfigured("pyodbc 3.0 or newer is required; you have %s" % Database.version)
2020

21-
from django.conf import settings # noqa
22-
from django.db import NotSupportedError # noqa
23-
from django.db.backends.base.base import BaseDatabaseWrapper # noqa
24-
from django.utils.encoding import smart_str # noqa
25-
from django.utils.functional import cached_property # noqa
21+
from django.conf import settings # noqa
22+
from django.db import NotSupportedError # noqa
23+
from django.db.backends.base.base import BaseDatabaseWrapper # noqa
24+
from django.utils.encoding import smart_str # noqa
25+
from django.utils.functional import cached_property # noqa
2626

2727
if hasattr(settings, 'DATABASE_CONNECTION_POOLING'):
2828
if not settings.DATABASE_CONNECTION_POOLING:
2929
Database.pooling = False
3030

31-
from .client import DatabaseClient # noqa
32-
from .creation import DatabaseCreation # noqa
33-
from .features import DatabaseFeatures # noqa
34-
from .introspection import DatabaseIntrospection # noqa
35-
from .operations import DatabaseOperations # noqa
36-
from .schema import DatabaseSchemaEditor # noqa
31+
from .client import DatabaseClient # noqa
32+
from .creation import DatabaseCreation # noqa
33+
from .features import DatabaseFeatures # noqa
34+
from .introspection import DatabaseIntrospection # noqa
35+
from .operations import DatabaseOperations # noqa
36+
from .schema import DatabaseSchemaEditor # noqa
3737

3838
EDITION_AZURE_SQL_DB = 5
3939

@@ -69,8 +69,8 @@ class DatabaseWrapper(BaseDatabaseWrapper):
6969
# be interpolated against the values of Field.__dict__ before being output.
7070
# If a column type is set to None, it won't be included in the output.
7171
data_types = {
72-
'AutoField': 'int IDENTITY (1, 1)',
73-
'BigAutoField': 'bigint IDENTITY (1, 1)',
72+
'AutoField': 'int',
73+
'BigAutoField': 'bigint',
7474
'BigIntegerField': 'bigint',
7575
'BinaryField': 'varbinary(max)',
7676
'BooleanField': 'bit',
@@ -90,12 +90,17 @@ class DatabaseWrapper(BaseDatabaseWrapper):
9090
'PositiveIntegerField': 'int',
9191
'PositiveSmallIntegerField': 'smallint',
9292
'SlugField': 'nvarchar(%(max_length)s)',
93-
'SmallAutoField': 'smallint IDENTITY (1, 1)',
93+
'SmallAutoField': 'smallint',
9494
'SmallIntegerField': 'smallint',
9595
'TextField': 'nvarchar(max)',
9696
'TimeField': 'time',
9797
'UUIDField': 'char(32)',
9898
}
99+
data_types_suffix = {
100+
'AutoField': 'IDENTITY (1, 1)',
101+
'BigAutoField': 'IDENTITY (1, 1)',
102+
'SmallAutoField': 'IDENTITY (1, 1)',
103+
}
99104
data_type_check_constraints = {
100105
'PositiveIntegerField': '[%(column)s] >= 0',
101106
'PositiveSmallIntegerField': '[%(column)s] >= 0',

sql_server/pyodbc/functions.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django import VERSION
22
from django.db.models import BooleanField
33
from django.db.models.functions import Cast
4-
from django.db.models.functions.math import ATan2, Log, Ln, Round
4+
from django.db.models.functions.math import ATan2, Log, Ln, Mod, Round
55
from django.db.models.expressions import Case, Exists, OrderBy, When
66
from django.db.models.lookups import Lookup
77

@@ -26,6 +26,20 @@ def sqlserver_ln(self, compiler, connection, **extra_context):
2626
return self.as_sql(compiler, connection, function='LOG', **extra_context)
2727

2828

29+
def sqlserver_mod(self, compiler, connection):
30+
# MSSQL doesn't have keyword MOD
31+
expr = self.get_source_expressions()
32+
number_a = compiler.compile(expr[0])
33+
number_b = compiler.compile(expr[1])
34+
return self.as_sql(
35+
compiler, connection,
36+
function="",
37+
template='(ABS({a}) - FLOOR(ABS({a}) / ABS({b})) * ABS({b})) * SIGN({a}) * SIGN({b})'.format(
38+
a=number_a[0], b=number_b[0]),
39+
arg_joiner=""
40+
)
41+
42+
2943
def sqlserver_round(self, compiler, connection, **extra_context):
3044
return self.as_sql(compiler, connection, template='%(function)s(%(expressions)s, 0)', **extra_context)
3145

@@ -77,6 +91,7 @@ def sqlserver_orderby(self, compiler, connection):
7791
ATan2.as_microsoft = sqlserver_atan2
7892
Log.as_microsoft = sqlserver_log
7993
Ln.as_microsoft = sqlserver_ln
94+
Mod.as_microsoft = sqlserver_mod
8095
Round.as_microsoft = sqlserver_round
8196

8297
if DJANGO3:

sql_server/pyodbc/operations.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ def date_extract_sql(self, lookup_type, field_name):
110110
return "DATEPART(weekday, %s)" % field_name
111111
elif lookup_type == 'week':
112112
return "DATEPART(iso_week, %s)" % field_name
113+
elif lookup_type == 'iso_year':
114+
return "YEAR(DATEADD(day, 26 - DATEPART(isoww, %s), %s))" % (field_name, field_name)
113115
else:
114116
return "DATEPART(%s, %s)" % (lookup_type, field_name)
115117

0 commit comments

Comments
 (0)