summaryrefslogtreecommitdiff
path: root/odoo
diff options
context:
space:
mode:
authorRaphael Collet <rco@odoo.com>2020-05-05 12:56:19 +0000
committerRaphael Collet <rco@odoo.com>2020-05-20 07:58:51 +0000
commitaed2134b6388258e907f8cb0dbd53857fb32d5c4 (patch)
treeea5fb92f9917e88a1ad582f3a452149e0a32f822 /odoo
parent538f428787d15725383474a89e656c05a8c0b9fa (diff)
[REF] expression: search on translated fields
Use a LEFT JOIN on table `ir_translation` instead of a subquery, by using the same implementation as the one used for ordering by a translated field. In particular, this avoids having both the LEFT JOIN and subquery when searching and sorting on a given translated field.
Diffstat (limited to 'odoo')
-rw-r--r--odoo/addons/base/tests/test_expression.py14
-rw-r--r--odoo/osv/expression.py34
2 files changed, 5 insertions, 43 deletions
diff --git a/odoo/addons/base/tests/test_expression.py b/odoo/addons/base/tests/test_expression.py
index fdec7204503..7aeb57cac1d 100644
--- a/odoo/addons/base/tests/test_expression.py
+++ b/odoo/addons/base/tests/test_expression.py
@@ -1010,19 +1010,7 @@ class TestQueries(TransactionCase):
AND "res_partner_title__name"."name" = %s
AND "res_partner_title__name"."lang" = %s
AND "res_partner_title__name"."value" != %s)
- WHERE ("res_partner_title"."id" IN (
- WITH temp_irt_current (id, name) AS (
- SELECT ct.id, coalesce(it.value, ct."name")
- FROM res_partner_title ct
- LEFT JOIN ir_translation it ON
- (it.name = %s AND it.lang = %s AND it.type = %s
- AND it.res_id = ct.id AND it.value != '')
- )
- SELECT id
- FROM temp_irt_current
- WHERE name LIKE %s
- ORDER BY name
- ))
+ WHERE COALESCE("res_partner_title__name"."value", "res_partner_title"."name") LIKE %s
ORDER BY COALESCE("res_partner_title__name"."value", "res_partner_title"."name")
''']):
Model.search([('name', 'like', 'foo')])
diff --git a/odoo/osv/expression.py b/odoo/osv/expression.py
index fc959c062ae..95edcecb023 100644
--- a/odoo/osv/expression.py
+++ b/odoo/osv/expression.py
@@ -966,40 +966,14 @@ class expression(object):
sql_operator = {'=like': 'like', '=ilike': 'ilike'}.get(operator, operator)
if need_wildcard:
right = '%%%s%%' % right
-
- inselect_operator = 'inselect'
- if sql_operator in NEGATIVE_TERM_OPERATORS:
- # negate operator (fix lp:1071710)
- sql_operator = sql_operator[4:] if sql_operator[:3] == 'not' else '='
- inselect_operator = 'not inselect'
+ if sql_operator == 'in':
+ right = tuple(right)
unaccent = self._unaccent if sql_operator.endswith('like') else lambda x: x
+ left = unaccent(model._generate_translated_field(alias, left, self.query))
instr = unaccent('%s')
-
- if sql_operator == 'in':
- right = tuple(right)
-
- subselect = """WITH temp_irt_current (id, name) as (
- SELECT ct.id, coalesce(it.value,ct.{quote_left})
- FROM {current_table} ct
- LEFT JOIN ir_translation it ON (it.name = %s and
- it.lang = %s and
- it.type = %s and
- it.res_id = ct.id and
- it.value != '')
- )
- SELECT id FROM temp_irt_current WHERE {name} {operator} {right} order by name
- """.format(current_table=model._table, quote_left=_quote(left), name=unaccent('name'),
- operator=sql_operator, right=instr)
-
- params = (
- model._name + ',' + left,
- get_lang(model.env).code,
- 'model',
- right,
- )
- push(('id', inselect_operator, (subselect, params)), model, alias, internal=True)
+ push_result(f"{left} {sql_operator} {instr}", [right])
else:
expr, params = self.__leaf_to_sql(leaf, model, alias)