Changed __year lookup to use a BETWEEN SQL statement instead of comparing the result of EXTRACT(year). This should be more efficient.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4505 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2007-02-14 06:32:32 +00:00
parent 9efa60dafb
commit 4c4209b144
3 changed files with 29 additions and 17 deletions

View File

@ -164,7 +164,7 @@ class Field(object):
def get_db_prep_lookup(self, lookup_type, value): def get_db_prep_lookup(self, lookup_type, value):
"Returns field's value prepared for database lookup." "Returns field's value prepared for database lookup."
if lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte', 'year', 'month', 'day', 'search'): if lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte', 'month', 'day', 'search'):
return [value] return [value]
elif lookup_type in ('range', 'in'): elif lookup_type in ('range', 'in'):
return value return value
@ -178,7 +178,13 @@ class Field(object):
return ["%%%s" % prep_for_like_query(value)] return ["%%%s" % prep_for_like_query(value)]
elif lookup_type == 'isnull': elif lookup_type == 'isnull':
return [] return []
raise TypeError, "Field has invalid lookup: %s" % lookup_type elif lookup_type == 'year':
try:
value = int(value)
except ValueError:
raise ValueError("The __year lookup type requires an integer argument")
return ['%s-01-01 00:00:00' % value, '%s-12-31 23:59:59.999999' % value]
raise TypeError("Field has invalid lookup: %s" % lookup_type)
def has_default(self): def has_default(self):
"Returns a boolean of whether this field has a default value." "Returns a boolean of whether this field has a default value."

View File

@ -708,9 +708,9 @@ def get_where_clause(lookup_type, table_prefix, field_name, value):
return '%s%s IN (%s)' % (table_prefix, field_name, in_string) return '%s%s IN (%s)' % (table_prefix, field_name, in_string)
else: else:
raise EmptyResultSet raise EmptyResultSet
elif lookup_type == 'range': elif 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 ('year', 'month', 'day'): elif lookup_type in ('month', 'day'):
return "%s = %%s" % backend.get_date_extract_sql(lookup_type, table_prefix + field_name) return "%s = %%s" % backend.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 ''))

View File

@ -319,7 +319,6 @@ AttributeError: Manager isn't accessible via Article instances
>>> Article.objects.filter(id__lte=4).delete() >>> Article.objects.filter(id__lte=4).delete()
>>> Article.objects.all() >>> Article.objects.all()
[<Article: Article 6>, <Article: Default headline>, <Article: Article 7>, <Article: Updated article 8>] [<Article: Article 6>, <Article: Default headline>, <Article: Article 7>, <Article: Updated article 8>]
"""} """}
from django.conf import settings from django.conf import settings
@ -358,4 +357,11 @@ __test__['API_TESTS'] += """
>>> a10 = Article.objects.create(headline="Article 10", pub_date=datetime(2005, 7, 31, 12, 30, 45)) >>> a10 = Article.objects.create(headline="Article 10", pub_date=datetime(2005, 7, 31, 12, 30, 45))
>>> Article.objects.get(headline="Article 10") >>> Article.objects.get(headline="Article 10")
<Article: Article 10> <Article: Article 10>
# Edge-case test: A year lookup should retrieve all objects in the given
year, including Jan. 1 and Dec. 31.
>>> a11 = Article.objects.create(headline='Article 11', pub_date=datetime(2008, 1, 1))
>>> a12 = Article.objects.create(headline='Article 12', pub_date=datetime(2008, 12, 31, 23, 59, 59, 999999))
>>> Article.objects.filter(pub_date__year=2008)
[<Article: Article 11>, <Article: Article 12>]
""" """