Add sql_cast on Field
issue7266 review37141002
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -180,6 +180,10 @@ Instance methods:
|
||||
For the list of supported types by Tryton see
|
||||
:ref:`backend types <topics-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.
|
||||
|
||||
@@ -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):
|
||||
'''
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user