1
0
mirror of https://github.com/django/django.git synced 2025-07-05 18:29:11 +00:00

queryset-refactor: Make sure the right list of fields is passed to the

Query.resolve_columns() method for those backends that provide it (e.g. Oracle).

Refs #7088


git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@7469 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2008-04-26 11:20:42 +00:00
parent bb132aa395
commit 3933aff6cc
2 changed files with 21 additions and 3 deletions

View File

@ -54,6 +54,8 @@ class Query(object):
self.standard_ordering = True self.standard_ordering = True
self.ordering_aliases = [] self.ordering_aliases = []
self.start_meta = None self.start_meta = None
self.select_fields = []
self.related_select_fields = []
# SQL-related attributes # SQL-related attributes
self.select = [] self.select = []
@ -142,6 +144,8 @@ class Query(object):
obj.standard_ordering = self.standard_ordering obj.standard_ordering = self.standard_ordering
obj.ordering_aliases = [] obj.ordering_aliases = []
obj.start_meta = self.start_meta obj.start_meta = self.start_meta
obj.select_fields = self.select_fields[:]
obj.related_select_fields = self.related_select_fields[:]
obj.select = self.select[:] obj.select = self.select[:]
obj.tables = self.tables[:] obj.tables = self.tables[:]
obj.where = deepcopy(self.where) obj.where = deepcopy(self.where)
@ -169,8 +173,12 @@ class Query(object):
""" """
Returns an iterator over the results from executing this query. Returns an iterator over the results from executing this query.
""" """
fields = self.model._meta.fields
resolve_columns = hasattr(self, 'resolve_columns') 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
for rows in self.execute_sql(MULTI): for rows in self.execute_sql(MULTI):
for row in rows: for row in rows:
if resolve_columns: if resolve_columns:
@ -187,6 +195,7 @@ class Query(object):
obj.clear_limits() obj.clear_limits()
obj.select_related = False obj.select_related = False
obj.related_select_cols = [] obj.related_select_cols = []
obj.related_select_fields = []
if obj.distinct and len(obj.select) > 1: if obj.distinct and len(obj.select) > 1:
obj = self.clone(CountQuery, _query=obj, where=self.where_class(), obj = self.clone(CountQuery, _query=obj, where=self.where_class(),
distinct=False) distinct=False)
@ -199,7 +208,8 @@ class Query(object):
number = data[0] number = data[0]
# Apply offset and limit constraints manually, since using LIMIT/OFFSET # Apply offset and limit constraints manually, since using LIMIT/OFFSET
# in SQL doesn't change the COUNT output. # in SQL (in variants that provide them) doesn't change the COUNT
# output.
number = max(0, number - self.low_mark) number = max(0, number - self.low_mark)
if self.high_mark: if self.high_mark:
number = min(number, self.high_mark - self.low_mark) number = min(number, self.high_mark - self.low_mark)
@ -333,6 +343,7 @@ class Query(object):
item = deepcopy(col) item = deepcopy(col)
item.relabel_aliases(change_map) item.relabel_aliases(change_map)
self.select.append(item) self.select.append(item)
self.select_fields = rhs.select_fields[:]
self.extra_select = rhs.extra_select.copy() self.extra_select = rhs.extra_select.copy()
self.extra_tables = rhs.extra_tables self.extra_tables = rhs.extra_tables
self.extra_where = rhs.extra_where self.extra_where = rhs.extra_where
@ -835,6 +846,7 @@ class Query(object):
opts = self.get_meta() opts = self.get_meta()
root_alias = self.get_initial_alias() root_alias = self.get_initial_alias()
self.related_select_cols = [] self.related_select_cols = []
self.related_select_fields = []
if not used: if not used:
used = set() used = set()
@ -869,6 +881,7 @@ class Query(object):
used.add(alias) used.add(alias)
self.related_select_cols.extend([(alias, f2.column) self.related_select_cols.extend([(alias, f2.column)
for f2 in f.rel.to._meta.fields]) for f2 in f.rel.to._meta.fields])
self.related_select_fields.extend(f.rel.to._meta.fields)
if restricted: if restricted:
next = requested.get(f.name, {}) next = requested.get(f.name, {})
else: else:
@ -1237,7 +1250,7 @@ class Query(object):
opts = self.get_meta() opts = self.get_meta()
try: try:
for name in field_names: for name in field_names:
u1, target, u2, joins, u3 = self.setup_joins( field, target, u2, joins, u3 = self.setup_joins(
name.split(LOOKUP_SEP), opts, alias, False, allow_m2m, name.split(LOOKUP_SEP), opts, alias, False, allow_m2m,
True) True)
final_alias = joins[-1] final_alias = joins[-1]
@ -1254,6 +1267,7 @@ class Query(object):
# doing unnecessary left outer joins here. # doing unnecessary left outer joins here.
self.promote_alias(join) self.promote_alias(join)
self.select.append((final_alias, col)) self.select.append((final_alias, col))
self.select_fields.append(field)
except MultiJoin: except MultiJoin:
raise FieldError("Invalid field name: '%s'" % name) raise FieldError("Invalid field name: '%s'" % name)
except FieldError: except FieldError:
@ -1322,6 +1336,7 @@ class Query(object):
# level. # level.
self.distinct = False self.distinct = False
self.select = [select] self.select = [select]
self.select_fields = [None]
self.extra_select = {} self.extra_select = {}
self.extra_select_params = () self.extra_select_params = ()
@ -1338,6 +1353,7 @@ class Query(object):
d = d.setdefault(part, {}) d = d.setdefault(part, {})
self.select_related = field_dict self.select_related = field_dict
self.related_select_cols = [] self.related_select_cols = []
self.related_select_fields = []
def add_extra(self, select, select_params, where, params, tables, order_by): def add_extra(self, select, select_params, where, params, tables, order_by):
""" """
@ -1390,6 +1406,7 @@ class Query(object):
start.split(LOOKUP_SEP), opts, alias, False) start.split(LOOKUP_SEP), opts, alias, False)
alias = joins[last[-1]] alias = joins[last[-1]]
self.select = [(alias, self.alias_map[alias][RHS_JOIN_COL])] self.select = [(alias, self.alias_map[alias][RHS_JOIN_COL])]
self.select_fields = [field]
self.start_meta = opts self.start_meta = opts
# The call to setup_joins add an extra reference to everything in # The call to setup_joins add an extra reference to everything in

View File

@ -348,6 +348,7 @@ class DateQuery(Query):
select = Date((alias, column), lookup_type, select = Date((alias, column), lookup_type,
self.connection.ops.date_trunc_sql) self.connection.ops.date_trunc_sql)
self.select = [select] self.select = [select]
self.select_fields = [None]
self.distinct = True self.distinct = True
self.order_by = order == 'ASC' and [1] or [-1] self.order_by = order == 'ASC' and [1] or [-1]