Permalink
Browse files

Fixed #3575: use UPPER() instead ILIKE for postgres case-insensitive …

…comparisons.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8536 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 06d4976 commit 3df72660560730393fc958700d2a3ab975c4b361 @jacobian jacobian committed Aug 25, 2008
@@ -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',
@@ -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):
@@ -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':
@@ -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',
@@ -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):

0 comments on commit 3df7266

Please sign in to comment.