Fixed #28925 -- Fixed durations-only expressions crash on SQLite and MySQL.

This removes also unused DatabaseOperations.date_interval_sql().
This commit is contained in:
Sergey Fedoseev 2020-06-29 11:28:06 +05:00 committed by Mariusz Felisiak
parent 9d752dabe3
commit dd5aa8cb5f
6 changed files with 22 additions and 40 deletions

View file

@ -56,10 +56,12 @@ class Combinable:
def _combine(self, other, connector, reversed):
if not hasattr(other, 'resolve_expression'):
# everything must be resolvable to an expression
if isinstance(other, datetime.timedelta):
other = DurationValue(other, output_field=fields.DurationField())
else:
other = Value(other)
output_field = (
fields.DurationField()
if isinstance(other, datetime.timedelta) else
None
)
other = Value(other, output_field=output_field)
if reversed:
return CombinedExpression(other, connector, self)
@ -451,7 +453,8 @@ class CombinedExpression(SQLiteNumericMixin, Expression):
rhs_type = None
if (
not connection.features.has_native_duration_field and
'DurationField' in {lhs_type, rhs_type}
'DurationField' in {lhs_type, rhs_type} and
lhs_type != rhs_type
):
return DurationExpression(self.lhs, self.connector, self.rhs).as_sql(compiler, connection)
datetime_fields = {'DateField', 'DateTimeField', 'TimeField'}
@ -480,15 +483,14 @@ class CombinedExpression(SQLiteNumericMixin, Expression):
class DurationExpression(CombinedExpression):
def compile(self, side, compiler, connection):
if not isinstance(side, DurationValue):
try:
output = side.output_field
except FieldError:
pass
else:
if output.get_internal_type() == 'DurationField':
sql, params = compiler.compile(side)
return connection.ops.format_for_duration_arithmetic(sql), params
try:
output = side.output_field
except FieldError:
pass
else:
if output.get_internal_type() == 'DurationField':
sql, params = compiler.compile(side)
return connection.ops.format_for_duration_arithmetic(sql), params
return compiler.compile(side)
def as_sql(self, compiler, connection):
@ -709,14 +711,6 @@ class Value(Expression):
return []
class DurationValue(Value):
def as_sql(self, compiler, connection):
connection.ops.check_expression_support(self)
if connection.features.has_native_duration_field:
return super().as_sql(compiler, connection)
return connection.ops.date_interval_sql(self.value), []
class RawSQL(Expression):
def __init__(self, sql, params, output_field=None):
if output_field is None: