Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implemented QuerySet.datetimes on Oracle.

  • Loading branch information...
commit 9c0859ff7c0b00734afe7fc15609d43d83215072 1 parent 68ab511
Aymeric Augustin aaugustin authored
3  django/contrib/gis/db/backends/oracle/compiler.py
View
@@ -20,3 +20,6 @@ class SQLAggregateCompiler(compiler.SQLAggregateCompiler, GeoSQLCompiler):
class SQLDateCompiler(compiler.SQLDateCompiler, GeoSQLCompiler):
pass
+
+class SQLDateTimeCompiler(compiler.SQLDateTimeCompiler, GeoSQLCompiler):
+ pass
39 django/db/backends/oracle/base.py
View
@@ -128,12 +128,11 @@ def cache_key_culling_sql(self):
"""
def date_extract_sql(self, lookup_type, field_name):
- # http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96540/functions42a.htm#1017163
if lookup_type == 'week_day':
# TO_CHAR(field, 'D') returns an integer from 1-7, where 1=Sunday.
return "TO_CHAR(%s, 'D')" % field_name
- else:
- return "EXTRACT(%s FROM %s)" % (lookup_type, field_name)
+ # http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions050.htm
+ return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name)
def date_interval_sql(self, sql, connector, timedelta):
"""
@@ -150,13 +149,35 @@ def date_interval_sql(self, sql, connector, timedelta):
timedelta.microseconds, day_precision)
def date_trunc_sql(self, lookup_type, field_name):
- # Oracle uses TRUNC() for both dates and numbers.
- # http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96540/functions155a.htm#SQLRF06151
- if lookup_type == 'day':
- sql = 'TRUNC(%s)' % field_name
+ # http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions230.htm#i1002084
+ if lookup_type in ('year', 'month'):
+ return "TRUNC(%s, '%s')" % (field_name, lookup_type.upper())
else:
- sql = "TRUNC(%s, '%s')" % (field_name, lookup_type)
- return sql
+ return "TRUNC(%s)" % field_name
+
+ def datetime_extract_sql(self, lookup_type, field_name):
+ if settings.USE_TZ:
+ field_name = "CAST((FROM_TZ(%s, 'UTC') AT TIME ZONE (%%s)) AS DATE)" % field_name
+ if lookup_type == 'week_day':
+ # TO_CHAR(field, 'D') returns an integer from 1-7, where 1=Sunday.
+ return "TO_CHAR(%s, 'D')" % field_name
+ # http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions050.htm
+ return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name)
+
+ def datetime_trunc_sql(self, lookup_type, field_name):
+ if settings.USE_TZ:
+ field_name = "CAST((FROM_TZ(%s, 'UTC') AT TIME ZONE (%%s)) AS DATE)" % field_name
+ # http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions230.htm#i1002084
+ if lookup_type in ('year', 'month'):
+ return "TRUNC(%s, '%s')" % (field_name, lookup_type.upper())
+ elif lookup_type == 'day':
+ return "TRUNC(%s)" % field_name
+ elif lookup_type == 'hour':
+ return "TRUNC(%s, 'HH24')" % field_name
+ elif lookup_type == 'minute':
+ return "TRUNC(%s, 'MI')" % field_name
+ else:
+ return field_name
def convert_values(self, value, field):
if isinstance(value, Database.LOB):
3  django/db/backends/oracle/compiler.py
View
@@ -71,3 +71,6 @@ class SQLAggregateCompiler(compiler.SQLAggregateCompiler, SQLCompiler):
class SQLDateCompiler(compiler.SQLDateCompiler, SQLCompiler):
pass
+
+class SQLDateTimeCompiler(compiler.SQLDateTimeCompiler, SQLCompiler):
+ pass
Please sign in to comment.
Something went wrong with that request. Please try again.