diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index 68ee950a825..2ae652a3e84 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -68,3 +68,10 @@ class BaseDatabaseOperations(object): the given specificity. """ raise NotImplementedError() + + def datetime_cast_sql(self): + """ + Returns the SQL necessary to cast a datetime value so that it will be + retrieved as a Python datetime object instead of a string. + """ + return None diff --git a/django/db/backends/ado_mssql/base.py b/django/db/backends/ado_mssql/base.py index 2c2d33c87f3..9d72433208b 100644 --- a/django/db/backends/ado_mssql/base.py +++ b/django/db/backends/ado_mssql/base.py @@ -97,9 +97,6 @@ def get_last_insert_id(cursor, table_name, pk_name): cursor.execute("SELECT %s FROM %s WHERE %s = @@IDENTITY" % (pk_name, table_name, pk_name)) return cursor.fetchone()[0] -def get_datetime_cast_sql(): - return None - def get_limit_offset_sql(limit, offset=None): # TODO: This is a guess. Make sure this is correct. sql = "LIMIT %s" % limit diff --git a/django/db/backends/dummy/base.py b/django/db/backends/dummy/base.py index a13b62169a7..95650ffbf7f 100644 --- a/django/db/backends/dummy/base.py +++ b/django/db/backends/dummy/base.py @@ -44,7 +44,6 @@ dictfetchone = complain dictfetchmany = complain dictfetchall = complain get_last_insert_id = complain -get_datetime_cast_sql = complain get_limit_offset_sql = complain get_random_function_sql = complain get_deferrable_sql = complain diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py index e426137251f..56976b77cc4 100644 --- a/django/db/backends/mysql/base.py +++ b/django/db/backends/mysql/base.py @@ -152,9 +152,6 @@ dictfetchall = util.dictfetchall def get_last_insert_id(cursor, table_name, pk_name): return cursor.lastrowid -def get_datetime_cast_sql(): - return None - def get_limit_offset_sql(limit, offset=None): sql = "LIMIT " if offset and offset != 0: diff --git a/django/db/backends/mysql_old/base.py b/django/db/backends/mysql_old/base.py index 579fc01f689..c7eea74b6d4 100644 --- a/django/db/backends/mysql_old/base.py +++ b/django/db/backends/mysql_old/base.py @@ -171,9 +171,6 @@ dictfetchall = util.dictfetchall def get_last_insert_id(cursor, table_name, pk_name): return cursor.lastrowid -def get_datetime_cast_sql(): - return None - def get_limit_offset_sql(limit, offset=None): sql = "LIMIT " if offset and offset != 0: diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py index 0534d786fba..8847368cee1 100644 --- a/django/db/backends/oracle/base.py +++ b/django/db/backends/oracle/base.py @@ -51,6 +51,9 @@ class DatabaseOperations(BaseDatabaseOperations): sql = "TRUNC(%s, '%s')" % (field_name, lookup_type) return sql + def datetime_cast_sql(self): + return "TO_TIMESTAMP(%s, 'YYYY-MM-DD HH24:MI:SS.FF')" + class DatabaseWrapper(BaseDatabaseWrapper): ops = DatabaseOperations() @@ -166,9 +169,6 @@ def get_last_insert_id(cursor, table_name, pk_name): cursor.execute('SELECT %s_sq.currval FROM dual' % sq_name) return cursor.fetchone()[0] -def get_datetime_cast_sql(): - return "TO_TIMESTAMP(%s, 'YYYY-MM-DD HH24:MI:SS.FF')" - def get_field_cast_sql(db_type): if db_type.endswith('LOB'): return "DBMS_LOB.SUBSTR(%s%s)" diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py index 5221ebf871e..8c06bb494e2 100644 --- a/django/db/backends/postgresql/base.py +++ b/django/db/backends/postgresql/base.py @@ -128,9 +128,6 @@ def get_last_insert_id(cursor, table_name, pk_name): cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name)) return cursor.fetchone()[0] -def get_datetime_cast_sql(): - return None - def get_limit_offset_sql(limit, offset=None): sql = "LIMIT %s" % limit if offset and offset != 0: diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py index d5ece11a9cb..6a471a4f049 100644 --- a/django/db/backends/postgresql_psycopg2/base.py +++ b/django/db/backends/postgresql_psycopg2/base.py @@ -82,9 +82,6 @@ def get_last_insert_id(cursor, table_name, pk_name): cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name)) return cursor.fetchone()[0] -def get_datetime_cast_sql(): - return None - def get_limit_offset_sql(limit, offset=None): sql = "LIMIT %s" % limit if offset and offset != 0: diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index 6caf01dcac4..3e99ac72c70 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -115,9 +115,6 @@ def _sqlite_extract(lookup_type, dt): return None return str(getattr(dt, lookup_type)) -def get_datetime_cast_sql(): - return None - def get_limit_offset_sql(limit, offset=None): sql = "LIMIT %s" % limit if offset and offset != 0: diff --git a/django/db/models/query.py b/django/db/models/query.py index d6ec1e29573..ec56a6ce1a0 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -780,8 +780,8 @@ def get_where_clause(lookup_type, table_prefix, field_name, value, db_type): if table_prefix.endswith('.'): table_prefix = backend.quote_name(table_prefix[:-1])+'.' field_name = backend.quote_name(field_name) - if type(value) == datetime.datetime and backend.get_datetime_cast_sql(): - cast_sql = backend.get_datetime_cast_sql() + if type(value) == datetime.datetime and connection.ops.datetime_cast_sql(): + cast_sql = connection.ops.datetime_cast_sql() else: cast_sql = '%s' if db_type and hasattr(backend, 'get_field_cast_sql'): @@ -794,8 +794,7 @@ def get_where_clause(lookup_type, table_prefix, field_name, value, db_type): else: format = '%s %s' try: - return format % (field_sql, - backend.OPERATOR_MAPPING[lookup_type] % cast_sql) + return format % (field_sql, backend.OPERATOR_MAPPING[lookup_type] % cast_sql) except KeyError: pass if lookup_type == 'in':