From 2115be616b10f4542df7155e3a4c3f02f97cc2a3 Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Fri, 5 Jan 2018 03:16:17 +0500 Subject: [PATCH] Refs #28459 -- Improved performance of duration expressions on SQLite. --- django/db/backends/sqlite3/base.py | 16 ++++------------ django/db/backends/sqlite3/operations.py | 4 ++-- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index fd489e34de..857b050361 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -1,6 +1,7 @@ """ SQLite3 backend for the sqlite3 module in the standard library. """ +import datetime import decimal import math import re @@ -14,7 +15,7 @@ from django.db import utils from django.db.backends import utils as backend_utils from django.db.backends.base.base import BaseDatabaseWrapper from django.utils import timezone -from django.utils.dateparse import parse_datetime, parse_duration, parse_time +from django.utils.dateparse import parse_datetime, parse_time from django.utils.duration import duration_microseconds from .client import DatabaseClient # isort:skip @@ -426,20 +427,11 @@ def _sqlite_format_dtdelta(conn, lhs, rhs): """ LHS and RHS can be either: - An integer number of microseconds - - A string representing a timedelta object - A string representing a datetime """ try: - if isinstance(lhs, int): - lhs = str(decimal.Decimal(lhs) / decimal.Decimal(1000000)) - real_lhs = parse_duration(lhs) - if real_lhs is None: - real_lhs = backend_utils.typecast_timestamp(lhs) - if isinstance(rhs, int): - rhs = str(decimal.Decimal(rhs) / decimal.Decimal(1000000)) - real_rhs = parse_duration(rhs) - if real_rhs is None: - real_rhs = backend_utils.typecast_timestamp(rhs) + 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: diff --git a/django/db/backends/sqlite3/operations.py b/django/db/backends/sqlite3/operations.py index 69744365a2..10b064d966 100644 --- a/django/db/backends/sqlite3/operations.py +++ b/django/db/backends/sqlite3/operations.py @@ -10,7 +10,7 @@ from django.db.models import aggregates, fields from django.db.models.expressions import Col from django.utils import timezone from django.utils.dateparse import parse_date, parse_datetime, parse_time -from django.utils.duration import duration_string +from django.utils.duration import duration_microseconds class DatabaseOperations(BaseDatabaseOperations): @@ -63,7 +63,7 @@ class DatabaseOperations(BaseDatabaseOperations): return "django_date_extract('%s', %s)" % (lookup_type.lower(), field_name) def date_interval_sql(self, timedelta): - return "'%s'" % duration_string(timedelta) + return str(duration_microseconds(timedelta)) def format_for_duration_arithmetic(self, sql): """Do nothing since formatting is handled in the custom function."""