Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion django_spanner/lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ def iexact(self, compiler, connection):
# lhs_sql is the expression/column to use as the regular expression.
# Use concat to make the value case-insensitive.
lhs_sql = "CONCAT('^(?i)', " + lhs_sql + ", '$')"
rhs_sql = rhs_sql.replace("%%s", "%s")
if not self.rhs_is_direct_value() and not params:
# If rhs is not a direct value and parameter is not present we want
# to have only 1 formatable argument in rhs_sql else we need 2.
rhs_sql = rhs_sql.replace("%%s", "%s")
# rhs_sql is REGEXP_CONTAINS(%s, %%s), and lhs_sql is the column name.
return rhs_sql % lhs_sql, params

Expand Down
14 changes: 14 additions & 0 deletions tests/unit/django_spanner/test_lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,17 @@ def test_iexact_sql_query_case_insensitive_function_transform(self):
+ "CONCAT('^(?i)', CAST(UPPER(tests_author.name) AS STRING), '$'))",
)
self.assertEqual(params, ())

def test_iexact_sql_query_case_insensitive_value_match(self):

qs1 = Author.objects.filter(name__upper__iexact="abc").values("name")
compiler = SQLCompiler(qs1.query, self.connection, "default")
sql_compiled, params = compiler.as_sql()

self.assertEqual(
sql_compiled,
"SELECT tests_author.name FROM tests_author WHERE "
+ "REGEXP_CONTAINS((UPPER(CONCAT('^(?i)', "
+ "CAST(UPPER(tests_author.name) AS STRING), '$'))), %s)",
)
self.assertEqual(params, ("abc",))