Fixed #800 -- Fixed bug in treatement of underscores and percent signs in SQLite backend. In order to do this, changed OPERATOR_MAPPING in DB backends to include a format string of the field value, because ESCAPE comes after the field value.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1326 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-11-21 02:46:15 +00:00
parent 10ca90a2fd
commit f1a8869339
5 changed files with 56 additions and 53 deletions

View File

@ -116,19 +116,19 @@ def get_relations(cursor, table_name):
raise NotImplementedError raise NotImplementedError
OPERATOR_MAPPING = { OPERATOR_MAPPING = {
'exact': '=', 'exact': '= %s',
'iexact': 'LIKE', 'iexact': 'LIKE %s',
'contains': 'LIKE', 'contains': 'LIKE %s',
'icontains': 'LIKE', 'icontains': 'LIKE %s',
'ne': '!=', 'ne': '!= %s',
'gt': '>', 'gt': '> %s',
'gte': '>=', 'gte': '>= %s',
'lt': '<', 'lt': '< %s',
'lte': '<=', 'lte': '<= %s',
'startswith': 'LIKE', 'startswith': 'LIKE %s',
'endswith': 'LIKE', 'endswith': 'LIKE %s',
'istartswith': 'LIKE', 'istartswith': 'LIKE %s',
'iendswith': 'LIKE', 'iendswith': 'LIKE %s',
} }
DATA_TYPES = { DATA_TYPES = {

View File

@ -128,19 +128,19 @@ def get_relations(cursor, table_name):
raise NotImplementedError raise NotImplementedError
OPERATOR_MAPPING = { OPERATOR_MAPPING = {
'exact': '=', 'exact': '= %s',
'iexact': 'LIKE', 'iexact': 'LIKE %s',
'contains': 'LIKE BINARY', 'contains': 'LIKE BINARY %s',
'icontains': 'LIKE', 'icontains': 'LIKE %s',
'ne': '!=', 'ne': '!= %s',
'gt': '>', 'gt': '> %s',
'gte': '>=', 'gte': '>= %s',
'lt': '<', 'lt': '< %s',
'lte': '<=', 'lte': '<= %s',
'startswith': 'LIKE BINARY', 'startswith': 'LIKE BINARY %s',
'endswith': 'LIKE BINARY', 'endswith': 'LIKE BINARY %s',
'istartswith': 'LIKE', 'istartswith': 'LIKE %s',
'iendswith': 'LIKE', 'iendswith': 'LIKE %s',
} }
# This dictionary maps Field objects to their associated MySQL column # This dictionary maps Field objects to their associated MySQL column

View File

@ -133,19 +133,19 @@ Database.register_type(Database.new_type((1114,1184), "TIMESTAMP", typecasts.typ
Database.register_type(Database.new_type((16,), "BOOLEAN", typecasts.typecast_boolean)) Database.register_type(Database.new_type((16,), "BOOLEAN", typecasts.typecast_boolean))
OPERATOR_MAPPING = { OPERATOR_MAPPING = {
'exact': '=', 'exact': '= %s',
'iexact': 'ILIKE', 'iexact': 'ILIKE %s',
'contains': 'LIKE', 'contains': 'LIKE %s',
'icontains': 'ILIKE', 'icontains': 'ILIKE %s',
'ne': '!=', 'ne': '!= %s',
'gt': '>', 'gt': '> %s',
'gte': '>=', 'gte': '>= %s',
'lt': '<', 'lt': '< %s',
'lte': '<=', 'lte': '<= %s',
'startswith': 'LIKE', 'startswith': 'LIKE %s',
'endswith': 'LIKE', 'endswith': 'LIKE %s',
'istartswith': 'ILIKE', 'istartswith': 'ILIKE %s',
'iendswith': 'ILIKE', 'iendswith': 'ILIKE %s',
} }
# This dictionary maps Field objects to their associated PostgreSQL column # This dictionary maps Field objects to their associated PostgreSQL column

View File

@ -131,20 +131,23 @@ def get_relations(cursor, table_name):
# Operators and fields ######################################################## # Operators and fields ########################################################
# SQLite requires LIKE statements to include an ESCAPE clause if the value
# being escaped has a percent or underscore in it.
# See http://www.sqlite.org/lang_expr.html for an explanation.
OPERATOR_MAPPING = { OPERATOR_MAPPING = {
'exact': '=', 'exact': '= %s',
'iexact': 'LIKE', 'iexact': "LIKE %s ESCAPE '\\'",
'contains': 'LIKE', 'contains': "LIKE %s ESCAPE '\\'",
'icontains': 'LIKE', 'icontains': "LIKE %s ESCAPE '\\'",
'ne': '!=', 'ne': '!= %s',
'gt': '>', 'gt': '> %s',
'gte': '>=', 'gte': '>= %s',
'lt': '<', 'lt': '< %s',
'lte': '<=', 'lte': '<= %s',
'startswith': 'LIKE', 'startswith': "LIKE %s ESCAPE '\\'",
'endswith': 'LIKE', 'endswith': "LIKE %s ESCAPE '\\'",
'istartswith': 'LIKE', 'istartswith': "LIKE %s ESCAPE '\\'",
'iendswith': 'LIKE', 'iendswith': "LIKE %s ESCAPE '\\'",
} }
# SQLite doesn't actually support most of these types, but it "does the right # SQLite doesn't actually support most of these types, but it "does the right

View File

@ -1127,7 +1127,7 @@ def _get_where_clause(lookup_type, table_prefix, field_name, value):
table_prefix = db.db.quote_name(table_prefix[:-1])+'.' table_prefix = db.db.quote_name(table_prefix[:-1])+'.'
field_name = db.db.quote_name(field_name) field_name = db.db.quote_name(field_name)
try: try:
return '%s%s %s %%s' % (table_prefix, field_name, db.OPERATOR_MAPPING[lookup_type]) return '%s%s %s' % (table_prefix, field_name, (db.OPERATOR_MAPPING[lookup_type] % '%s'))
except KeyError: except KeyError:
pass pass
if lookup_type == 'in': if lookup_type == 'in':