Avoided storing ExpressionNodes in dicts

This commit is contained in:
Anssi Kääriäinen 2012-09-30 17:50:56 +03:00
parent ab2a1773fd
commit ddd7d1af20
1 changed files with 16 additions and 6 deletions

View File

@ -6,7 +6,7 @@ class SQLEvaluator(object):
def __init__(self, expression, query, allow_joins=True): def __init__(self, expression, query, allow_joins=True):
self.expression = expression self.expression = expression
self.opts = query.get_meta() self.opts = query.get_meta()
self.cols = {} self.cols = []
self.contains_aggregate = False self.contains_aggregate = False
self.expression.prepare(self, query, allow_joins) self.expression.prepare(self, query, allow_joins)
@ -18,11 +18,15 @@ class SQLEvaluator(object):
return self.expression.evaluate(self, qn, connection) return self.expression.evaluate(self, qn, connection)
def relabel_aliases(self, change_map): def relabel_aliases(self, change_map):
for node, col in self.cols.items(): new_cols = []
for node, col in self.cols:
if hasattr(col, "relabel_aliases"): if hasattr(col, "relabel_aliases"):
col.relabel_aliases(change_map) col.relabel_aliases(change_map)
new_cols.append((node, col))
else: else:
self.cols[node] = (change_map.get(col[0], col[0]), col[1]) new_cols.append((node,
(change_map.get(col[0], col[0]), col[1])))
self.cols = new_cols
##################################################### #####################################################
# Vistor methods for initial expression preparation # # Vistor methods for initial expression preparation #
@ -41,7 +45,7 @@ class SQLEvaluator(object):
if (len(field_list) == 1 and if (len(field_list) == 1 and
node.name in query.aggregate_select.keys()): node.name in query.aggregate_select.keys()):
self.contains_aggregate = True self.contains_aggregate = True
self.cols[node] = query.aggregate_select[node.name] self.cols.append((node, query.aggregate_select[node.name]))
else: else:
try: try:
field, source, opts, join_list, last, _ = query.setup_joins( field, source, opts, join_list, last, _ = query.setup_joins(
@ -49,7 +53,7 @@ class SQLEvaluator(object):
query.get_initial_alias(), False) query.get_initial_alias(), False)
col, _, join_list = query.trim_joins(source, join_list, last, False) col, _, join_list = query.trim_joins(source, join_list, last, False)
self.cols[node] = (join_list[-1], col) self.cols.append((node, (join_list[-1], col)))
except FieldDoesNotExist: except FieldDoesNotExist:
raise FieldError("Cannot resolve keyword %r into field. " raise FieldError("Cannot resolve keyword %r into field. "
"Choices are: %s" % (self.name, "Choices are: %s" % (self.name,
@ -80,7 +84,13 @@ class SQLEvaluator(object):
return connection.ops.combine_expression(node.connector, expressions), expression_params return connection.ops.combine_expression(node.connector, expressions), expression_params
def evaluate_leaf(self, node, qn, connection): def evaluate_leaf(self, node, qn, connection):
col = self.cols[node] col = None
for n, c in self.cols:
if n is node:
col = c
break
if col is None:
raise ValueError("Given node not found")
if hasattr(col, 'as_sql'): if hasattr(col, 'as_sql'):
return col.as_sql(qn, connection), () return col.as_sql(qn, connection), ()
else: else: