def convert_domain(self, domain, tables, Model): transaction = Transaction() context = transaction.context database = transaction.database expression = super().convert_domain(domain, tables, Model) name, operator, value = domain if operator.endswith('ilike'): table, _ = tables[None] if self.translate: language = transaction.language model, join, column = self._get_translation_column( Model, name) column = Coalesce(NullIf(column, ''), self.sql_column(model)) else: language = None column = self.sql_column(table) column = self._domain_column(operator, column) threshold = context.get( '%s.%s.search_similarity' % (Model.__name__, name), context.get('search_similarity')) if database.has_similarity() and is_full_text(value) and threshold: sim_value = unescape_wildcard(value) sim_value = self._domain_value(operator, sim_value) expression = ( database.similarity(column, sim_value) >= threshold) if operator.startswith('not'): expression = Not(expression) if self.translate: expression = table.id.in_( join.select(model.id, where=expression)) key = '%s.%s.search_full_text' % (Model.__name__, name) if ((self.search_full_text or context.get(key)) and context.get(key, True) and database.has_search_full_text()): if context.get(key) or is_full_text(value): fts_column = database.format_full_text( column, language=language) fts_value = value if key not in context: fts_value = unescape_wildcard(fts_value) fts_value = self._domain_value(operator, fts_value) fts_value = database.format_full_text_query( fts_value, language=language) fts = database.search_full_text(fts_column, fts_value) if operator.startswith('not'): fts = Not(fts) if self.translate: fts = table.id.in_( join.select(model.id, where=fts)) if database.has_similarity() and is_full_text(value): if operator.startswith('not'): expression |= fts else: expression &= fts else: expression = fts return expression
def test_not(self): for not_ in [Not(self.table.c), ~self.table.c]: self.assertEqual(str(not_), '(NOT "c")') self.assertEqual(not_.params, ()) not_ = Not(Literal(False)) self.assertEqual(str(not_), '(NOT %s)') self.assertEqual(not_.params, (False,))
def convert_domain(self, domain, tables, Model): transaction = Transaction() database = transaction.database table, _ = tables[None] name, operator, value = domain assert name == self.name column = self.sql_column(table) column = self._domain_column(operator, column) if operator.endswith('like'): if database.has_search_full_text(): value = database.format_full_text_query( value, language=transaction.language) expression = database.search_full_text(column, value) else: expression = Literal(True) for v in value.split(): expression &= ILike(column, '%' + v + '%') if operator.startswith('not'): expression = Not(expression) else: Operator = SQL_OPERATORS[operator] column = self._rank_column(column, name, Model) expression = Operator(column, self._domain_value(operator, value)) if isinstance(expression, In) and not expression.right: expression = Literal(False) elif isinstance(expression, NotIn) and not expression.right: expression = Literal(True) expression = self._domain_add_null(column, operator, value, expression) return expression
def __invert__(self): from sql.operators import Not return Not(self)