Fixed #3463 -- EmptyQuerySet's iterator() now returns a generator. Thanks, Gary Wilson

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4475 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2007-02-10 05:38:38 +00:00
parent 1aa1c4672c
commit 007f17d63e
2 changed files with 19 additions and 17 deletions

View File

@ -167,17 +167,16 @@ class QuerySet(object):
def iterator(self): def iterator(self):
"Performs the SELECT database lookup of this QuerySet." "Performs the SELECT database lookup of this QuerySet."
# self._select is a dictionary, and dictionaries' key order is
# undefined, so we convert it to a list of tuples.
extra_select = self._select.items()
cursor = connection.cursor()
try: try:
select, sql, params = self._get_sql_clause() select, sql, params = self._get_sql_clause()
except EmptyResultSet: except EmptyResultSet:
raise StopIteration raise StopIteration
# self._select is a dictionary, and dictionaries' key order is
# undefined, so we convert it to a list of tuples.
extra_select = self._select.items()
cursor = connection.cursor()
cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params)
fill_cache = self._select_related fill_cache = self._select_related
index_end = len(self.model._meta.fields) index_end = len(self.model._meta.fields)
@ -523,11 +522,18 @@ class QuerySet(object):
return select, " ".join(sql), params return select, " ".join(sql), params
class ValuesQuerySet(QuerySet): class ValuesQuerySet(QuerySet):
def iterator(self): def __init__(self, *args, **kwargs):
super(ValuesQuerySet, self).__init__(*args, **kwargs)
# select_related and select aren't supported in values(). # select_related and select aren't supported in values().
self._select_related = False self._select_related = False
self._select = {} self._select = {}
def iterator(self):
try:
select, sql, params = self._get_sql_clause()
except EmptyResultSet:
raise StopIteration
# self._fields is a list of field names to fetch. # self._fields is a list of field names to fetch.
if self._fields: if self._fields:
columns = [self.model._meta.get_field(f, many_to_many=False).column for f in self._fields] columns = [self.model._meta.get_field(f, many_to_many=False).column for f in self._fields]
@ -536,14 +542,8 @@ class ValuesQuerySet(QuerySet):
columns = [f.column for f in self.model._meta.fields] columns = [f.column for f in self.model._meta.fields]
field_names = [f.attname for f in self.model._meta.fields] field_names = [f.attname for f in self.model._meta.fields]
cursor = connection.cursor()
try:
select, sql, params = self._get_sql_clause()
except EmptyResultSet:
raise StopIteration
select = ['%s.%s' % (backend.quote_name(self.model._meta.db_table), backend.quote_name(c)) for c in columns] select = ['%s.%s' % (backend.quote_name(self.model._meta.db_table), backend.quote_name(c)) for c in columns]
cursor = connection.cursor()
cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params)
while 1: while 1:
rows = cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE) rows = cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE)
@ -592,9 +592,6 @@ class EmptyQuerySet(QuerySet):
super(EmptyQuerySet, self).__init__(model) super(EmptyQuerySet, self).__init__(model)
self._result_cache = [] self._result_cache = []
def iterator(self):
raise StopIteration
def count(self): def count(self):
return 0 return 0
@ -606,6 +603,9 @@ class EmptyQuerySet(QuerySet):
c._result_cache = [] c._result_cache = []
return c return c
def _get_sql_clause(self):
raise EmptyResultSet
class QOperator(object): class QOperator(object):
"Base class for QAnd and QOr" "Base class for QAnd and QOr"
def __init__(self, *args): def __init__(self, *args):

View File

@ -198,6 +198,8 @@ DoesNotExist: Article matching query does not exist.
[] []
>>> Article.objects.none().count() >>> Article.objects.none().count()
0 0
>>> [article for article in Article.objects.none().iterator()]
[]
# using __in with an empty list should return an empty query set # using __in with an empty list should return an empty query set
>>> Article.objects.filter(id__in=[]) >>> Article.objects.filter(id__in=[])