Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Factored out database-specific date_extract behavior into dbmod.get_d…

…ate_extract_sql(). Refs #46

git-svn-id: http://code.djangoproject.com/svn/django/trunk@159 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 272b2175578c0ede4359fb10c96831b4aea95933 1 parent 35e81ce
Adrian Holovaty adrianholovaty authored
1  django/core/db/__init__.py
View
@@ -24,5 +24,6 @@
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
4 django/core/db/backends/mysql.py
View
@@ -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',
4 django/core/db/backends/postgresql.py
View
@@ -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.
2  django/core/meta.py
View
@@ -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)
Please sign in to comment.
Something went wrong with that request. Please try again.