summaryrefslogtreecommitdiff
path: root/django/db/backends/postgresql_psycopg2/base.py
diff options
context:
space:
mode:
authorThomas Chaumeny <t.chaumeny@gmail.com>2014-09-27 12:41:54 +0200
committerAnssi Kääriäinen <akaariai@gmail.com>2014-11-28 12:50:42 +0200
commit6b5d82749c57a1aae8c9e81d2b85b2cadb9ef933 (patch)
treec3f40781e1e452567a1a09126c20eff49becb899 /django/db/backends/postgresql_psycopg2/base.py
parentf39b0421b406b411c3bcb58e8aa415d885fea505 (diff)
Fixed #16731 -- Made pattern lookups work properly with F() expressions
Diffstat (limited to 'django/db/backends/postgresql_psycopg2/base.py')
-rw-r--r--django/db/backends/postgresql_psycopg2/base.py17
1 files changed, 15 insertions, 2 deletions
diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py
index 45fb7c77f0..1530e20eb7 100644
--- a/django/db/backends/postgresql_psycopg2/base.py
+++ b/django/db/backends/postgresql_psycopg2/base.py
@@ -86,9 +86,22 @@ class DatabaseWrapper(BaseDatabaseWrapper):
'iendswith': 'LIKE UPPER(%s)',
}
+ # The patterns below are used to generate SQL pattern lookup clauses when
+ # the right-hand side of the lookup isn't a raw string (it might be an expression
+ # or the result of a bilateral transformation).
+ # In those cases, special characters for LIKE operators (e.g. \, *, _) should be
+ # escaped on database side.
+ #
+ # Note: we use str.format() here for readability as '%' is used as a wildcard for
+ # the LIKE operator.
+ pattern_esc = r"REPLACE(REPLACE(REPLACE({}, '\', '\\'), '%%', '\%%'), '_', '\_')"
pattern_ops = {
- 'startswith': "LIKE %s || '%%%%'",
- 'istartswith': "LIKE UPPER(%s) || '%%%%'",
+ 'contains': "LIKE '%%' || {} || '%%'",
+ 'icontains': "LIKE '%%' || UPPER({}) || '%%'",
+ 'startswith': "LIKE {} || '%%'",
+ 'istartswith': "LIKE UPPER({}) || '%%'",
+ 'endswith': "LIKE '%%' || {}",
+ 'iendswith': "LIKE '%%' || UPPER({})",
}
Database = Database