Add sql_id method to Reference field and use it for index

issue11788
review447351003
This commit is contained in:
Cédric Krier
2022-10-12 08:25:20 +02:00
parent 7892f8ae56
commit 72fd407fdb
6 changed files with 18 additions and 13 deletions
+1
View File
@@ -1,3 +1,4 @@
* Add sql_id method to Reference field
* Use declarative index definition for ModelSQL
* Add configuration entries for unaccent and similarity functions
* Add authentication services
+4
View File
@@ -684,6 +684,10 @@ Instance methods:
Same as :meth:`~Selection.translated` but for the translated name of the
target model.
.. method:: Reference.sql_id(column, Model)
Return the SQL expression that extract the record ID of the column.
Many2One
--------
+2 -5
View File
@@ -3,9 +3,8 @@
from collections import defaultdict
from itertools import chain
from sql import Cast, Literal, Null
from sql import Literal, Null
from sql.conditionals import Coalesce
from sql.functions import Position, Substring
from trytond.pool import Pool
from trytond.pyson import PYSONEncoder
@@ -379,9 +378,7 @@ class Many2Many(Field):
origin_where = None
if origin_field._type == 'reference':
origin_where = origin.like(Model.__name__ + ',%')
origin = Cast(Substring(origin,
Position(',', origin) + Literal(1)),
Relation.id.sql_type().base)
origin = origin_field.sql_id(origin, Relation)
target = getattr(Relation, self.target).sql_column(relation)
if '.' not in name:
+1 -5
View File
@@ -3,9 +3,7 @@
from collections import defaultdict
from itertools import chain
from sql import Cast, Literal
from sql.conditionals import Coalesce
from sql.functions import Position, Substring
from trytond.pool import Pool
from trytond.pyson import PYSONEncoder
@@ -332,9 +330,7 @@ class One2Many(Field):
origin_where = None
if origin_field._type == 'reference':
origin_where = origin.like(Model.__name__ + ',%')
origin = Cast(Substring(origin,
Position(',', origin) + Literal(1)),
Target.id.sql_type().base)
origin = origin_field.sql_id(origin, Target)
if '.' not in name:
if value is None:
+8 -3
View File
@@ -187,6 +187,12 @@ class Reference(SelectionMixin, Field):
value = str(value)
return super(Reference, self).sql_format(value)
def sql_id(self, column, Model):
"Return SQL expression for the id part of the field"
return Cast(Substring(
column, Position(',', column) + Literal(1)),
Model.id.sql_type().base)
@with_inactive_records
def convert_domain(self, domain, tables, Model):
if '.' not in domain[0]:
@@ -203,9 +209,8 @@ class Reference(SelectionMixin, Field):
if 'active' in Target._fields:
target_domain.append(('active', 'in', [True, False]))
query = Target.search(target_domain, order=[], query=True)
return (Cast(Substring(column,
Position(',', column) + Literal(1)),
Model.id.sql_type().base).in_(query)
return (
self.sql_id(column, Model).in_(query)
& column.like(target + ',%'))
def definition(self, model, language):
+2
View File
@@ -318,6 +318,8 @@ class ModelSQL(ModelStorage):
Index(
target,
(column, Index.Similarity(begin=True)),
(target_field.sql_id(column, Target),
Index.Range()),
where=where),
})
else: