base_search_fuzzy: Migration to 14.0

pull/2530/head
Areeb Siddiqi 2021-05-28 15:07:09 -04:00 committed by Daniel Reis
parent af8562c241
commit 2d95afc1f7
7 changed files with 67 additions and 14 deletions

View File

@ -1,4 +1,5 @@
# Copyright 2016 ForgeFlow S.L. # Copyright 2016 ForgeFlow S.L.
# Copyright 2016 Serpent Consulting Services Pvt. Ltd. # Copyright 2016 Serpent Consulting Services Pvt. Ltd.
# Copyright 2020 NextERP SRL.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from . import models from . import models

View File

@ -1,5 +1,7 @@
# Copyright 2016 ForgeFlow S.L. # Copyright 2016 ForgeFlow S.L.
# Copyright 2016 Serpent Consulting Services Pvt. Ltd. # Copyright 2016 Serpent Consulting Services Pvt. Ltd.
# Copyright 2020 NextERP SRL.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from . import ir_model from . import ir_model
from . import trgm_index from . import trgm_index
from . import query

View File

@ -1,6 +1,7 @@
# Copyright 2016 ForgeFlow S.L. # Copyright 2016 ForgeFlow S.L.
# Copyright 2016 Serpent Consulting Services Pvt. Ltd. # Copyright 2016 Serpent Consulting Services Pvt. Ltd.
# Copyright 2017 LasLabs Inc. # Copyright 2017 LasLabs Inc.
# Copyright 2020 NextERP SRL.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging import logging
@ -11,12 +12,9 @@ _logger = logging.getLogger(__name__)
def patch_leaf_trgm(method): def patch_leaf_trgm(method):
def decorate_leaf_to_sql(self, eleaf): def decorate_leaf_to_sql(self, leaf, model, alias):
model = eleaf.model
leaf = eleaf.leaf
left, operator, right = leaf left, operator, right = leaf
table_alias = '"%s"' % (eleaf.generate_alias()) table_alias = '"%s"' % alias
if operator == "%": if operator == "%":
sql_operator = "%%" sql_operator = "%%"
@ -45,8 +43,9 @@ def patch_leaf_trgm(method):
return query, params return query, params
elif operator == "inselect": elif operator == "inselect":
right = (right[0].replace(" % ", " %% "), right[1]) right = (right[0].replace(" % ", " %% "), right[1])
eleaf.leaf = (left, operator, right) leaf = (left, operator, right)
return method(self, eleaf)
return method(self, leaf, model, alias)
decorate_leaf_to_sql.__decorated__ = True decorate_leaf_to_sql.__decorated__ = True

View File

@ -0,0 +1,49 @@
# Copyright 2016 ForgeFlow S.L.
# Copyright 2016 Serpent Consulting Services Pvt. Ltd.
# Copyright 2017 LasLabs Inc.
# Copyright 2020 NextERP SRL.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo.osv import query
Oridinal_Query_obj = query.Query
def percent_search_fuzzy(self, where_claus):
if " % " in " ".join(where_claus):
new_where_clause = [x.replace(" % ", " %% ") for x in where_claus]
return tuple(new_where_clause)
return where_claus
Oridinal_Query_obj.percent_search_fuzzy = percent_search_fuzzy
# @property
def where_clause_new(self):
ok_where = self.percent_search_fuzzy(self._where_clausess)
return tuple(ok_where)
# original return(self._where_clausess)
Oridinal_Query_obj.where_clause = where_clause_new
def get_sql_new(self):
""" Returns (query_from, query_where, query_params). """
tables = [query._from_table(table, alias) for alias, table in self._tables.items()]
joins = []
params = []
for alias, (kind, table, condition, condition_params) in self._joins.items():
joins.append(f"{kind} {query._from_table(table, alias)} ON ({condition})")
params.extend(condition_params)
from_clause = " ".join([", ".join(tables)] + joins)
ok_where = self.percent_search_fuzzy(self._where_clauses)
where_clause = " AND ".join(ok_where)
# original where_clause = " AND ".join(self._where_clauses)
return from_clause, where_clause, params + self._where_params
Oridinal_Query_obj.get_sql = get_sql_new

View File

@ -22,6 +22,7 @@ class TrgmIndex(models.Model):
field_id = fields.Many2one( field_id = fields.Many2one(
comodel_name="ir.model.fields", comodel_name="ir.model.fields",
string="Field", string="Field",
ondelete="set default",
required=True, required=True,
help='You can either select a field of type "text" or "char".', help='You can either select a field of type "text" or "char".',
) )
@ -41,10 +42,10 @@ class TrgmIndex(models.Model):
string="Index Type", string="Index Type",
default="gin", default="gin",
required=True, required=True,
help='Cite from PostgreSQL documentation: "As a rule of thumb, a ' ondelete={"gin": "set default", "gist": "set default"},
"GIN index is faster to search than a GiST index, but slower to " help="Cite from PostgreSQL documentation: GIN indexes are "
"build or update; so GIN is better suited for static data and " "the preferred text search index type."
'GiST for often-updated data."', "See: https://www.postgresql.org/docs/current/textsearch-indexes.html",
) )
def _trgm_extension_exists(self): def _trgm_extension_exists(self):

View File

@ -4,6 +4,6 @@
* Dave Lasley <dave@laslabs.com> * Dave Lasley <dave@laslabs.com>
* `Tecnativa <https://www.tecnativa.com>`_: * `Tecnativa <https://www.tecnativa.com>`_:
* Vicent Cubells * Vicent Cubells
* Ernesto Tejeda * Ernesto Tejeda
* teodoralexandru@nexterp.ro 2020 NextERP SRL.

View File

@ -49,7 +49,8 @@ class QueryGenerationCase(TransactionCase):
# the % parameter has to be escaped (%%) for the string replation # the % parameter has to be escaped (%%) for the string replation
self.assertIn( self.assertIn(
"""SELECT id FROM temp_irt_current WHERE name %% %s""", where_clause """COALESCE("res_partner_category__name"."value", "res_partner_category"."name") %% %s""", # noqa
where_clause,
) )
complete_where = self.env.cr.mogrify( complete_where = self.env.cr.mogrify(
@ -58,7 +59,7 @@ class QueryGenerationCase(TransactionCase):
) )
self.assertIn( self.assertIn(
b"""SELECT id FROM temp_irt_current WHERE name % 'Goschaeftlic'""", b"""SELECT FROM "res_partner_category" LEFT JOIN "ir_translation" AS "res_partner_category__name" ON ("res_partner_category"."id" = "res_partner_category__name"."res_id" AND "res_partner_category__name"."type" = \'model\' AND "res_partner_category__name"."name" = \'res.partner.category,name\' AND "res_partner_category__name"."lang" = \'de_DE\' AND "res_partner_category__name"."value" != \'\') WHERE COALESCE("res_partner_category__name"."value", "res_partner_category"."name") % \'Goschaeftlic\'""", # noqa
complete_where, complete_where,
) )