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
This commit is contained in:
Adrian Holovaty 2005-07-17 18:23:34 +00:00
parent 35e81ce5af
commit 272b217557
4 changed files with 10 additions and 1 deletions

View File

@ -24,5 +24,6 @@ dictfetchmany = dbmod.dictfetchmany
dictfetchall = dbmod.dictfetchall dictfetchall = dbmod.dictfetchall
dictfetchall = dbmod.dictfetchall dictfetchall = dbmod.dictfetchall
get_last_insert_id = dbmod.get_last_insert_id get_last_insert_id = dbmod.get_last_insert_id
get_date_extract_sql = dbmod.get_date_extract_sql
OPERATOR_MAPPING = dbmod.OPERATOR_MAPPING OPERATOR_MAPPING = dbmod.OPERATOR_MAPPING
DATA_TYPES = dbmod.DATA_TYPES DATA_TYPES = dbmod.DATA_TYPES

View File

@ -62,6 +62,10 @@ def get_last_insert_id(cursor, table_name, pk_name):
cursor.execute("SELECT LAST_INSERT_ID()") cursor.execute("SELECT LAST_INSERT_ID()")
return cursor.fetchone()[0] 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 = { OPERATOR_MAPPING = {
'exact': '=', 'exact': '=',
'iexact': 'LIKE', 'iexact': 'LIKE',

View File

@ -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)) cursor.execute("SELECT CURRVAL('%s_%s_seq')" % (table_name, pk_name))
return cursor.fetchone()[0] 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 # Register these custom typecasts, because Django expects dates/times to be
# in Python's native (standard-library) datetime/time format, whereas psycopg # in Python's native (standard-library) datetime/time format, whereas psycopg
# use mx.DateTime by default. # use mx.DateTime by default.

View File

@ -1006,7 +1006,7 @@ def _get_where_clause(lookup_type, table_prefix, field_name, value):
if lookup_type in ('range', 'year'): if lookup_type in ('range', 'year'):
return '%s%s BETWEEN %%s AND %%s' % (table_prefix, field_name) return '%s%s BETWEEN %%s AND %%s' % (table_prefix, field_name)
elif lookup_type in ('month', 'day'): 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': elif lookup_type == 'isnull':
return "%s%s IS %sNULL" % (table_prefix, field_name, (not value and 'NOT ' or '')) return "%s%s IS %sNULL" % (table_prefix, field_name, (not value and 'NOT ' or ''))
raise TypeError, "Got invalid lookup_type: %s" % repr(lookup_type) raise TypeError, "Got invalid lookup_type: %s" % repr(lookup_type)