summaryrefslogtreecommitdiff
path: root/django/db/backends
diff options
context:
space:
mode:
authorSigurd Ljødal <sigurd@ljodal.no>2017-09-28 22:28:48 +0200
committerTim Graham <timograham@gmail.com>2018-08-18 13:09:15 -0400
commit3e09b37f80ab34cf57e245e1fcdabb3d4ff92a38 (patch)
tree7b56c5caeb4d6ebe6b5075c711c36a0572419c6f /django/db/backends
parentc832885a3e8659d4a704bf103d523b610c24e4ec (diff)
Fixed #28649 -- Added ExtractIsoYear database function and iso_year lookup.
Diffstat (limited to 'django/db/backends')
-rw-r--r--django/db/backends/mysql/operations.py4
-rw-r--r--django/db/backends/oracle/operations.py2
-rw-r--r--django/db/backends/postgresql/operations.py2
-rw-r--r--django/db/backends/sqlite3/base.py4
4 files changed, 12 insertions, 0 deletions
diff --git a/django/db/backends/mysql/operations.py b/django/db/backends/mysql/operations.py
index babf522e45..877c32b6a7 100644
--- a/django/db/backends/mysql/operations.py
+++ b/django/db/backends/mysql/operations.py
@@ -42,6 +42,10 @@ class DatabaseOperations(BaseDatabaseOperations):
# other database backends.
# Mode 3: Monday, 1-53, with 4 or more days this year.
return "WEEK(%s, 3)" % field_name
+ elif lookup_type == 'iso_year':
+ # Get the year part from the YEARWEEK function, which returns a
+ # number as year * 100 + week.
+ return "TRUNCATE(YEARWEEK(%s, 3), -2) / 100" % field_name
else:
# EXTRACT returns 1-53 based on ISO-8601 for the week number.
return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name)
diff --git a/django/db/backends/oracle/operations.py b/django/db/backends/oracle/operations.py
index 62830476bf..7018123cfa 100644
--- a/django/db/backends/oracle/operations.py
+++ b/django/db/backends/oracle/operations.py
@@ -69,6 +69,8 @@ END;
return "TO_CHAR(%s, 'IW')" % field_name
elif lookup_type == 'quarter':
return "TO_CHAR(%s, 'Q')" % field_name
+ elif lookup_type == 'iso_year':
+ return "TO_CHAR(%s, 'IYYY')" % field_name
else:
# https://docs.oracle.com/database/121/SQLRF/functions067.htm#SQLRF00639
return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name)
diff --git a/django/db/backends/postgresql/operations.py b/django/db/backends/postgresql/operations.py
index f4e9571f81..b1b83861c1 100644
--- a/django/db/backends/postgresql/operations.py
+++ b/django/db/backends/postgresql/operations.py
@@ -31,6 +31,8 @@ class DatabaseOperations(BaseDatabaseOperations):
if lookup_type == 'week_day':
# For consistency across backends, we return Sunday=1, Saturday=7.
return "EXTRACT('dow' FROM %s) + 1" % field_name
+ elif lookup_type == 'iso_year':
+ return "EXTRACT('isoyear' FROM %s)" % field_name
else:
return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name)
diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py
index 20905c83b8..d124fcd47b 100644
--- a/django/db/backends/sqlite3/base.py
+++ b/django/db/backends/sqlite3/base.py
@@ -338,6 +338,8 @@ def _sqlite_date_extract(lookup_type, dt):
return dt.isocalendar()[1]
elif lookup_type == 'quarter':
return math.ceil(dt.month / 3)
+ elif lookup_type == 'iso_year':
+ return dt.isocalendar()[0]
else:
return getattr(dt, lookup_type)
@@ -410,6 +412,8 @@ def _sqlite_datetime_extract(lookup_type, dt, tzname):
return dt.isocalendar()[1]
elif lookup_type == 'quarter':
return math.ceil(dt.month / 3)
+ elif lookup_type == 'iso_year':
+ return dt.isocalendar()[0]
else:
return getattr(dt, lookup_type)