Skip to content

Commit

Permalink
Fixed #3575: use UPPER() instead ILIKE for postgres case-insensitive …
Browse files Browse the repository at this point in the history
…comparisons.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8536 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jacobian committed Aug 25, 2008
1 parent 06d4976 commit 3df7266
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
8 changes: 4 additions & 4 deletions django/db/backends/postgresql/base.py
Expand Up @@ -70,9 +70,9 @@ class DatabaseFeatures(BaseDatabaseFeatures):
class DatabaseWrapper(BaseDatabaseWrapper):
operators = {
'exact': '= %s',
'iexact': 'ILIKE %s',
'iexact': '= UPPER(%s)',
'contains': 'LIKE %s',
'icontains': 'ILIKE %s',
'icontains': 'LIKE UPPER(%s)',
'regex': '~ %s',
'iregex': '~* %s',
'gt': '> %s',
Expand All @@ -81,8 +81,8 @@ class DatabaseWrapper(BaseDatabaseWrapper):
'lte': '<= %s',
'startswith': 'LIKE %s',
'endswith': 'LIKE %s',
'istartswith': 'ILIKE %s',
'iendswith': 'ILIKE %s',
'istartswith': 'LIKE UPPER(%s)',
'iendswith': 'LIKE UPPER(%s)',
}

def __init__(self, *args, **kwargs):
Expand Down
16 changes: 12 additions & 4 deletions django/db/backends/postgresql/operations.py
Expand Up @@ -36,10 +36,18 @@ def deferrable_sql(self):
return " DEFERRABLE INITIALLY DEFERRED"

def lookup_cast(self, lookup_type):
if lookup_type in ('iexact', 'contains', 'icontains', 'startswith', 'istartswith',
'endswith', 'iendswith'):
return "%s::text"
return "%s"
lookup = '%s'

# Cast text lookups to text to allow things like filter(x__contains=4)
if lookup_type in ('iexact', 'contains', 'icontains', 'startswith',
'istartswith', 'endswith', 'iendswith'):
lookup = "%s::text"

# Use UPPER(x) for case-insensitive lookups; it's faster.
if lookup_type in ('iexact', 'icontains', 'istartswith', 'iendswith'):
lookup = 'UPPER(%s)' % lookup

return lookup

def field_cast_sql(self, db_type):
if db_type == 'inet':
Expand Down
8 changes: 4 additions & 4 deletions django/db/backends/postgresql_psycopg2/base.py
Expand Up @@ -40,9 +40,9 @@ def last_executed_query(self, cursor, sql, params):
class DatabaseWrapper(BaseDatabaseWrapper):
operators = {
'exact': '= %s',
'iexact': 'ILIKE %s',
'iexact': '= UPPER(%s)',
'contains': 'LIKE %s',
'icontains': 'ILIKE %s',
'icontains': 'LIKE UPPER(%s)',
'regex': '~ %s',
'iregex': '~* %s',
'gt': '> %s',
Expand All @@ -51,8 +51,8 @@ class DatabaseWrapper(BaseDatabaseWrapper):
'lte': '<= %s',
'startswith': 'LIKE %s',
'endswith': 'LIKE %s',
'istartswith': 'ILIKE %s',
'iendswith': 'ILIKE %s',
'istartswith': 'LIKE UPPER(%s)',
'iendswith': 'LIKE UPPER(%s)',
}

def __init__(self, *args, **kwargs):
Expand Down

0 comments on commit 3df7266

Please sign in to comment.