From 6b5d82749c57a1aae8c9e81d2b85b2cadb9ef933 Mon Sep 17 00:00:00 2001 From: Thomas Chaumeny Date: Sat, 27 Sep 2014 12:41:54 +0200 Subject: Fixed #16731 -- Made pattern lookups work properly with F() expressions --- django/db/backends/postgresql_psycopg2/base.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'django/db/backends/postgresql_psycopg2/base.py') 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 -- cgit v1.3