2015-01-11 00:11:15 +08:00
|
|
|
from django.db.models import Lookup, Transform
|
2016-04-24 01:13:31 +08:00
|
|
|
from django.utils.encoding import force_text
|
2014-09-06 04:53:11 +08:00
|
|
|
|
2015-06-01 05:45:03 +08:00
|
|
|
from .search import SearchVector, SearchVectorExact, SearchVectorField
|
|
|
|
|
2014-09-06 04:53:11 +08:00
|
|
|
|
2015-01-11 00:11:15 +08:00
|
|
|
class PostgresSimpleLookup(Lookup):
|
|
|
|
def as_sql(self, qn, connection):
|
|
|
|
lhs, lhs_params = self.process_lhs(qn, connection)
|
|
|
|
rhs, rhs_params = self.process_rhs(qn, connection)
|
|
|
|
params = lhs_params + rhs_params
|
|
|
|
return '%s %s %s' % (lhs, self.operator, rhs), params
|
|
|
|
|
|
|
|
|
|
|
|
class DataContains(PostgresSimpleLookup):
|
|
|
|
lookup_name = 'contains'
|
|
|
|
operator = '@>'
|
|
|
|
|
|
|
|
|
|
|
|
class ContainedBy(PostgresSimpleLookup):
|
|
|
|
lookup_name = 'contained_by'
|
|
|
|
operator = '<@'
|
|
|
|
|
|
|
|
|
|
|
|
class Overlap(PostgresSimpleLookup):
|
|
|
|
lookup_name = 'overlap'
|
|
|
|
operator = '&&'
|
|
|
|
|
|
|
|
|
2015-05-31 04:22:36 +08:00
|
|
|
class HasKey(PostgresSimpleLookup):
|
|
|
|
lookup_name = 'has_key'
|
|
|
|
operator = '?'
|
2016-04-24 01:13:31 +08:00
|
|
|
prepare_rhs = False
|
2015-05-31 04:22:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
class HasKeys(PostgresSimpleLookup):
|
|
|
|
lookup_name = 'has_keys'
|
|
|
|
operator = '?&'
|
|
|
|
|
2016-04-24 01:13:31 +08:00
|
|
|
def get_prep_lookup(self):
|
|
|
|
return [force_text(item) for item in self.rhs]
|
2015-05-31 04:22:36 +08:00
|
|
|
|
2016-04-24 01:13:31 +08:00
|
|
|
|
|
|
|
class HasAnyKeys(HasKeys):
|
2015-05-31 04:22:36 +08:00
|
|
|
lookup_name = 'has_any_keys'
|
|
|
|
operator = '?|'
|
|
|
|
|
|
|
|
|
2015-08-03 10:30:06 +08:00
|
|
|
class Unaccent(Transform):
|
2014-09-06 04:53:11 +08:00
|
|
|
bilateral = True
|
|
|
|
lookup_name = 'unaccent'
|
2015-01-11 00:11:15 +08:00
|
|
|
function = 'UNACCENT'
|
2015-06-01 05:45:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
class SearchLookup(SearchVectorExact):
|
|
|
|
lookup_name = 'search'
|
|
|
|
|
|
|
|
def process_lhs(self, qn, connection):
|
|
|
|
if not isinstance(self.lhs.output_field, SearchVectorField):
|
|
|
|
self.lhs = SearchVector(self.lhs)
|
|
|
|
lhs, lhs_params = super(SearchLookup, self).process_lhs(qn, connection)
|
|
|
|
return lhs, lhs_params
|
2015-06-06 00:37:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TrigramSimilar(PostgresSimpleLookup):
|
|
|
|
lookup_name = 'trigram_similar'
|
|
|
|
operator = '%%'
|