2015-01-11 00:11:15 +08:00
|
|
|
from django.db.models import Lookup, Transform
|
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 FunctionTransform(Transform):
|
|
|
|
def as_sql(self, qn, connection):
|
|
|
|
lhs, params = qn.compile(self.lhs)
|
|
|
|
return "%s(%s)" % (self.function, lhs), 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 = '?'
|
|
|
|
|
|
|
|
|
|
|
|
class HasKeys(PostgresSimpleLookup):
|
|
|
|
lookup_name = 'has_keys'
|
|
|
|
operator = '?&'
|
|
|
|
|
|
|
|
|
|
|
|
class HasAnyKeys(PostgresSimpleLookup):
|
|
|
|
lookup_name = 'has_any_keys'
|
|
|
|
operator = '?|'
|
|
|
|
|
|
|
|
|
2015-01-11 00:11:15 +08:00
|
|
|
class Unaccent(FunctionTransform):
|
2014-09-06 04:53:11 +08:00
|
|
|
bilateral = True
|
|
|
|
lookup_name = 'unaccent'
|
2015-01-11 00:11:15 +08:00
|
|
|
function = 'UNACCENT'
|