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

Closes #12532
(grafted from c07764f86f1655997d1271a5d2b30b9607a02a72)

--HG--
branch : 6.0
This commit is contained in:
Cédric Krier
2024-06-30 16:25:41 +02:00
parent 0ab6215199
commit cba76e245a
2 changed files with 11 additions and 15 deletions
+6 -15
View File
@@ -1,19 +1,12 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from decimal import Decimal
from sql import Cast, Literal, Select, CombiningQuery, As
from sql import Cast, Literal, Select, CombiningQuery
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``).
@@ -22,18 +15,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
@@ -1351,6 +1351,11 @@ class ModelSQL(ModelStorage):
columns += [Extract('EPOCH',
Coalesce(main_table.write_date, main_table.create_date)
).cast(sql_type).as_('_timestamp')]
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, order_by=order_by, limit=limit, offset=offset)
if query: