Set type declaration in column names of SQLite only on top-level query

Closes #12532
This commit is contained in:
Cédric Krier
2024-06-30 16:25:41 +02:00
parent 2144389096
commit f5e33378be
2 changed files with 11 additions and 15 deletions
+6 -15
View File
@@ -2,20 +2,13 @@
# this repository contains the full copyright notices and license terms.
from decimal import Decimal
from sql import As, Cast, CombiningQuery, Literal, Select
from sql import Cast, CombiningQuery, Literal, Select
from trytond import backend
from .float import Float
class SQLite_Cast(Cast):
def as_(self, output_name):
# Use PARSE_COLNAMES instead of CAST for final column
return As(self.expression, '%s [NUMERIC]' % output_name)
class Numeric(Float):
'''
Define a numeric field (``decimal``).
@@ -24,18 +17,16 @@ class Numeric(Float):
_sql_type = 'NUMERIC'
_py_type = Decimal
def sql_column(self, table):
column = super(Numeric, self).sql_column(table)
db_type = backend.name
if db_type == 'sqlite':
def _domain_column(self, operator, column):
column = super()._domain_column(operator, column)
if backend.name == 'sqlite':
# Must be casted as Decimal is stored as bytes
column = SQLite_Cast(column, self.sql_type().base)
column = Cast(column, self.sql_type().base)
return column
def _domain_value(self, operator, value):
value = super(Numeric, self)._domain_value(operator, value)
db_type = backend.name
if db_type == 'sqlite':
if backend.name == 'sqlite':
if isinstance(value, (Select, CombiningQuery)):
return value
# Must be casted as Decimal is adapted to bytes
+5
View File
@@ -1825,6 +1825,11 @@ class ModelSQL(ModelStorage):
# compute it here because __search_order might modify tables
table = convert_from(None, tables)
columns = cls.__searched_columns(main_table, eager=not query)
if backend.name == 'sqlite':
for column in columns:
field = cls._fields.get(column.output_name)
if field:
column.output_name += ' [%s]' % field.sql_type().base
select = table.select(
*columns, where=expression, limit=limit, offset=offset,
order_by=order_by)