summaryrefslogtreecommitdiff
path: root/django/db/models/sql
diff options
context:
space:
mode:
authorJustin Bronn <jbronn@gmail.com>2008-08-05 17:15:33 +0000
committerJustin Bronn <jbronn@gmail.com>2008-08-05 17:15:33 +0000
commitaa239e3e5405933af6a29dac3cf587b59a099927 (patch)
treeea2cbd139c9a8cf84c09e0b2008bff70e05927ef /django/db/models/sql
parent45b73c9a4685809236f84046cc7ffd32a50db958 (diff)
gis: Merged revisions 7981-8001,8003-8011,8013-8033,8035-8036,8038-8039,8041-8063,8065-8076,8078-8139,8141-8154,8156-8214 via svnmerge from trunk.archive/attic/gis
git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@8215 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/db/models/sql')
-rw-r--r--django/db/models/sql/query.py34
-rw-r--r--django/db/models/sql/where.py39
2 files changed, 51 insertions, 22 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index ef69d7657f..18dd8cc3f2 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -7,7 +7,6 @@ databases). The abstraction barrier only works one way: this module has to know
all about the internals of models in order to get the information it needs.
"""
-import datetime
from copy import deepcopy
from django.utils.tree import Node
@@ -107,6 +106,8 @@ class Query(object):
Pickling support.
"""
obj_dict = self.__dict__.copy()
+ obj_dict['related_select_fields'] = []
+ obj_dict['related_select_cols'] = []
del obj_dict['connection']
return obj_dict
@@ -196,14 +197,18 @@ class Query(object):
Returns an iterator over the results from executing this query.
"""
resolve_columns = hasattr(self, 'resolve_columns')
- if resolve_columns:
- if self.select_fields:
- fields = self.select_fields + self.related_select_fields
- else:
- fields = self.model._meta.fields
+ fields = None
for rows in self.execute_sql(MULTI):
for row in rows:
if resolve_columns:
+ if fields is None:
+ # We only set this up here because
+ # related_select_fields isn't populated until
+ # execute_sql() has been called.
+ if self.select_fields:
+ fields = self.select_fields + self.related_select_fields
+ else:
+ fields = self.model._meta.fields
row = self.resolve_columns(row, fields)
yield row
@@ -1088,20 +1093,23 @@ class Query(object):
join_it = iter(join_list)
table_it = iter(self.tables)
join_it.next(), table_it.next()
+ table_promote = False
for join in join_it:
table = table_it.next()
if join == table and self.alias_refcount[join] > 1:
continue
- self.promote_alias(join)
+ join_promote = self.promote_alias(join)
if table != join:
- self.promote_alias(table)
+ table_promote = self.promote_alias(table)
break
for join in join_it:
- self.promote_alias(join)
+ if self.promote_alias(join, join_promote):
+ join_promote = True
for table in table_it:
# Some of these will have been promoted from the join_list, but
# that's harmless.
- self.promote_alias(table)
+ if self.promote_alias(table, table_promote):
+ table_promote = True
self.where.add((alias, col, field, lookup_type, value), connector)
@@ -1214,7 +1222,6 @@ class Query(object):
raise MultiJoin(pos + 1)
if model:
# The field lives on a base class of the current model.
- alias_list = []
for int_model in opts.get_base_chain(model):
lhs_col = opts.parents[int_model].column
dedupe = lhs_col in opts.duplicate_targets
@@ -1618,8 +1625,9 @@ class Query(object):
if self.ordering_aliases:
result = order_modified_iter(cursor, len(self.ordering_aliases),
self.connection.features.empty_fetchmany_value)
- result = iter((lambda: cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE)),
- self.connection.features.empty_fetchmany_value)
+ else:
+ result = iter((lambda: cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE)),
+ self.connection.features.empty_fetchmany_value)
if not self.connection.features.can_use_chunked_reads:
# If we are using non-chunked reads, we return the same data
# structure as normally, but ensure it is all read into memory
diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py
index 18e4bf2f7e..662d99a4a2 100644
--- a/django/db/models/sql/where.py
+++ b/django/db/models/sql/where.py
@@ -35,20 +35,30 @@ class WhereNode(tree.Node):
storing any reference to field objects). Otherwise, the 'data' is
stored unchanged and can be anything with an 'as_sql()' method.
"""
+ # Because of circular imports, we need to import this here.
+ from django.db.models.base import ObjectDoesNotExist
+
if not isinstance(data, (list, tuple)):
super(WhereNode, self).add(data, connector)
return
alias, col, field, lookup_type, value = data
- if field:
- params = field.get_db_prep_lookup(lookup_type, value)
- db_type = field.db_type()
- else:
- # This is possible when we add a comparison to NULL sometimes (we
- # don't really need to waste time looking up the associated field
- # object).
- params = Field().get_db_prep_lookup(lookup_type, value)
- db_type = None
+ try:
+ if field:
+ params = field.get_db_prep_lookup(lookup_type, value)
+ db_type = field.db_type()
+ else:
+ # This is possible when we add a comparison to NULL sometimes
+ # (we don't really need to waste time looking up the associated
+ # field object).
+ params = Field().get_db_prep_lookup(lookup_type, value)
+ db_type = None
+ except ObjectDoesNotExist:
+ # This can happen when trying to insert a reference to a null pk.
+ # We break out of the normal path and indicate there's nothing to
+ # match.
+ super(WhereNode, self).add(NothingNode(), connector)
+ return
if isinstance(value, datetime.datetime):
annotation = datetime.datetime
else:
@@ -190,3 +200,14 @@ class EverythingNode(object):
def relabel_aliases(self, change_map, node=None):
return
+
+class NothingNode(object):
+ """
+ A node that matches nothing.
+ """
+ def as_sql(self, qn=None):
+ raise EmptyResultSet
+
+ def relabel_aliases(self, change_map, node=None):
+ return
+