From 29f67dce9efd048b915d70f905ee1c9d2bda5de9 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Sun, 19 Aug 2007 22:55:05 +0000 Subject: [PATCH] Refactored get_datetime_cast_sql() to DatabaseOperations.datetime_cast_sql(). Refs #5106 git-svn-id: http://code.djangoproject.com/svn/django/trunk@5953 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/backends/__init__.py | 7 +++++++ django/db/backends/ado_mssql/base.py | 3 --- django/db/backends/dummy/base.py | 1 - django/db/backends/mysql/base.py | 3 --- django/db/backends/mysql_old/base.py | 3 --- django/db/backends/oracle/base.py | 6 +++--- django/db/backends/postgresql/base.py | 3 --- django/db/backends/postgresql_psycopg2/base.py | 3 --- django/db/backends/sqlite3/base.py | 3 --- django/db/models/query.py | 7 +++---- 10 files changed, 13 insertions(+), 26 deletions(-) diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index 68ee950a82..2ae652a3e8 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 2c2d33c87f..9d72433208 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 a13b62169a..95650ffbf7 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 e426137251..56976b77cc 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 579fc01f68..c7eea74b6d 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 0534d786fb..8847368cee 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 5221ebf871..8c06bb494e 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 d5ece11a9c..6a471a4f04 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 6caf01dcac..3e99ac72c7 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 d6ec1e2957..ec56a6ce1a 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':