2014-01-18 17:09:43 +08:00
|
|
|
import inspect
|
2015-01-28 20:35:27 +08:00
|
|
|
from copy import copy
|
2014-01-18 17:09:43 +08:00
|
|
|
|
|
|
|
from django.utils.functional import cached_property
|
2014-12-13 21:04:36 +08:00
|
|
|
from django.utils.six.moves import range
|
2014-01-18 17:09:43 +08:00
|
|
|
|
2014-09-14 18:34:41 +08:00
|
|
|
from .query_utils import QueryWrapper
|
|
|
|
|
2014-01-18 17:09:43 +08:00
|
|
|
|
|
|
|
class RegisterLookupMixin(object):
|
2014-03-02 03:21:57 +08:00
|
|
|
def _get_lookup(self, lookup_name):
|
2014-01-18 17:09:43 +08:00
|
|
|
try:
|
|
|
|
return self.class_lookups[lookup_name]
|
|
|
|
except KeyError:
|
2014-03-02 03:21:57 +08:00
|
|
|
# To allow for inheritance, check parent class' class_lookups.
|
2014-01-18 17:09:43 +08:00
|
|
|
for parent in inspect.getmro(self.__class__):
|
2014-03-31 03:11:05 +08:00
|
|
|
if 'class_lookups' not in parent.__dict__:
|
2014-01-18 17:09:43 +08:00
|
|
|
continue
|
|
|
|
if lookup_name in parent.class_lookups:
|
|
|
|
return parent.class_lookups[lookup_name]
|
|
|
|
except AttributeError:
|
|
|
|
# This class didn't have any class_lookups
|
|
|
|
pass
|
|
|
|
return None
|
|
|
|
|
2014-03-02 03:21:57 +08:00
|
|
|
def get_lookup(self, lookup_name):
|
|
|
|
found = self._get_lookup(lookup_name)
|
2014-06-17 23:57:16 +08:00
|
|
|
if found is None and hasattr(self, 'output_field'):
|
|
|
|
return self.output_field.get_lookup(lookup_name)
|
2014-03-02 03:21:57 +08:00
|
|
|
if found is not None and not issubclass(found, Lookup):
|
|
|
|
return None
|
|
|
|
return found
|
|
|
|
|
|
|
|
def get_transform(self, lookup_name):
|
|
|
|
found = self._get_lookup(lookup_name)
|
2014-06-17 23:57:16 +08:00
|
|
|
if found is None and hasattr(self, 'output_field'):
|
|
|
|
return self.output_field.get_transform(lookup_name)
|
2014-03-02 03:21:57 +08:00
|
|
|
if found is not None and not issubclass(found, Transform):
|
|
|
|
return None
|
|
|
|
return found
|
|
|
|
|
2014-01-18 17:09:43 +08:00
|
|
|
@classmethod
|
|
|
|
def register_lookup(cls, lookup):
|
2014-03-31 03:11:05 +08:00
|
|
|
if 'class_lookups' not in cls.__dict__:
|
2014-01-18 17:09:43 +08:00
|
|
|
cls.class_lookups = {}
|
|
|
|
cls.class_lookups[lookup.lookup_name] = lookup
|
2014-10-10 00:04:50 +08:00
|
|
|
return lookup
|
2014-01-18 17:09:43 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _unregister_lookup(cls, lookup):
|
|
|
|
"""
|
|
|
|
Removes given lookup from cls lookups. Meant to be used in
|
|
|
|
tests only.
|
|
|
|
"""
|
|
|
|
del cls.class_lookups[lookup.lookup_name]
|
|
|
|
|
|
|
|
|
|
|
|
class Transform(RegisterLookupMixin):
|
2014-09-14 18:34:41 +08:00
|
|
|
|
|
|
|
bilateral = False
|
|
|
|
|
2014-01-18 17:09:43 +08:00
|
|
|
def __init__(self, lhs, lookups):
|
|
|
|
self.lhs = lhs
|
|
|
|
self.init_lookups = lookups[:]
|
|
|
|
|
2014-11-16 09:56:42 +08:00
|
|
|
def as_sql(self, compiler, connection):
|
2014-01-18 17:09:43 +08:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@cached_property
|
2014-06-17 23:57:16 +08:00
|
|
|
def output_field(self):
|
|
|
|
return self.lhs.output_field
|
2014-01-18 17:09:43 +08:00
|
|
|
|
|
|
|
def relabeled_clone(self, relabels):
|
|
|
|
return self.__class__(self.lhs.relabeled_clone(relabels))
|
|
|
|
|
|
|
|
def get_group_by_cols(self):
|
|
|
|
return self.lhs.get_group_by_cols()
|
|
|
|
|
2014-11-15 19:04:02 +08:00
|
|
|
def get_bilateral_transforms(self):
|
|
|
|
if hasattr(self.lhs, 'get_bilateral_transforms'):
|
|
|
|
bilateral_transforms = self.lhs.get_bilateral_transforms()
|
|
|
|
else:
|
|
|
|
bilateral_transforms = []
|
|
|
|
if self.bilateral:
|
|
|
|
bilateral_transforms.append((self.__class__, self.init_lookups))
|
|
|
|
return bilateral_transforms
|
|
|
|
|
2014-12-23 21:16:56 +08:00
|
|
|
@cached_property
|
|
|
|
def contains_aggregate(self):
|
|
|
|
return self.lhs.contains_aggregate
|
|
|
|
|
2014-01-18 17:09:43 +08:00
|
|
|
|
|
|
|
class Lookup(RegisterLookupMixin):
|
|
|
|
lookup_name = None
|
|
|
|
|
2014-11-15 19:04:02 +08:00
|
|
|
def __init__(self, lhs, rhs):
|
2014-01-18 17:09:43 +08:00
|
|
|
self.lhs, self.rhs = lhs, rhs
|
|
|
|
self.rhs = self.get_prep_lookup()
|
2014-11-15 19:04:02 +08:00
|
|
|
if hasattr(self.lhs, 'get_bilateral_transforms'):
|
|
|
|
bilateral_transforms = self.lhs.get_bilateral_transforms()
|
|
|
|
else:
|
2014-09-14 18:34:41 +08:00
|
|
|
bilateral_transforms = []
|
|
|
|
if bilateral_transforms:
|
|
|
|
# We should warn the user as soon as possible if he is trying to apply
|
|
|
|
# a bilateral transformation on a nested QuerySet: that won't work.
|
|
|
|
# We need to import QuerySet here so as to avoid circular
|
|
|
|
from django.db.models.query import QuerySet
|
|
|
|
if isinstance(rhs, QuerySet):
|
|
|
|
raise NotImplementedError("Bilateral transformations on nested querysets are not supported.")
|
|
|
|
self.bilateral_transforms = bilateral_transforms
|
|
|
|
|
|
|
|
def apply_bilateral_transforms(self, value):
|
|
|
|
for transform, lookups in self.bilateral_transforms:
|
|
|
|
value = transform(value, lookups)
|
|
|
|
return value
|
|
|
|
|
2014-11-16 09:56:42 +08:00
|
|
|
def batch_process_rhs(self, compiler, connection, rhs=None):
|
2014-09-14 18:34:41 +08:00
|
|
|
if rhs is None:
|
|
|
|
rhs = self.rhs
|
|
|
|
if self.bilateral_transforms:
|
|
|
|
sqls, sqls_params = [], []
|
|
|
|
for p in rhs:
|
|
|
|
value = QueryWrapper('%s',
|
|
|
|
[self.lhs.output_field.get_db_prep_value(p, connection)])
|
|
|
|
value = self.apply_bilateral_transforms(value)
|
2014-11-16 09:56:42 +08:00
|
|
|
sql, sql_params = compiler.compile(value)
|
2014-09-14 18:34:41 +08:00
|
|
|
sqls.append(sql)
|
|
|
|
sqls_params.extend(sql_params)
|
|
|
|
else:
|
|
|
|
params = self.lhs.output_field.get_db_prep_lookup(
|
|
|
|
self.lookup_name, rhs, connection, prepared=True)
|
|
|
|
sqls, sqls_params = ['%s'] * len(params), params
|
|
|
|
return sqls, sqls_params
|
2014-01-18 17:09:43 +08:00
|
|
|
|
|
|
|
def get_prep_lookup(self):
|
2014-06-17 23:57:16 +08:00
|
|
|
return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
|
2014-01-18 17:09:43 +08:00
|
|
|
|
|
|
|
def get_db_prep_lookup(self, value, connection):
|
|
|
|
return (
|
2014-06-17 23:57:16 +08:00
|
|
|
'%s', self.lhs.output_field.get_db_prep_lookup(
|
2014-01-18 17:09:43 +08:00
|
|
|
self.lookup_name, value, connection, prepared=True))
|
|
|
|
|
2014-11-16 09:56:42 +08:00
|
|
|
def process_lhs(self, compiler, connection, lhs=None):
|
2014-01-18 17:09:43 +08:00
|
|
|
lhs = lhs or self.lhs
|
2014-11-16 09:56:42 +08:00
|
|
|
return compiler.compile(lhs)
|
2014-01-18 17:09:43 +08:00
|
|
|
|
2014-11-16 09:56:42 +08:00
|
|
|
def process_rhs(self, compiler, connection):
|
2014-01-19 17:30:26 +08:00
|
|
|
value = self.rhs
|
2014-09-14 18:34:41 +08:00
|
|
|
if self.bilateral_transforms:
|
|
|
|
if self.rhs_is_direct_value():
|
|
|
|
# Do not call get_db_prep_lookup here as the value will be
|
|
|
|
# transformed before being used for lookup
|
|
|
|
value = QueryWrapper("%s",
|
|
|
|
[self.lhs.output_field.get_db_prep_value(value, connection)])
|
|
|
|
value = self.apply_bilateral_transforms(value)
|
2014-01-18 17:09:43 +08:00
|
|
|
# Due to historical reasons there are a couple of different
|
|
|
|
# ways to produce sql here. get_compiler is likely a Query
|
|
|
|
# instance, _as_sql QuerySet and as_sql just something with
|
|
|
|
# as_sql. Finally the value can of course be just plain
|
|
|
|
# Python value.
|
|
|
|
if hasattr(value, 'get_compiler'):
|
|
|
|
value = value.get_compiler(connection=connection)
|
|
|
|
if hasattr(value, 'as_sql'):
|
2014-11-16 09:56:42 +08:00
|
|
|
sql, params = compiler.compile(value)
|
2014-01-18 17:09:43 +08:00
|
|
|
return '(' + sql + ')', params
|
|
|
|
if hasattr(value, '_as_sql'):
|
|
|
|
sql, params = value._as_sql(connection=connection)
|
|
|
|
return '(' + sql + ')', params
|
|
|
|
else:
|
|
|
|
return self.get_db_prep_lookup(value, connection)
|
|
|
|
|
2014-01-19 17:30:26 +08:00
|
|
|
def rhs_is_direct_value(self):
|
|
|
|
return not(
|
|
|
|
hasattr(self.rhs, 'as_sql') or
|
|
|
|
hasattr(self.rhs, '_as_sql') or
|
|
|
|
hasattr(self.rhs, 'get_compiler'))
|
|
|
|
|
2014-01-18 17:09:43 +08:00
|
|
|
def relabeled_clone(self, relabels):
|
|
|
|
new = copy(self)
|
|
|
|
new.lhs = new.lhs.relabeled_clone(relabels)
|
|
|
|
if hasattr(new.rhs, 'relabeled_clone'):
|
|
|
|
new.rhs = new.rhs.relabeled_clone(relabels)
|
|
|
|
return new
|
|
|
|
|
|
|
|
def get_group_by_cols(self):
|
|
|
|
cols = self.lhs.get_group_by_cols()
|
|
|
|
if hasattr(self.rhs, 'get_group_by_cols'):
|
|
|
|
cols.extend(self.rhs.get_group_by_cols())
|
|
|
|
return cols
|
|
|
|
|
2014-11-16 09:56:42 +08:00
|
|
|
def as_sql(self, compiler, connection):
|
2014-01-18 17:09:43 +08:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2014-12-23 21:16:56 +08:00
|
|
|
@cached_property
|
|
|
|
def contains_aggregate(self):
|
|
|
|
return self.lhs.contains_aggregate or getattr(self.rhs, 'contains_aggregate', False)
|
|
|
|
|
2014-01-18 17:09:43 +08:00
|
|
|
|
|
|
|
class BuiltinLookup(Lookup):
|
2014-11-16 09:56:42 +08:00
|
|
|
def process_lhs(self, compiler, connection, lhs=None):
|
2014-01-20 10:38:46 +08:00
|
|
|
lhs_sql, params = super(BuiltinLookup, self).process_lhs(
|
2014-11-16 09:56:42 +08:00
|
|
|
compiler, connection, lhs)
|
2014-06-17 23:57:16 +08:00
|
|
|
field_internal_type = self.lhs.output_field.get_internal_type()
|
|
|
|
db_type = self.lhs.output_field.db_type(connection=connection)
|
2014-01-20 10:38:46 +08:00
|
|
|
lhs_sql = connection.ops.field_cast_sql(
|
|
|
|
db_type, field_internal_type) % lhs_sql
|
2015-01-11 02:13:28 +08:00
|
|
|
lhs_sql = connection.ops.lookup_cast(self.lookup_name, field_internal_type) % lhs_sql
|
2014-01-20 10:38:46 +08:00
|
|
|
return lhs_sql, params
|
|
|
|
|
2014-11-16 09:56:42 +08:00
|
|
|
def as_sql(self, compiler, connection):
|
|
|
|
lhs_sql, params = self.process_lhs(compiler, connection)
|
|
|
|
rhs_sql, rhs_params = self.process_rhs(compiler, connection)
|
2014-01-18 17:09:43 +08:00
|
|
|
params.extend(rhs_params)
|
2014-01-20 10:38:46 +08:00
|
|
|
rhs_sql = self.get_rhs_op(connection, rhs_sql)
|
|
|
|
return '%s %s' % (lhs_sql, rhs_sql), params
|
2014-01-18 17:09:43 +08:00
|
|
|
|
|
|
|
def get_rhs_op(self, connection, rhs):
|
|
|
|
return connection.operators[self.lookup_name] % rhs
|
|
|
|
|
|
|
|
|
|
|
|
default_lookups = {}
|
|
|
|
|
|
|
|
|
|
|
|
class Exact(BuiltinLookup):
|
|
|
|
lookup_name = 'exact'
|
|
|
|
default_lookups['exact'] = Exact
|
|
|
|
|
|
|
|
|
|
|
|
class IExact(BuiltinLookup):
|
|
|
|
lookup_name = 'iexact'
|
2015-01-11 00:11:15 +08:00
|
|
|
|
|
|
|
def process_rhs(self, qn, connection):
|
|
|
|
rhs, params = super(IExact, self).process_rhs(qn, connection)
|
|
|
|
if params:
|
|
|
|
params[0] = connection.ops.prep_for_iexact_query(params[0])
|
|
|
|
return rhs, params
|
|
|
|
|
|
|
|
|
2014-01-18 17:09:43 +08:00
|
|
|
default_lookups['iexact'] = IExact
|
|
|
|
|
|
|
|
|
|
|
|
class GreaterThan(BuiltinLookup):
|
|
|
|
lookup_name = 'gt'
|
|
|
|
default_lookups['gt'] = GreaterThan
|
|
|
|
|
|
|
|
|
|
|
|
class GreaterThanOrEqual(BuiltinLookup):
|
|
|
|
lookup_name = 'gte'
|
|
|
|
default_lookups['gte'] = GreaterThanOrEqual
|
|
|
|
|
|
|
|
|
|
|
|
class LessThan(BuiltinLookup):
|
|
|
|
lookup_name = 'lt'
|
|
|
|
default_lookups['lt'] = LessThan
|
|
|
|
|
|
|
|
|
|
|
|
class LessThanOrEqual(BuiltinLookup):
|
|
|
|
lookup_name = 'lte'
|
|
|
|
default_lookups['lte'] = LessThanOrEqual
|
|
|
|
|
|
|
|
|
|
|
|
class In(BuiltinLookup):
|
|
|
|
lookup_name = 'in'
|
|
|
|
|
2014-11-16 09:56:42 +08:00
|
|
|
def process_rhs(self, compiler, connection):
|
2014-09-14 18:34:41 +08:00
|
|
|
if self.rhs_is_direct_value():
|
|
|
|
# rhs should be an iterable, we use batch_process_rhs
|
|
|
|
# to prepare/transform those values
|
|
|
|
rhs = list(self.rhs)
|
|
|
|
if not rhs:
|
|
|
|
from django.db.models.sql.datastructures import EmptyResultSet
|
|
|
|
raise EmptyResultSet
|
2014-11-16 09:56:42 +08:00
|
|
|
sqls, sqls_params = self.batch_process_rhs(compiler, connection, rhs)
|
2014-09-14 18:34:41 +08:00
|
|
|
placeholder = '(' + ', '.join(sqls) + ')'
|
|
|
|
return (placeholder, sqls_params)
|
|
|
|
else:
|
2014-11-16 09:56:42 +08:00
|
|
|
return super(In, self).process_rhs(compiler, connection)
|
2014-01-18 17:09:43 +08:00
|
|
|
|
|
|
|
def get_rhs_op(self, connection, rhs):
|
|
|
|
return 'IN %s' % rhs
|
2014-01-19 17:30:26 +08:00
|
|
|
|
2014-11-16 09:56:42 +08:00
|
|
|
def as_sql(self, compiler, connection):
|
2014-01-19 17:30:26 +08:00
|
|
|
max_in_list_size = connection.ops.max_in_list_size()
|
|
|
|
if self.rhs_is_direct_value() and (max_in_list_size and
|
|
|
|
len(self.rhs) > max_in_list_size):
|
2014-09-14 18:34:41 +08:00
|
|
|
# This is a special case for Oracle which limits the number of elements
|
|
|
|
# which can appear in an 'IN' clause.
|
2014-11-16 09:56:42 +08:00
|
|
|
lhs, lhs_params = self.process_lhs(compiler, connection)
|
|
|
|
rhs, rhs_params = self.batch_process_rhs(compiler, connection)
|
2014-01-19 17:30:26 +08:00
|
|
|
in_clause_elements = ['(']
|
|
|
|
params = []
|
2014-12-13 21:04:36 +08:00
|
|
|
for offset in range(0, len(rhs_params), max_in_list_size):
|
2014-01-19 17:30:26 +08:00
|
|
|
if offset > 0:
|
|
|
|
in_clause_elements.append(' OR ')
|
|
|
|
in_clause_elements.append('%s IN (' % lhs)
|
|
|
|
params.extend(lhs_params)
|
2014-09-14 18:34:41 +08:00
|
|
|
sqls = rhs[offset: offset + max_in_list_size]
|
|
|
|
sqls_params = rhs_params[offset: offset + max_in_list_size]
|
|
|
|
param_group = ', '.join(sqls)
|
2014-01-19 17:30:26 +08:00
|
|
|
in_clause_elements.append(param_group)
|
|
|
|
in_clause_elements.append(')')
|
2014-09-14 18:34:41 +08:00
|
|
|
params.extend(sqls_params)
|
2014-01-19 17:30:26 +08:00
|
|
|
in_clause_elements.append(')')
|
|
|
|
return ''.join(in_clause_elements), params
|
|
|
|
else:
|
2014-11-16 09:56:42 +08:00
|
|
|
return super(In, self).as_sql(compiler, connection)
|
2014-01-19 17:30:26 +08:00
|
|
|
|
|
|
|
|
2014-01-18 17:09:43 +08:00
|
|
|
default_lookups['in'] = In
|
|
|
|
|
|
|
|
|
|
|
|
class PatternLookup(BuiltinLookup):
|
2014-09-27 18:41:54 +08:00
|
|
|
|
2014-01-18 17:09:43 +08:00
|
|
|
def get_rhs_op(self, connection, rhs):
|
|
|
|
# Assume we are in startswith. We need to produce SQL like:
|
|
|
|
# col LIKE %s, ['thevalue%']
|
|
|
|
# For python values we can (and should) do that directly in Python,
|
|
|
|
# but if the value is for example reference to other column, then
|
|
|
|
# we need to add the % pattern match to the lookup by something like
|
|
|
|
# col LIKE othercol || '%%'
|
|
|
|
# So, for Python values we don't need any special pattern, but for
|
2014-09-14 18:34:41 +08:00
|
|
|
# SQL reference values or SQL transformations we need the correct
|
|
|
|
# pattern added.
|
|
|
|
if (hasattr(self.rhs, 'get_compiler') or hasattr(self.rhs, 'as_sql')
|
|
|
|
or hasattr(self.rhs, '_as_sql') or self.bilateral_transforms):
|
2014-09-27 18:41:54 +08:00
|
|
|
pattern = connection.pattern_ops[self.lookup_name].format(connection.pattern_esc)
|
|
|
|
return pattern.format(rhs)
|
2014-01-18 17:09:43 +08:00
|
|
|
else:
|
|
|
|
return super(PatternLookup, self).get_rhs_op(connection, rhs)
|
|
|
|
|
|
|
|
|
2014-09-27 18:41:54 +08:00
|
|
|
class Contains(PatternLookup):
|
|
|
|
lookup_name = 'contains'
|
2015-01-11 00:11:15 +08:00
|
|
|
|
|
|
|
def process_rhs(self, qn, connection):
|
|
|
|
rhs, params = super(Contains, self).process_rhs(qn, connection)
|
|
|
|
if params and not self.bilateral_transforms:
|
|
|
|
params[0] = "%%%s%%" % connection.ops.prep_for_like_query(params[0])
|
|
|
|
return rhs, params
|
|
|
|
|
|
|
|
|
2014-09-27 18:41:54 +08:00
|
|
|
default_lookups['contains'] = Contains
|
|
|
|
|
|
|
|
|
2015-01-11 00:11:15 +08:00
|
|
|
class IContains(Contains):
|
2014-09-27 18:41:54 +08:00
|
|
|
lookup_name = 'icontains'
|
2015-01-11 00:11:15 +08:00
|
|
|
|
|
|
|
|
2014-09-27 18:41:54 +08:00
|
|
|
default_lookups['icontains'] = IContains
|
|
|
|
|
|
|
|
|
2014-01-18 17:09:43 +08:00
|
|
|
class StartsWith(PatternLookup):
|
|
|
|
lookup_name = 'startswith'
|
2015-01-11 00:11:15 +08:00
|
|
|
|
|
|
|
def process_rhs(self, qn, connection):
|
|
|
|
rhs, params = super(StartsWith, self).process_rhs(qn, connection)
|
|
|
|
if params and not self.bilateral_transforms:
|
|
|
|
params[0] = "%s%%" % connection.ops.prep_for_like_query(params[0])
|
|
|
|
return rhs, params
|
|
|
|
|
|
|
|
|
2014-01-18 17:09:43 +08:00
|
|
|
default_lookups['startswith'] = StartsWith
|
|
|
|
|
|
|
|
|
|
|
|
class IStartsWith(PatternLookup):
|
|
|
|
lookup_name = 'istartswith'
|
2015-01-11 00:11:15 +08:00
|
|
|
|
|
|
|
def process_rhs(self, qn, connection):
|
|
|
|
rhs, params = super(IStartsWith, self).process_rhs(qn, connection)
|
|
|
|
if params and not self.bilateral_transforms:
|
|
|
|
params[0] = "%s%%" % connection.ops.prep_for_like_query(params[0])
|
|
|
|
return rhs, params
|
|
|
|
|
|
|
|
|
2014-01-18 17:09:43 +08:00
|
|
|
default_lookups['istartswith'] = IStartsWith
|
|
|
|
|
|
|
|
|
2014-09-27 18:41:54 +08:00
|
|
|
class EndsWith(PatternLookup):
|
2014-01-18 17:09:43 +08:00
|
|
|
lookup_name = 'endswith'
|
2015-01-11 00:11:15 +08:00
|
|
|
|
|
|
|
def process_rhs(self, qn, connection):
|
|
|
|
rhs, params = super(EndsWith, self).process_rhs(qn, connection)
|
|
|
|
if params and not self.bilateral_transforms:
|
|
|
|
params[0] = "%%%s" % connection.ops.prep_for_like_query(params[0])
|
|
|
|
return rhs, params
|
|
|
|
|
|
|
|
|
2014-01-18 17:09:43 +08:00
|
|
|
default_lookups['endswith'] = EndsWith
|
|
|
|
|
|
|
|
|
2014-09-27 18:41:54 +08:00
|
|
|
class IEndsWith(PatternLookup):
|
2014-01-18 17:09:43 +08:00
|
|
|
lookup_name = 'iendswith'
|
2015-01-11 00:11:15 +08:00
|
|
|
|
|
|
|
def process_rhs(self, qn, connection):
|
|
|
|
rhs, params = super(IEndsWith, self).process_rhs(qn, connection)
|
|
|
|
if params and not self.bilateral_transforms:
|
|
|
|
params[0] = "%%%s" % connection.ops.prep_for_like_query(params[0])
|
|
|
|
return rhs, params
|
|
|
|
|
|
|
|
|
2014-01-18 17:09:43 +08:00
|
|
|
default_lookups['iendswith'] = IEndsWith
|
|
|
|
|
|
|
|
|
|
|
|
class Between(BuiltinLookup):
|
|
|
|
def get_rhs_op(self, connection, rhs):
|
|
|
|
return "BETWEEN %s AND %s" % (rhs, rhs)
|
|
|
|
|
|
|
|
|
2014-09-14 18:34:41 +08:00
|
|
|
class Range(BuiltinLookup):
|
2014-01-18 17:09:43 +08:00
|
|
|
lookup_name = 'range'
|
2014-09-14 18:34:41 +08:00
|
|
|
|
|
|
|
def get_rhs_op(self, connection, rhs):
|
|
|
|
return "BETWEEN %s AND %s" % (rhs[0], rhs[1])
|
|
|
|
|
2014-11-16 09:56:42 +08:00
|
|
|
def process_rhs(self, compiler, connection):
|
2014-09-14 18:34:41 +08:00
|
|
|
if self.rhs_is_direct_value():
|
|
|
|
# rhs should be an iterable of 2 values, we use batch_process_rhs
|
|
|
|
# to prepare/transform those values
|
2014-11-16 09:56:42 +08:00
|
|
|
return self.batch_process_rhs(compiler, connection)
|
2014-09-14 18:34:41 +08:00
|
|
|
else:
|
2014-11-16 09:56:42 +08:00
|
|
|
return super(Range, self).process_rhs(compiler, connection)
|
2014-09-14 18:34:41 +08:00
|
|
|
|
2014-01-18 17:09:43 +08:00
|
|
|
default_lookups['range'] = Range
|
|
|
|
|
|
|
|
|
|
|
|
class IsNull(BuiltinLookup):
|
|
|
|
lookup_name = 'isnull'
|
|
|
|
|
2014-11-16 09:56:42 +08:00
|
|
|
def as_sql(self, compiler, connection):
|
|
|
|
sql, params = compiler.compile(self.lhs)
|
2014-01-18 17:09:43 +08:00
|
|
|
if self.rhs:
|
|
|
|
return "%s IS NULL" % sql, params
|
|
|
|
else:
|
|
|
|
return "%s IS NOT NULL" % sql, params
|
|
|
|
default_lookups['isnull'] = IsNull
|
|
|
|
|
|
|
|
|
|
|
|
class Search(BuiltinLookup):
|
|
|
|
lookup_name = 'search'
|
2014-04-23 04:01:16 +08:00
|
|
|
|
2014-11-16 09:56:42 +08:00
|
|
|
def as_sql(self, compiler, connection):
|
|
|
|
lhs, lhs_params = self.process_lhs(compiler, connection)
|
|
|
|
rhs, rhs_params = self.process_rhs(compiler, connection)
|
2014-04-23 04:01:16 +08:00
|
|
|
sql_template = connection.ops.fulltext_search_sql(field_name=lhs)
|
|
|
|
return sql_template, lhs_params + rhs_params
|
|
|
|
|
2014-01-18 17:09:43 +08:00
|
|
|
default_lookups['search'] = Search
|
|
|
|
|
|
|
|
|
|
|
|
class Regex(BuiltinLookup):
|
|
|
|
lookup_name = 'regex'
|
2014-01-20 10:38:46 +08:00
|
|
|
|
2014-11-16 09:56:42 +08:00
|
|
|
def as_sql(self, compiler, connection):
|
2014-01-20 10:38:46 +08:00
|
|
|
if self.lookup_name in connection.operators:
|
2014-11-16 09:56:42 +08:00
|
|
|
return super(Regex, self).as_sql(compiler, connection)
|
2014-01-20 10:38:46 +08:00
|
|
|
else:
|
2014-11-16 09:56:42 +08:00
|
|
|
lhs, lhs_params = self.process_lhs(compiler, connection)
|
|
|
|
rhs, rhs_params = self.process_rhs(compiler, connection)
|
2014-01-20 10:38:46 +08:00
|
|
|
sql_template = connection.ops.regex_lookup(self.lookup_name)
|
|
|
|
return sql_template % (lhs, rhs), lhs_params + rhs_params
|
2014-01-18 17:09:43 +08:00
|
|
|
default_lookups['regex'] = Regex
|
|
|
|
|
|
|
|
|
2014-01-20 10:38:46 +08:00
|
|
|
class IRegex(Regex):
|
2014-01-18 17:09:43 +08:00
|
|
|
lookup_name = 'iregex'
|
|
|
|
default_lookups['iregex'] = IRegex
|