[IMP] core: introduce _field_to_sql() to replace _inherits_join_calc()

The new method should be used to generate an SQL object that represents
the value of a field in an SQL query.  Later this method will add
metadata to SQL objects, and that metadata will be used to determine
which fields to flush before executing some SQL code.

Part-of: odoo/odoo#138019
This commit is contained in:
Raphael Collet
2023-10-20 09:35:18 +00:00
parent 31caff2834
commit 2c09f5ed68
2 changed files with 103 additions and 73 deletions
+6 -6
View File
@@ -29,7 +29,7 @@ except ImportError:
from decorator import decorator
from .exceptions import AccessError, CacheMiss
from .tools import classproperty, frozendict, lazy_property, OrderedSet, Query, StackMap
from .tools import frozendict, lazy_property, OrderedSet, Query, SQL, StackMap
from .tools.translate import _
_logger = logging.getLogger(__name__)
@@ -1249,14 +1249,14 @@ class Cache(object):
# select the column for the given ids
query = Query(env.cr, model._table, model._table_query)
qname = model._inherits_join_calc(model._table, field.name, query)
sql_id = SQL.identifier(model._table, 'id')
sql_field = model._field_to_sql(model._table, field.name, query)
if field.type == 'binary' and (
model.env.context.get('bin_size') or model.env.context.get('bin_size_' + field.name)
):
qname = f'pg_size_pretty(length({qname})::bigint)'
query.add_where(f'"{model._table}".id IN %s', [tuple(ids)])
query_str, params = query.select(f'"{model._table}".id', qname)
env.cr.execute(query_str, params)
sql_field = SQL('pg_size_pretty(length(%s)::bigint)', sql_field)
query.add_where(SQL("%s IN %s", sql_id, tuple(ids)))
env.cr.execute(query.select(sql_id, sql_field))
# compare returned values with corresponding values in cache
for id_, value in env.cr.fetchall():