| 1 |
from django.db.models import Transform |
| 2 |
from django.db.models.lookups import PostgresOperatorLookup |
| 3 |
from django.db.models.sql.query import Query |
| 4 |
|
| 5 |
from .search import SearchVector, SearchVectorExact, SearchVectorField |
| 6 |
|
| 7 |
|
| 8 |
class DataContains(PostgresOperatorLookup): |
| 9 |
lookup_name = "contains" |
| 10 |
postgres_operator = "@>" |
| 11 |
|
| 12 |
|
| 13 |
class ContainedBy(PostgresOperatorLookup): |
| 14 |
lookup_name = "contained_by" |
| 15 |
postgres_operator = "<@" |
| 16 |
|
| 17 |
|
| 18 |
class Overlap(PostgresOperatorLookup): |
| 19 |
lookup_name = "overlap" |
| 20 |
postgres_operator = "&&" |
| 21 |
|
| 22 |
def get_prep_lookup(self): |
| 23 |
from .expressions import ArraySubquery |
| 24 |
|
| 25 |
if isinstance(self.rhs, Query): |
| 26 |
self.rhs = ArraySubquery(self.rhs) |
| 27 |
return super().get_prep_lookup() |
| 28 |
|
| 29 |
|
| 30 |
class HasKey(PostgresOperatorLookup): |
| 31 |
lookup_name = "has_key" |
| 32 |
postgres_operator = "?" |
| 33 |
prepare_rhs = False |
| 34 |
|
| 35 |
|
| 36 |
class HasKeys(PostgresOperatorLookup): |
| 37 |
lookup_name = "has_keys" |
| 38 |
postgres_operator = "?&" |
| 39 |
|
| 40 |
def get_prep_lookup(self): |
| 41 |
return [str(item) for item in self.rhs] |
| 42 |
|
| 43 |
|
| 44 |
class HasAnyKeys(HasKeys): |
| 45 |
lookup_name = "has_any_keys" |
| 46 |
postgres_operator = "?|" |
| 47 |
|
| 48 |
|
| 49 |
class Unaccent(Transform): |
| 50 |
bilateral = True |
| 51 |
lookup_name = "unaccent" |
| 52 |
function = "UNACCENT" |
| 53 |
|
| 54 |
|
| 55 |
class SearchLookup(SearchVectorExact): |
| 56 |
lookup_name = "search" |
| 57 |
|
| 58 |
def process_lhs(self, qn, connection): |
| 59 |
if not isinstance(self.lhs.output_field, SearchVectorField): |
| 60 |
config = getattr(self.rhs, "config", None) |
| 61 |
self.lhs = SearchVector(self.lhs, config=config) |
| 62 |
lhs, lhs_params = super().process_lhs(qn, connection) |
| 63 |
return lhs, lhs_params |
| 64 |
|
| 65 |
|
| 66 |
class TrigramSimilar(PostgresOperatorLookup): |
| 67 |
lookup_name = "trigram_similar" |
| 68 |
postgres_operator = "%%" |
| 69 |
|
| 70 |
|
| 71 |
class TrigramWordSimilar(PostgresOperatorLookup): |
| 72 |
lookup_name = "trigram_word_similar" |
| 73 |
postgres_operator = "%%>" |
| 74 |
|
| 75 |
|
| 76 |
class TrigramStrictWordSimilar(PostgresOperatorLookup): |
| 77 |
lookup_name = "trigram_strict_word_similar" |
| 78 |
postgres_operator = "%%>>" |