diff --git a/CHANGELOG b/CHANGELOG index 8439b9d9a1..c9fb88a1e4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Add sql_cast on Field + Version 4.8.0 - 2018-04-23 * Bug fixes (see mercurial logs for details) * Add index method to order field set calls diff --git a/doc/ref/models/fields.rst b/doc/ref/models/fields.rst index 3e5ffb82b5..21759a0f88 100644 --- a/doc/ref/models/fields.rst +++ b/doc/ref/models/fields.rst @@ -180,6 +180,10 @@ Instance methods: For the list of supported types by Tryton see :ref:`backend types `. +.. method:: Field.sql_cast(expression) + + Return the SQL expression with cast with the type of the field. + .. method:: Field.sql_column(table) Return the Column instance based on table. diff --git a/trytond/model/fields/date.py b/trytond/model/fields/date.py index a086df2c66..cff91df8bd 100644 --- a/trytond/model/fields/date.py +++ b/trytond/model/fields/date.py @@ -1,10 +1,27 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. import datetime +from sql.functions import Function +from ... import backend from .field import Field +class SQLite_Date(Function): + __slots__ = () + _function = 'DATE' + + +class SQLite_DateTime(Function): + __slots__ = () + _function = 'DATETIME' + + +class SQLite_Time(Function): + __slots__ = () + _function = 'TIME' + + class Date(Field): ''' Define a date field (``date``). @@ -29,6 +46,11 @@ class Date(Field): % (type(value), type(self))) return value + def sql_cast(self, expression): + if backend.name() == 'sqlite': + return SQLite_Date(expression) + return super(Date, self).sql_cast(expression) + class DateTime(Field): ''' @@ -65,6 +87,11 @@ class DateTime(Field): % (type(value), type(self))) return value.replace(microsecond=0) + def sql_cast(self, expression): + if backend.name() == 'sqlite': + return SQLite_DateTime(expression) + return super(DateTime, self).sql_cast(expression) + class Timestamp(Field): ''' @@ -111,6 +138,11 @@ class Time(DateTime): % (type(value), type(self))) return value.replace(microsecond=0) + def sql_cast(self, expression): + if backend.name() == 'sqlite': + return SQLite_Time(expression) + return super(Time, self).sql_cast(expression) + class TimeDelta(Field): ''' diff --git a/trytond/model/fields/field.py b/trytond/model/fields/field.py index ce5de4b99f..0600e293e2 100644 --- a/trytond/model/fields/field.py +++ b/trytond/model/fields/field.py @@ -4,7 +4,7 @@ import warnings from functools import wraps from sql import (operators, Column, Literal, Select, CombiningQuery, Null, - Query, Expression) + Query, Expression, Cast) from sql.conditionals import Coalesce, NullIf from sql.operators import Concat @@ -314,6 +314,9 @@ class Field(object): database = Transaction().database return database.sql_type(self._sql_type) + def sql_cast(self, expression): + return Cast(expression, self.sql_type().base) + def sql_column(self, table): return Column(table, self.name)