|
10 | 10 | from django.db.models.expressions import Case, Exists, Expression, OrderBy, When, Window
|
11 | 11 | from django.db.models.fields import BinaryField, Field
|
12 | 12 | from django.db.models.functions import Cast, NthValue, MD5, SHA1, SHA224, SHA256, SHA384, SHA512
|
| 13 | +from django.db.models.functions.datetime import Now |
13 | 14 | from django.db.models.functions.math import ATan2, Ln, Log, Mod, Round, Degrees, Radians, Power
|
| 15 | +from django.db.models.functions.text import Replace |
14 | 16 | from django.db.models.lookups import In, Lookup
|
15 | 17 | from django.db.models.query import QuerySet
|
16 | 18 | from django.db.models.sql.query import Query
|
@@ -44,6 +46,19 @@ def sqlserver_log(self, compiler, connection, **extra_context):
|
44 | 46 | def sqlserver_ln(self, compiler, connection, **extra_context):
|
45 | 47 | return self.as_sql(compiler, connection, function='LOG', **extra_context)
|
46 | 48 |
|
| 49 | + |
| 50 | +def sqlserver_replace(self, compiler, connection, **extra_context): |
| 51 | + current_db = "CONVERT(varchar, (SELECT DB_NAME()))" |
| 52 | + with connection.cursor() as cursor: |
| 53 | + cursor.execute("SELECT CONVERT(varchar, DATABASEPROPERTYEX(%s, 'collation'))" % current_db) |
| 54 | + default_collation = cursor.fetchone()[0] |
| 55 | + current_collation = default_collation.replace('_CI', '_CS') |
| 56 | + return self.as_sql( |
| 57 | + compiler, connection, function='REPLACE', |
| 58 | + template = 'REPLACE(%s COLLATE %s)' % ('%(expressions)s', current_collation), |
| 59 | + **extra_context |
| 60 | + ) |
| 61 | + |
47 | 62 | def sqlserver_degrees(self, compiler, connection, **extra_context):
|
48 | 63 | return self.as_sql(
|
49 | 64 | compiler, connection, function='DEGREES',
|
@@ -109,6 +124,10 @@ def sqlserver_exists(self, compiler, connection, template=None, **extra_context)
|
109 | 124 | sql = 'CASE WHEN {} THEN 1 ELSE 0 END'.format(sql)
|
110 | 125 | return sql, params
|
111 | 126 |
|
| 127 | +def sqlserver_now(self, compiler, connection, **extra_context): |
| 128 | + return self.as_sql( |
| 129 | + compiler, connection, template="SYSDATETIME()", **extra_context |
| 130 | + ) |
112 | 131 |
|
113 | 132 | def sqlserver_lookup(self, compiler, connection):
|
114 | 133 | # MSSQL doesn't allow EXISTS() to be compared to another expression
|
@@ -273,7 +292,7 @@ def bulk_update_with_default(self, objs, fields, batch_size=None, default=0):
|
273 | 292 | SQL Server require that at least one of the result expressions in a CASE specification must be an expression other than the NULL constant.
|
274 | 293 | Patched with a default value 0. The user can also pass a custom default value for CASE statement.
|
275 | 294 | """
|
276 |
| - if batch_size is not None and batch_size < 0: |
| 295 | + if batch_size is not None and batch_size <= 0: |
277 | 296 | raise ValueError('Batch size must be a positive integer.')
|
278 | 297 | if not fields:
|
279 | 298 | raise ValueError('Field names must be given to bulk_update().')
|
@@ -441,6 +460,8 @@ def sqlserver_sha512(self, compiler, connection, **extra_context):
|
441 | 460 | NthValue.as_microsoft = sqlserver_nth_value
|
442 | 461 | Round.as_microsoft = sqlserver_round
|
443 | 462 | Window.as_microsoft = sqlserver_window
|
| 463 | +Replace.as_microsoft = sqlserver_replace |
| 464 | +Now.as_microsoft = sqlserver_now |
444 | 465 | MD5.as_microsoft = sqlserver_md5
|
445 | 466 | SHA1.as_microsoft = sqlserver_sha1
|
446 | 467 | SHA224.as_microsoft = sqlserver_sha224
|
|
0 commit comments