summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorJosh Smeaton <josh.smeaton@gmail.com>2015-05-23 18:12:09 +1000
committerTim Graham <timograham@gmail.com>2015-05-30 20:41:33 -0400
commitc7805ee214802ff1c0de53660bd2594bc1abfebb (patch)
tree16a61581b713de26fdafea5f89f2645890535b27 /django
parente60cce4e401ae9481b66b6c7821e85237f1baa1a (diff)
Fixed #24699 -- Added aggregate support for DurationField on Oracle
Diffstat (limited to 'django')
-rw-r--r--django/db/backends/base/features.py3
-rw-r--r--django/db/backends/oracle/features.py1
-rw-r--r--django/db/backends/oracle/functions.py24
-rw-r--r--django/db/models/aggregates.py18
4 files changed, 42 insertions, 4 deletions
diff --git a/django/db/backends/base/features.py b/django/db/backends/base/features.py
index 8c7b31e471..e0a7e49512 100644
--- a/django/db/backends/base/features.py
+++ b/django/db/backends/base/features.py
@@ -157,9 +157,6 @@ class BaseDatabaseFeatures(object):
# Support for the DISTINCT ON clause
can_distinct_on_fields = False
- # Can the backend use an Avg aggregate on DurationField?
- can_avg_on_durationfield = True
-
# Does the backend decide to commit before SAVEPOINT statements
# when autocommit is disabled? http://bugs.python.org/issue8145#msg109965
autocommits_when_autocommit_is_off = False
diff --git a/django/db/backends/oracle/features.py b/django/db/backends/oracle/features.py
index 2be00acd99..1ef0f232e5 100644
--- a/django/db/backends/oracle/features.py
+++ b/django/db/backends/oracle/features.py
@@ -39,7 +39,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
uppercases_column_names = True
# select for update with limit can be achieved on Oracle, but not with the current backend.
supports_select_for_update_with_limit = False
- can_avg_on_durationfield = False # Pending implementation (#24699).
def introspected_boolean_field_type(self, field=None, created_separately=False):
"""
diff --git a/django/db/backends/oracle/functions.py b/django/db/backends/oracle/functions.py
new file mode 100644
index 0000000000..384f092fd4
--- /dev/null
+++ b/django/db/backends/oracle/functions.py
@@ -0,0 +1,24 @@
+from django.db.models import DecimalField, DurationField, Func
+
+
+class IntervalToSeconds(Func):
+ function = ''
+ template = """
+ EXTRACT(day from %(expressions)s) * 86400 +
+ EXTRACT(hour from %(expressions)s) * 3600 +
+ EXTRACT(minute from %(expressions)s) * 60 +
+ EXTRACT(second from %(expressions)s)
+ """
+
+ def __init__(self, expression, **extra):
+ output_field = extra.pop('output_field', DecimalField())
+ super(IntervalToSeconds, self).__init__(expression, output_field=output_field, **extra)
+
+
+class SecondsToInterval(Func):
+ function = 'NUMTODSINTERVAL'
+ template = "%(function)s(%(expressions)s, 'SECOND')"
+
+ def __init__(self, expression, **extra):
+ output_field = extra.pop('output_field', DurationField())
+ super(SecondsToInterval, self).__init__(expression, output_field=output_field, **extra)
diff --git a/django/db/models/aggregates.py b/django/db/models/aggregates.py
index 2d7c43c90e..08c9169c6c 100644
--- a/django/db/models/aggregates.py
+++ b/django/db/models/aggregates.py
@@ -78,6 +78,15 @@ class Avg(Aggregate):
output_field = extra.pop('output_field', FloatField())
super(Avg, self).__init__(expression, output_field=output_field, **extra)
+ def as_oracle(self, compiler, connection):
+ if self.output_field.get_internal_type() == 'DurationField':
+ expression = self.get_source_expressions()[0]
+ from django.db.backends.oracle.functions import IntervalToSeconds, SecondsToInterval
+ return compiler.compile(
+ SecondsToInterval(Avg(IntervalToSeconds(expression)))
+ )
+ return super(Avg, self).as_sql(compiler, connection)
+
class Count(Aggregate):
function = 'COUNT'
@@ -137,6 +146,15 @@ class Sum(Aggregate):
function = 'SUM'
name = 'Sum'
+ def as_oracle(self, compiler, connection):
+ if self.output_field.get_internal_type() == 'DurationField':
+ expression = self.get_source_expressions()[0]
+ from django.db.backends.oracle.functions import IntervalToSeconds, SecondsToInterval
+ return compiler.compile(
+ SecondsToInterval(Sum(IntervalToSeconds(expression)))
+ )
+ return super(Sum, self).as_sql(compiler, connection)
+
class Variance(Aggregate):
name = 'Variance'