Refs #25287 -- Added _sqlite_prepare_dtdelta_param() hook.

This commit is contained in:
Tobias Bengfort 2021-04-20 09:05:52 +02:00 committed by Mariusz Felisiak
parent 4bb27c8719
commit 9e1ccd7283
1 changed files with 19 additions and 9 deletions

View File

@ -548,6 +548,15 @@ def _sqlite_time_extract(lookup_type, dt):
return getattr(dt, lookup_type)
def _sqlite_prepare_dtdelta_param(conn, param):
if conn in ['+', '-']:
if isinstance(param, int):
return datetime.timedelta(0, 0, param)
else:
return backend_utils.typecast_timestamp(param)
return param
@none_guard
def _sqlite_format_dtdelta(conn, lhs, rhs):
"""
@ -555,18 +564,19 @@ def _sqlite_format_dtdelta(conn, lhs, rhs):
- An integer number of microseconds
- A string representing a datetime
"""
conn = conn.strip()
try:
real_lhs = datetime.timedelta(0, 0, lhs) if isinstance(lhs, int) else backend_utils.typecast_timestamp(lhs)
real_rhs = datetime.timedelta(0, 0, rhs) if isinstance(rhs, int) else backend_utils.typecast_timestamp(rhs)
if conn.strip() == '+':
out = real_lhs + real_rhs
else:
out = real_lhs - real_rhs
real_lhs = _sqlite_prepare_dtdelta_param(conn, lhs)
real_rhs = _sqlite_prepare_dtdelta_param(conn, rhs)
except (ValueError, TypeError):
return None
# typecast_timestamp returns a date or a datetime without timezone.
# It will be formatted as "%Y-%m-%d" or "%Y-%m-%d %H:%M:%S[.%f]"
return str(out)
if conn == '+':
# typecast_timestamp returns a date or a datetime without timezone.
# It will be formatted as "%Y-%m-%d" or "%Y-%m-%d %H:%M:%S[.%f]"
out = str(real_lhs + real_rhs)
else:
out = str(real_lhs - real_rhs)
return out
@none_guard