Skip to content

Commit bcca753

Browse files
authored
Add case-sensitive feature for Replace function (#222)
* replacetests case sensitive fix * remove case sensitive skipped test * refix to use django .replace
1 parent 17bb0c8 commit bcca753

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

mssql/functions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from django.db.models.fields import BinaryField, Field
1212
from django.db.models.functions import Cast, NthValue, MD5, SHA1, SHA224, SHA256, SHA384, SHA512
1313
from django.db.models.functions.math import ATan2, Ln, Log, Mod, Round, Degrees, Radians, Power
14+
from django.db.models.functions.text import Replace
1415
from django.db.models.lookups import In, Lookup
1516
from django.db.models.query import QuerySet
1617
from django.db.models.sql.query import Query
@@ -44,6 +45,19 @@ def sqlserver_log(self, compiler, connection, **extra_context):
4445
def sqlserver_ln(self, compiler, connection, **extra_context):
4546
return self.as_sql(compiler, connection, function='LOG', **extra_context)
4647

48+
49+
def sqlserver_replace(self, compiler, connection, **extra_context):
50+
current_db = "CONVERT(varchar, (SELECT DB_NAME()))"
51+
with connection.cursor() as cursor:
52+
cursor.execute("SELECT CONVERT(varchar, DATABASEPROPERTYEX(%s, 'collation'))" % current_db)
53+
default_collation = cursor.fetchone()[0]
54+
current_collation = default_collation.replace('_CI', '_CS')
55+
return self.as_sql(
56+
compiler, connection, function='REPLACE',
57+
template = 'REPLACE(%s COLLATE %s)' % ('%(expressions)s', current_collation),
58+
**extra_context
59+
)
60+
4761
def sqlserver_degrees(self, compiler, connection, **extra_context):
4862
return self.as_sql(
4963
compiler, connection, function='DEGREES',
@@ -441,6 +455,7 @@ def sqlserver_sha512(self, compiler, connection, **extra_context):
441455
NthValue.as_microsoft = sqlserver_nth_value
442456
Round.as_microsoft = sqlserver_round
443457
Window.as_microsoft = sqlserver_window
458+
Replace.as_microsoft = sqlserver_replace
444459
MD5.as_microsoft = sqlserver_md5
445460
SHA1.as_microsoft = sqlserver_sha1
446461
SHA224.as_microsoft = sqlserver_sha224

testapp/settings.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@
138138
'aggregation.tests.AggregateTestCase.test_count_star',
139139
'aggregation_regress.tests.AggregationTests.test_values_list_annotation_args_ordering',
140140
'db_functions.text.test_pad.PadTests.test_pad',
141-
'db_functions.text.test_replace.ReplaceTests.test_case_sensitive',
142141
'expressions.tests.FTimeDeltaTests.test_invalid_operator',
143142
'fixtures_regress.tests.TestFixtures.test_loaddata_raises_error_when_fixture_has_invalid_foreign_key',
144143
'invalid_models_tests.test_ordinary_fields.TextFieldTests.test_max_length_warning',

0 commit comments

Comments
 (0)