From 272b2175578c0ede4359fb10c96831b4aea95933 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Sun, 17 Jul 2005 18:23:34 +0000 Subject: [PATCH] Factored out database-specific date_extract behavior into dbmod.get_date_extract_sql(). Refs #46 git-svn-id: http://code.djangoproject.com/svn/django/trunk@159 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/db/__init__.py | 1 + django/core/db/backends/mysql.py | 4 ++++ django/core/db/backends/postgresql.py | 4 ++++ django/core/meta.py | 2 +- 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/django/core/db/__init__.py b/django/core/db/__init__.py index deede3d7ca..8c20ef4068 100644 --- a/django/core/db/__init__.py +++ b/django/core/db/__init__.py @@ -24,5 +24,6 @@ dictfetchmany = dbmod.dictfetchmany dictfetchall = dbmod.dictfetchall dictfetchall = dbmod.dictfetchall get_last_insert_id = dbmod.get_last_insert_id +get_date_extract_sql = dbmod.get_date_extract_sql OPERATOR_MAPPING = dbmod.OPERATOR_MAPPING DATA_TYPES = dbmod.DATA_TYPES diff --git a/django/core/db/backends/mysql.py b/django/core/db/backends/mysql.py index 3c0924e53f..001a32c191 100644 --- a/django/core/db/backends/mysql.py +++ b/django/core/db/backends/mysql.py @@ -62,6 +62,10 @@ def get_last_insert_id(cursor, table_name, pk_name): cursor.execute("SELECT LAST_INSERT_ID()") return cursor.fetchone()[0] +def get_date_extract_sql(lookup_type, table_name): + # lookup_type is 'year', 'month', 'day' + return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), table_name) + OPERATOR_MAPPING = { 'exact': '=', 'iexact': 'LIKE', diff --git a/django/core/db/backends/postgresql.py b/django/core/db/backends/postgresql.py index ded611376d..df7829f683 100644 --- a/django/core/db/backends/postgresql.py +++ b/django/core/db/backends/postgresql.py @@ -61,6 +61,10 @@ 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_date_extract_sql(lookup_type, table_name): + # lookup_type is 'year', 'month', 'day' + return "EXTRACT('%s' FROM %s)" % (lookup_type, table_name) + # Register these custom typecasts, because Django expects dates/times to be # in Python's native (standard-library) datetime/time format, whereas psycopg # use mx.DateTime by default. diff --git a/django/core/meta.py b/django/core/meta.py index 68ee1f2b22..8e7c22596e 100644 --- a/django/core/meta.py +++ b/django/core/meta.py @@ -1006,7 +1006,7 @@ def _get_where_clause(lookup_type, table_prefix, field_name, value): if lookup_type in ('range', 'year'): return '%s%s BETWEEN %%s AND %%s' % (table_prefix, field_name) elif lookup_type in ('month', 'day'): - return "EXTRACT('%s' FROM %s%s) = %%s" % (lookup_type, table_prefix, field_name) + return "%s = %%s" % db.get_date_extract_sql(lookup_type, table_prefix + field_name) elif lookup_type == 'isnull': return "%s%s IS %sNULL" % (table_prefix, field_name, (not value and 'NOT ' or '')) raise TypeError, "Got invalid lookup_type: %s" % repr(lookup_type)