2018-12-02 07:43:27 +08:00
|
|
|
import sys
|
|
|
|
|
2018-12-02 07:46:28 +08:00
|
|
|
from django.db.models.fields import DecimalField, FloatField, IntegerField
|
2018-12-02 07:43:27 +08:00
|
|
|
from django.db.models.functions import Cast
|
|
|
|
|
|
|
|
|
|
|
|
class FixDecimalInputMixin:
|
|
|
|
|
|
|
|
def as_postgresql(self, compiler, connection, **extra_context):
|
|
|
|
# Cast FloatField to DecimalField as PostgreSQL doesn't support the
|
|
|
|
# following function signatures:
|
|
|
|
# - LOG(double, double)
|
|
|
|
# - MOD(double, double)
|
|
|
|
output_field = DecimalField(decimal_places=sys.float_info.dig, max_digits=1000)
|
|
|
|
clone = self.copy()
|
|
|
|
clone.set_source_expressions([
|
|
|
|
Cast(expression, output_field) if isinstance(expression.output_field, FloatField)
|
|
|
|
else expression for expression in self.get_source_expressions()
|
|
|
|
])
|
|
|
|
return clone.as_sql(compiler, connection, **extra_context)
|
|
|
|
|
|
|
|
|
2018-12-02 07:49:38 +08:00
|
|
|
class FixDurationInputMixin:
|
|
|
|
|
|
|
|
def as_mysql(self, compiler, connection, **extra_context):
|
|
|
|
sql, params = super().as_sql(compiler, connection, **extra_context)
|
|
|
|
if self.output_field.get_internal_type() == 'DurationField':
|
|
|
|
sql = 'CAST(%s AS SIGNED)' % sql
|
|
|
|
return sql, params
|
|
|
|
|
|
|
|
def as_oracle(self, compiler, connection, **extra_context):
|
|
|
|
if self.output_field.get_internal_type() == 'DurationField':
|
|
|
|
expression = self.get_source_expressions()[0]
|
|
|
|
options = self._get_repr_options()
|
|
|
|
from django.db.backends.oracle.functions import IntervalToSeconds, SecondsToInterval
|
|
|
|
return compiler.compile(
|
|
|
|
SecondsToInterval(self.__class__(IntervalToSeconds(expression), **options))
|
|
|
|
)
|
|
|
|
return super().as_sql(compiler, connection, **extra_context)
|
|
|
|
|
|
|
|
|
2018-12-02 07:43:27 +08:00
|
|
|
class NumericOutputFieldMixin:
|
|
|
|
|
|
|
|
def _resolve_output_field(self):
|
2018-12-02 07:46:28 +08:00
|
|
|
source_expressions = self.get_source_expressions()
|
|
|
|
if any(isinstance(s.output_field, DecimalField) for s in source_expressions):
|
|
|
|
return DecimalField()
|
|
|
|
if any(isinstance(s.output_field, IntegerField) for s in source_expressions):
|
|
|
|
return FloatField()
|
|
|
|
return super()._resolve_output_field() if source_expressions else FloatField()
|