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
This commit is contained in:
Jacob Kaplan-Moss 2008-08-25 12:56:06 +00:00
parent 06d49768bd
commit 3df7266056
3 changed files with 20 additions and 12 deletions

View File

@ -70,9 +70,9 @@ class DatabaseFeatures(BaseDatabaseFeatures):
class DatabaseWrapper(BaseDatabaseWrapper): class DatabaseWrapper(BaseDatabaseWrapper):
operators = { operators = {
'exact': '= %s', 'exact': '= %s',
'iexact': 'ILIKE %s', 'iexact': '= UPPER(%s)',
'contains': 'LIKE %s', 'contains': 'LIKE %s',
'icontains': 'ILIKE %s', 'icontains': 'LIKE UPPER(%s)',
'regex': '~ %s', 'regex': '~ %s',
'iregex': '~* %s', 'iregex': '~* %s',
'gt': '> %s', 'gt': '> %s',
@ -81,8 +81,8 @@ class DatabaseWrapper(BaseDatabaseWrapper):
'lte': '<= %s', 'lte': '<= %s',
'startswith': 'LIKE %s', 'startswith': 'LIKE %s',
'endswith': 'LIKE %s', 'endswith': 'LIKE %s',
'istartswith': 'ILIKE %s', 'istartswith': 'LIKE UPPER(%s)',
'iendswith': 'ILIKE %s', 'iendswith': 'LIKE UPPER(%s)',
} }
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):

View File

@ -36,10 +36,18 @@ class DatabaseOperations(BaseDatabaseOperations):
return " DEFERRABLE INITIALLY DEFERRED" return " DEFERRABLE INITIALLY DEFERRED"
def lookup_cast(self, lookup_type): def lookup_cast(self, lookup_type):
if lookup_type in ('iexact', 'contains', 'icontains', 'startswith', 'istartswith', lookup = '%s'
'endswith', 'iendswith'):
return "%s::text" # Cast text lookups to text to allow things like filter(x__contains=4)
return "%s" 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): def field_cast_sql(self, db_type):
if db_type == 'inet': if db_type == 'inet':

View File

@ -40,9 +40,9 @@ class DatabaseOperations(PostgresqlDatabaseOperations):
class DatabaseWrapper(BaseDatabaseWrapper): class DatabaseWrapper(BaseDatabaseWrapper):
operators = { operators = {
'exact': '= %s', 'exact': '= %s',
'iexact': 'ILIKE %s', 'iexact': '= UPPER(%s)',
'contains': 'LIKE %s', 'contains': 'LIKE %s',
'icontains': 'ILIKE %s', 'icontains': 'LIKE UPPER(%s)',
'regex': '~ %s', 'regex': '~ %s',
'iregex': '~* %s', 'iregex': '~* %s',
'gt': '> %s', 'gt': '> %s',
@ -51,8 +51,8 @@ class DatabaseWrapper(BaseDatabaseWrapper):
'lte': '<= %s', 'lte': '<= %s',
'startswith': 'LIKE %s', 'startswith': 'LIKE %s',
'endswith': 'LIKE %s', 'endswith': 'LIKE %s',
'istartswith': 'ILIKE %s', 'istartswith': 'LIKE UPPER(%s)',
'iendswith': 'ILIKE %s', 'iendswith': 'LIKE UPPER(%s)',
} }
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):