def convert_domain(self, domain, tables, Model): from trytond.ir.lang import get_parent_language pool = Pool() Translation = pool.get('ir.translation') IrModel = pool.get('ir.model') if not self.translate: return super(FieldTranslate, self).convert_domain(domain, tables, Model) table = join = Model.__table__() model = IrModel.__table__() name, operator, value = domain language = Transaction().language column = None while language: translation = Translation.__table__() join = self._get_translation_join(Model, name, translation, model, table, join, language) column = Coalesce(NullIf(column, ''), translation.value) language = get_parent_language(language) column = Coalesce(NullIf(column, ''), self.sql_column(table)) column = self._domain_column(operator, column) Operator = SQL_OPERATORS[operator] assert name == self.name where = Operator(column, self._domain_value(operator, value)) if isinstance(where, operators.In) and not where.right: where = Literal(False) elif isinstance(where, operators.NotIn) and not where.right: where = Literal(True) where = self._domain_add_null(column, operator, value, where) return tables[None][0].id.in_(join.select(table.id, where=where))
def convert_domain(self, domain, tables, Model): pool = Pool() Translation = pool.get('ir.translation') IrModel = pool.get('ir.model') if not self.translate: return super(FieldTranslate, self).convert_domain(domain, tables, Model) table = Model.__table__() translation = Translation.__table__() model = IrModel.__table__() name, operator, value = domain join = self._get_translation_join(Model, name, translation, model, table) Operator = SQL_OPERATORS[operator] assert name == self.name column = Coalesce(NullIf(translation.value, ''), self.sql_column(table)) where = Operator(column, self._domain_value(operator, value)) if isinstance(where, operators.In) and not where.right: where = Literal(False) elif isinstance(where, operators.NotIn) and not where.right: where = Literal(True) where = self._domain_add_null(column, operator, value, where) return tables[None][0].id.in_(join.select(table.id, where=where))
def _get_translation_order(self, tables, Model, name): from trytond.ir.lang import get_parent_language pool = Pool() Translation = pool.get('ir.translation') IrModel = pool.get('ir.model') table, _ = tables[None] join = table language = Transaction().language column = None while language: key = name + '.translation-' + language if key not in tables: translation = Translation.__table__() model = IrModel.__table__() translation, join = self._get_translation_join( Model, name, translation, model, table, table, language) if join.left == table: tables[key] = { None: (join.right, join.condition), } else: tables[key] = { None: (join.left.right, join.left.condition), 'translation': { None: (join.right, join.condition), }, } else: if 'translation' not in tables[key]: translation, _ = tables[key][None] else: translation, _ = tables[key]['translation'][None] column = Coalesce(NullIf(column, ''), translation.value) language = get_parent_language(language) return column
def convert_order(self, name, tables, Model): if not self.translate: return super().convert_order(name, tables, Model) assert name == self.name table, _ = tables[None] column = self._get_translation_order(tables, Model, name) return [Coalesce(NullIf(column, ''), self.sql_column(table))]
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 search_payment_days(cls, name, clause): Party = Pool().get('party.party') if backend.name == 'sqlite': # Sqlite does not support UNNEST function return ['OR', [ ('type', '=', 'out'), ('party.customer_payment_days',) + tuple(clause[1:]), ], [ ('type', '=', 'in'), ('party.supplier_payment_days',) + tuple(clause[1:]), ]] party = Party.__table__() _, operator, value = clause Operator = fields.SQL_OPERATORS[operator] customer_days = party.select(party.id, Unnest(RegExpSplitToArray(party.customer_payment_days, r'\s+')).as_('day')) customer_days = customer_days.select(customer_days.id, where=(Operator(Cast(NullIf(customer_days.day, ''), 'int'), value))) supplier_days = party.select(party.id, Unnest(RegExpSplitToArray(party.supplier_payment_days, r'\s+')).as_('day')) supplier_days = supplier_days.select(supplier_days.id, where=(Operator(Cast(NullIf(supplier_days.day, ''), 'int'), value))) return ['OR', [ ('type', '=', 'out'), ('party', 'in', customer_days), ], [ ('type', '=', 'in'), ('party', 'in', supplier_days), ]]
def _get_translation_column(self, Model, name): from trytond.ir.lang import get_parent_language pool = Pool() Translation = pool.get('ir.translation') IrModel = pool.get('ir.model') table = join = Model.__table__() model = IrModel.__table__() language = Transaction().language column = None while language: translation = Translation.__table__() translation, join = self._get_translation_join( Model, name, translation, model, table, join, language) column = Coalesce(NullIf(column, ''), translation.value) language = get_parent_language(language) return table, join, column
def convert_domain(self, domain, tables, Model): if not self.translate: return super(FieldTranslate, self).convert_domain(domain, tables, Model) table, _ = tables[None] name, operator, value = domain model, join, column = self._get_translation_column(Model, name) column = Coalesce(NullIf(column, ''), self.sql_column(model)) column = self._domain_column(operator, column) Operator = SQL_OPERATORS[operator] assert name == self.name where = Operator(column, self._domain_value(operator, value)) if isinstance(where, operators.In) and not where.right: where = Literal(False) elif isinstance(where, operators.NotIn) and not where.right: where = Literal(True) where = self._domain_add_null(column, operator, value, where) return table.id.in_(join.select(model.id, where=where))
def convert_order(self, name, tables, Model): from trytond.tools import get_parent_language pool = Pool() Translation = pool.get('ir.translation') IrModel = pool.get('ir.model') if not self.translate: return super(FieldTranslate, self).convert_order(name, tables, Model) assert name == self.name table, _ = tables[None] join = table language = Transaction().language column = None while language: key = name + '.translation-' + language if key not in tables: translation = Translation.__table__() model = IrModel.__table__() join = self._get_translation_join(Model, name, translation, model, table, table, language) if join.left == table: tables[key] = { None: (join.right, join.condition), } else: tables[key] = { None: (join.left.right, join.left.condition), 'translation': { None: (join.right, join.condition), }, } else: if 'translation' not in tables[key]: translation, _ = tables[key][None] else: translation, _ = tables[key]['translation'][None] column = Coalesce(NullIf(column, ''), translation.value) language = get_parent_language(language) return [Coalesce(column, self.sql_column(table))]
def __register__(cls, module_name): cursor = Transaction().connection.cursor() table = cls.__table_handler__(module_name) sql_table = cls.__table__() pool = Pool() Work = pool.get('project.work') work = Work.__table__() created_progress = not table.column_exist('progress') effort_exist = table.column_exist('effort_duration') super().__register__(module_name) # Migration from 5.0: Effort renamed into to progress if created_progress and effort_exist: # Don't use UPDATE FROM because SQLite does not support it. value = work.select( (Extract('EPOCH', sql_table.effort_duration) / NullIf(Extract('EPOCH', work.effort_duration), 0)), where=work.id == sql_table.work) cursor.execute(*sql_table.update([sql_table.progress], [value]))
def convert_order(self, name, tables, Model): pool = Pool() Translation = pool.get('ir.translation') IrModel = pool.get('ir.model') if not self.translate: return super(FieldTranslate, self).convert_order(name, tables, Model) assert name == self.name table, _ = tables[None] key = name + '.translation' if key not in tables: translation = Translation.__table__() model = IrModel.__table__() join = self._get_translation_join(Model, name, translation, model, table) if join.left == table: tables[key] = { None: (join.right, join.condition), } else: tables[key] = { None: (join.left.right, join.left.condition), 'translation': { None: (join.right, join.condition), }, } else: if 'translation' not in tables[key]: translation, _ = tables[key][None] else: translation, _ = tables[key]['translation'][None] return [ Coalesce(NullIf(translation.value, ''), self.sql_column(table)) ]
def json_get(self, column, key=None): if key: column = JSONExtract(column, '$.%s' % key) return NullIf(JSONQuote(column), JSONQuote(Null))
def test_nullif(self): nullif = NullIf(self.table.c1, 'foo') self.assertEqual(str(nullif), 'NULLIF("c1", %s)') self.assertEqual(nullif.params, ('foo',))