summaryrefslogtreecommitdiff
path: root/django/db/backends/sqlite3/_functions.py
diff options
context:
space:
mode:
authordjango-bot <ops@djangoproject.com>2022-02-03 20:24:19 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-02-07 20:37:05 +0100
commit9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch)
treef0506b668a013d0063e5fba3dbf4863b466713ba /django/db/backends/sqlite3/_functions.py
parentf68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff)
downloaddjango-9c19aff7c7561e3a82978a272ecdaad40dda5c00.tar.gz
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'django/db/backends/sqlite3/_functions.py')
-rw-r--r--django/db/backends/sqlite3/_functions.py234
1 files changed, 128 insertions, 106 deletions
diff --git a/django/db/backends/sqlite3/_functions.py b/django/db/backends/sqlite3/_functions.py
index 3529a99dd6..86684c1907 100644
--- a/django/db/backends/sqlite3/_functions.py
+++ b/django/db/backends/sqlite3/_functions.py
@@ -7,14 +7,30 @@ import statistics
from datetime import timedelta
from hashlib import sha1, sha224, sha256, sha384, sha512
from math import (
- acos, asin, atan, atan2, ceil, cos, degrees, exp, floor, fmod, log, pi,
- radians, sin, sqrt, tan,
+ acos,
+ asin,
+ atan,
+ atan2,
+ ceil,
+ cos,
+ degrees,
+ exp,
+ floor,
+ fmod,
+ log,
+ pi,
+ radians,
+ sin,
+ sqrt,
+ tan,
)
from re import search as re_search
from django.db.backends.base.base import timezone_constructor
from django.db.backends.utils import (
- split_tzname_delta, typecast_time, typecast_timestamp,
+ split_tzname_delta,
+ typecast_time,
+ typecast_timestamp,
)
from django.utils import timezone
from django.utils.crypto import md5
@@ -26,56 +42,62 @@ def register(connection):
connection.create_function,
deterministic=True,
)
- create_deterministic_function('django_date_extract', 2, _sqlite_datetime_extract)
- create_deterministic_function('django_date_trunc', 4, _sqlite_date_trunc)
- create_deterministic_function('django_datetime_cast_date', 3, _sqlite_datetime_cast_date)
- create_deterministic_function('django_datetime_cast_time', 3, _sqlite_datetime_cast_time)
- create_deterministic_function('django_datetime_extract', 4, _sqlite_datetime_extract)
- create_deterministic_function('django_datetime_trunc', 4, _sqlite_datetime_trunc)
- create_deterministic_function('django_time_extract', 2, _sqlite_time_extract)
- create_deterministic_function('django_time_trunc', 4, _sqlite_time_trunc)
- create_deterministic_function('django_time_diff', 2, _sqlite_time_diff)
- create_deterministic_function('django_timestamp_diff', 2, _sqlite_timestamp_diff)
- create_deterministic_function('django_format_dtdelta', 3, _sqlite_format_dtdelta)
- create_deterministic_function('regexp', 2, _sqlite_regexp)
- create_deterministic_function('ACOS', 1, _sqlite_acos)
- create_deterministic_function('ASIN', 1, _sqlite_asin)
- create_deterministic_function('ATAN', 1, _sqlite_atan)
- create_deterministic_function('ATAN2', 2, _sqlite_atan2)
- create_deterministic_function('BITXOR', 2, _sqlite_bitxor)
- create_deterministic_function('CEILING', 1, _sqlite_ceiling)
- create_deterministic_function('COS', 1, _sqlite_cos)
- create_deterministic_function('COT', 1, _sqlite_cot)
- create_deterministic_function('DEGREES', 1, _sqlite_degrees)
- create_deterministic_function('EXP', 1, _sqlite_exp)
- create_deterministic_function('FLOOR', 1, _sqlite_floor)
- create_deterministic_function('LN', 1, _sqlite_ln)
- create_deterministic_function('LOG', 2, _sqlite_log)
- create_deterministic_function('LPAD', 3, _sqlite_lpad)
- create_deterministic_function('MD5', 1, _sqlite_md5)
- create_deterministic_function('MOD', 2, _sqlite_mod)
- create_deterministic_function('PI', 0, _sqlite_pi)
- create_deterministic_function('POWER', 2, _sqlite_power)
- create_deterministic_function('RADIANS', 1, _sqlite_radians)
- create_deterministic_function('REPEAT', 2, _sqlite_repeat)
- create_deterministic_function('REVERSE', 1, _sqlite_reverse)
- create_deterministic_function('RPAD', 3, _sqlite_rpad)
- create_deterministic_function('SHA1', 1, _sqlite_sha1)
- create_deterministic_function('SHA224', 1, _sqlite_sha224)
- create_deterministic_function('SHA256', 1, _sqlite_sha256)
- create_deterministic_function('SHA384', 1, _sqlite_sha384)
- create_deterministic_function('SHA512', 1, _sqlite_sha512)
- create_deterministic_function('SIGN', 1, _sqlite_sign)
- create_deterministic_function('SIN', 1, _sqlite_sin)
- create_deterministic_function('SQRT', 1, _sqlite_sqrt)
- create_deterministic_function('TAN', 1, _sqlite_tan)
+ create_deterministic_function("django_date_extract", 2, _sqlite_datetime_extract)
+ create_deterministic_function("django_date_trunc", 4, _sqlite_date_trunc)
+ create_deterministic_function(
+ "django_datetime_cast_date", 3, _sqlite_datetime_cast_date
+ )
+ create_deterministic_function(
+ "django_datetime_cast_time", 3, _sqlite_datetime_cast_time
+ )
+ create_deterministic_function(
+ "django_datetime_extract", 4, _sqlite_datetime_extract
+ )
+ create_deterministic_function("django_datetime_trunc", 4, _sqlite_datetime_trunc)
+ create_deterministic_function("django_time_extract", 2, _sqlite_time_extract)
+ create_deterministic_function("django_time_trunc", 4, _sqlite_time_trunc)
+ create_deterministic_function("django_time_diff", 2, _sqlite_time_diff)
+ create_deterministic_function("django_timestamp_diff", 2, _sqlite_timestamp_diff)
+ create_deterministic_function("django_format_dtdelta", 3, _sqlite_format_dtdelta)
+ create_deterministic_function("regexp", 2, _sqlite_regexp)
+ create_deterministic_function("ACOS", 1, _sqlite_acos)
+ create_deterministic_function("ASIN", 1, _sqlite_asin)
+ create_deterministic_function("ATAN", 1, _sqlite_atan)
+ create_deterministic_function("ATAN2", 2, _sqlite_atan2)
+ create_deterministic_function("BITXOR", 2, _sqlite_bitxor)
+ create_deterministic_function("CEILING", 1, _sqlite_ceiling)
+ create_deterministic_function("COS", 1, _sqlite_cos)
+ create_deterministic_function("COT", 1, _sqlite_cot)
+ create_deterministic_function("DEGREES", 1, _sqlite_degrees)
+ create_deterministic_function("EXP", 1, _sqlite_exp)
+ create_deterministic_function("FLOOR", 1, _sqlite_floor)
+ create_deterministic_function("LN", 1, _sqlite_ln)
+ create_deterministic_function("LOG", 2, _sqlite_log)
+ create_deterministic_function("LPAD", 3, _sqlite_lpad)
+ create_deterministic_function("MD5", 1, _sqlite_md5)
+ create_deterministic_function("MOD", 2, _sqlite_mod)
+ create_deterministic_function("PI", 0, _sqlite_pi)
+ create_deterministic_function("POWER", 2, _sqlite_power)
+ create_deterministic_function("RADIANS", 1, _sqlite_radians)
+ create_deterministic_function("REPEAT", 2, _sqlite_repeat)
+ create_deterministic_function("REVERSE", 1, _sqlite_reverse)
+ create_deterministic_function("RPAD", 3, _sqlite_rpad)
+ create_deterministic_function("SHA1", 1, _sqlite_sha1)
+ create_deterministic_function("SHA224", 1, _sqlite_sha224)
+ create_deterministic_function("SHA256", 1, _sqlite_sha256)
+ create_deterministic_function("SHA384", 1, _sqlite_sha384)
+ create_deterministic_function("SHA512", 1, _sqlite_sha512)
+ create_deterministic_function("SIGN", 1, _sqlite_sign)
+ create_deterministic_function("SIN", 1, _sqlite_sin)
+ create_deterministic_function("SQRT", 1, _sqlite_sqrt)
+ create_deterministic_function("TAN", 1, _sqlite_tan)
# Don't use the built-in RANDOM() function because it returns a value
# in the range [-1 * 2^63, 2^63 - 1] instead of [0, 1).
- connection.create_function('RAND', 0, random.random)
- connection.create_aggregate('STDDEV_POP', 1, StdDevPop)
- connection.create_aggregate('STDDEV_SAMP', 1, StdDevSamp)
- connection.create_aggregate('VAR_POP', 1, VarPop)
- connection.create_aggregate('VAR_SAMP', 1, VarSamp)
+ connection.create_function("RAND", 0, random.random)
+ connection.create_aggregate("STDDEV_POP", 1, StdDevPop)
+ connection.create_aggregate("STDDEV_SAMP", 1, StdDevSamp)
+ connection.create_aggregate("VAR_POP", 1, VarPop)
+ connection.create_aggregate("VAR_SAMP", 1, VarSamp)
def _sqlite_datetime_parse(dt, tzname=None, conn_tzname=None):
@@ -90,9 +112,9 @@ def _sqlite_datetime_parse(dt, tzname=None, conn_tzname=None):
if tzname is not None and tzname != conn_tzname:
tzname, sign, offset = split_tzname_delta(tzname)
if offset:
- hours, minutes = offset.split(':')
+ hours, minutes = offset.split(":")
offset_delta = timedelta(hours=int(hours), minutes=int(minutes))
- dt += offset_delta if sign == '+' else -offset_delta
+ dt += offset_delta if sign == "+" else -offset_delta
dt = timezone.localtime(dt, timezone_constructor(tzname))
return dt
@@ -101,19 +123,19 @@ def _sqlite_date_trunc(lookup_type, dt, tzname, conn_tzname):
dt = _sqlite_datetime_parse(dt, tzname, conn_tzname)
if dt is None:
return None
- if lookup_type == 'year':
- return f'{dt.year:04d}-01-01'
- elif lookup_type == 'quarter':
+ if lookup_type == "year":
+ return f"{dt.year:04d}-01-01"
+ elif lookup_type == "quarter":
month_in_quarter = dt.month - (dt.month - 1) % 3
- return f'{dt.year:04d}-{month_in_quarter:02d}-01'
- elif lookup_type == 'month':
- return f'{dt.year:04d}-{dt.month:02d}-01'
- elif lookup_type == 'week':
+ return f"{dt.year:04d}-{month_in_quarter:02d}-01"
+ elif lookup_type == "month":
+ return f"{dt.year:04d}-{dt.month:02d}-01"
+ elif lookup_type == "week":
dt = dt - timedelta(days=dt.weekday())
- return f'{dt.year:04d}-{dt.month:02d}-{dt.day:02d}'
- elif lookup_type == 'day':
- return f'{dt.year:04d}-{dt.month:02d}-{dt.day:02d}'
- raise ValueError(f'Unsupported lookup type: {lookup_type!r}')
+ return f"{dt.year:04d}-{dt.month:02d}-{dt.day:02d}"
+ elif lookup_type == "day":
+ return f"{dt.year:04d}-{dt.month:02d}-{dt.day:02d}"
+ raise ValueError(f"Unsupported lookup type: {lookup_type!r}")
def _sqlite_time_trunc(lookup_type, dt, tzname, conn_tzname):
@@ -127,13 +149,13 @@ def _sqlite_time_trunc(lookup_type, dt, tzname, conn_tzname):
return None
else:
dt = dt_parsed
- if lookup_type == 'hour':
- return f'{dt.hour:02d}:00:00'
- elif lookup_type == 'minute':
- return f'{dt.hour:02d}:{dt.minute:02d}:00'
- elif lookup_type == 'second':
- return f'{dt.hour:02d}:{dt.minute:02d}:{dt.second:02d}'
- raise ValueError(f'Unsupported lookup type: {lookup_type!r}')
+ if lookup_type == "hour":
+ return f"{dt.hour:02d}:00:00"
+ elif lookup_type == "minute":
+ return f"{dt.hour:02d}:{dt.minute:02d}:00"
+ elif lookup_type == "second":
+ return f"{dt.hour:02d}:{dt.minute:02d}:{dt.second:02d}"
+ raise ValueError(f"Unsupported lookup type: {lookup_type!r}")
def _sqlite_datetime_cast_date(dt, tzname, conn_tzname):
@@ -154,15 +176,15 @@ def _sqlite_datetime_extract(lookup_type, dt, tzname=None, conn_tzname=None):
dt = _sqlite_datetime_parse(dt, tzname, conn_tzname)
if dt is None:
return None
- if lookup_type == 'week_day':
+ if lookup_type == "week_day":
return (dt.isoweekday() % 7) + 1
- elif lookup_type == 'iso_week_day':
+ elif lookup_type == "iso_week_day":
return dt.isoweekday()
- elif lookup_type == 'week':
+ elif lookup_type == "week":
return dt.isocalendar()[1]
- elif lookup_type == 'quarter':
+ elif lookup_type == "quarter":
return ceil(dt.month / 3)
- elif lookup_type == 'iso_year':
+ elif lookup_type == "iso_year":
return dt.isocalendar()[0]
else:
return getattr(dt, lookup_type)
@@ -172,25 +194,25 @@ def _sqlite_datetime_trunc(lookup_type, dt, tzname, conn_tzname):
dt = _sqlite_datetime_parse(dt, tzname, conn_tzname)
if dt is None:
return None
- if lookup_type == 'year':
- return f'{dt.year:04d}-01-01 00:00:00'
- elif lookup_type == 'quarter':
+ if lookup_type == "year":
+ return f"{dt.year:04d}-01-01 00:00:00"
+ elif lookup_type == "quarter":
month_in_quarter = dt.month - (dt.month - 1) % 3
- return f'{dt.year:04d}-{month_in_quarter:02d}-01 00:00:00'
- elif lookup_type == 'month':
- return f'{dt.year:04d}-{dt.month:02d}-01 00:00:00'
- elif lookup_type == 'week':
+ return f"{dt.year:04d}-{month_in_quarter:02d}-01 00:00:00"
+ elif lookup_type == "month":
+ return f"{dt.year:04d}-{dt.month:02d}-01 00:00:00"
+ elif lookup_type == "week":
dt = dt - timedelta(days=dt.weekday())
- return f'{dt.year:04d}-{dt.month:02d}-{dt.day:02d} 00:00:00'
- elif lookup_type == 'day':
- return f'{dt.year:04d}-{dt.month:02d}-{dt.day:02d} 00:00:00'
- elif lookup_type == 'hour':
- return f'{dt.year:04d}-{dt.month:02d}-{dt.day:02d} {dt.hour:02d}:00:00'
- elif lookup_type == 'minute':
- return f'{dt.year:04d}-{dt.month:02d}-{dt.day:02d} {dt.hour:02d}:{dt.minute:02d}:00'
- elif lookup_type == 'second':
- return f'{dt.year:04d}-{dt.month:02d}-{dt.day:02d} {dt.hour:02d}:{dt.minute:02d}:{dt.second:02d}'
- raise ValueError(f'Unsupported lookup type: {lookup_type!r}')
+ return f"{dt.year:04d}-{dt.month:02d}-{dt.day:02d} 00:00:00"
+ elif lookup_type == "day":
+ return f"{dt.year:04d}-{dt.month:02d}-{dt.day:02d} 00:00:00"
+ elif lookup_type == "hour":
+ return f"{dt.year:04d}-{dt.month:02d}-{dt.day:02d} {dt.hour:02d}:00:00"
+ elif lookup_type == "minute":
+ return f"{dt.year:04d}-{dt.month:02d}-{dt.day:02d} {dt.hour:02d}:{dt.minute:02d}:00"
+ elif lookup_type == "second":
+ return f"{dt.year:04d}-{dt.month:02d}-{dt.day:02d} {dt.hour:02d}:{dt.minute:02d}:{dt.second:02d}"
+ raise ValueError(f"Unsupported lookup type: {lookup_type!r}")
def _sqlite_time_extract(lookup_type, dt):
@@ -204,7 +226,7 @@ def _sqlite_time_extract(lookup_type, dt):
def _sqlite_prepare_dtdelta_param(conn, param):
- if conn in ['+', '-']:
+ if conn in ["+", "-"]:
if isinstance(param, int):
return timedelta(0, 0, param)
else:
@@ -227,13 +249,13 @@ def _sqlite_format_dtdelta(connector, lhs, rhs):
real_rhs = _sqlite_prepare_dtdelta_param(connector, rhs)
except (ValueError, TypeError):
return None
- if connector == '+':
+ if connector == "+":
# 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)
- elif connector == '-':
+ elif connector == "-":
out = str(real_lhs - real_rhs)
- elif connector == '*':
+ elif connector == "*":
out = real_lhs * real_rhs
else:
out = real_lhs / real_rhs
@@ -246,14 +268,14 @@ def _sqlite_time_diff(lhs, rhs):
left = typecast_time(lhs)
right = typecast_time(rhs)
return (
- (left.hour * 60 * 60 * 1000000) +
- (left.minute * 60 * 1000000) +
- (left.second * 1000000) +
- (left.microsecond) -
- (right.hour * 60 * 60 * 1000000) -
- (right.minute * 60 * 1000000) -
- (right.second * 1000000) -
- (right.microsecond)
+ (left.hour * 60 * 60 * 1000000)
+ + (left.minute * 60 * 1000000)
+ + (left.second * 1000000)
+ + (left.microsecond)
+ - (right.hour * 60 * 60 * 1000000)
+ - (right.minute * 60 * 1000000)
+ - (right.second * 1000000)
+ - (right.microsecond)
)
@@ -380,7 +402,7 @@ def _sqlite_pi():
def _sqlite_power(x, y):
if x is None or y is None:
return None
- return x ** y
+ return x**y
def _sqlite_radians(x):