2013-07-08 08:39:54 +08:00
|
|
|
import copy
|
|
|
|
|
2009-01-29 18:46:36 +08:00
|
|
|
from django.core.exceptions import FieldError
|
2012-09-09 07:51:36 +08:00
|
|
|
from django.db.models.constants import LOOKUP_SEP
|
2009-01-29 18:46:36 +08:00
|
|
|
from django.db.models.fields import FieldDoesNotExist
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-01-29 18:46:36 +08:00
|
|
|
|
|
|
|
class SQLEvaluator(object):
|
2012-12-21 03:25:48 +08:00
|
|
|
def __init__(self, expression, query, allow_joins=True, reuse=None):
|
2009-01-29 18:46:36 +08:00
|
|
|
self.expression = expression
|
|
|
|
self.opts = query.get_meta()
|
2012-11-23 02:27:28 +08:00
|
|
|
self.reuse = reuse
|
Refactored qs.add_q() and utils/tree.py
The sql/query.py add_q method did a lot of where/having tree hacking to
get complex queries to work correctly. The logic was refactored so that
it should be simpler to understand. The new logic should also produce
leaner WHERE conditions.
The changes cascade somewhat, as some other parts of Django (like
add_filter() and WhereNode) expect boolean trees in certain format or
they fail to work. So to fix the add_q() one must fix utils/tree.py,
some things in add_filter(), WhereNode and so on.
This commit also fixed add_filter to see negate clauses up the path.
A query like .exclude(Q(reversefk__in=a_list)) didn't work similarly to
.filter(~Q(reversefk__in=a_list)). The reason for this is that only
the immediate parent negate clauses were seen by add_filter, and thus a
tree like AND: (NOT AND: (AND: condition)) will not be handled
correctly, as there is one intermediary AND node in the tree. The
example tree is generated by .exclude(~Q(reversefk__in=a_list)).
Still, aggregation lost connectors in OR cases, and F() objects and
aggregates in same filter clause caused GROUP BY problems on some
databases.
Fixed #17600, fixed #13198, fixed #17025, fixed #17000, fixed #11293.
2012-05-25 05:27:24 +08:00
|
|
|
self.cols = []
|
2009-01-29 18:46:36 +08:00
|
|
|
self.expression.prepare(self, query, allow_joins)
|
|
|
|
|
2013-03-02 07:06:56 +08:00
|
|
|
def relabeled_clone(self, change_map):
|
|
|
|
clone = copy.copy(self)
|
|
|
|
clone.cols = []
|
Refactored qs.add_q() and utils/tree.py
The sql/query.py add_q method did a lot of where/having tree hacking to
get complex queries to work correctly. The logic was refactored so that
it should be simpler to understand. The new logic should also produce
leaner WHERE conditions.
The changes cascade somewhat, as some other parts of Django (like
add_filter() and WhereNode) expect boolean trees in certain format or
they fail to work. So to fix the add_q() one must fix utils/tree.py,
some things in add_filter(), WhereNode and so on.
This commit also fixed add_filter to see negate clauses up the path.
A query like .exclude(Q(reversefk__in=a_list)) didn't work similarly to
.filter(~Q(reversefk__in=a_list)). The reason for this is that only
the immediate parent negate clauses were seen by add_filter, and thus a
tree like AND: (NOT AND: (AND: condition)) will not be handled
correctly, as there is one intermediary AND node in the tree. The
example tree is generated by .exclude(~Q(reversefk__in=a_list)).
Still, aggregation lost connectors in OR cases, and F() objects and
aggregates in same filter clause caused GROUP BY problems on some
databases.
Fixed #17600, fixed #13198, fixed #17025, fixed #17000, fixed #11293.
2012-05-25 05:27:24 +08:00
|
|
|
for node, col in self.cols:
|
2013-03-02 07:06:56 +08:00
|
|
|
if hasattr(col, 'relabeled_clone'):
|
|
|
|
clone.cols.append((node, col.relabeled_clone(change_map)))
|
|
|
|
else:
|
|
|
|
clone.cols.append((node,
|
|
|
|
(change_map.get(col[0], col[0]), col[1])))
|
|
|
|
return clone
|
|
|
|
|
2014-01-18 17:09:43 +08:00
|
|
|
def get_group_by_cols(self):
|
Refactored qs.add_q() and utils/tree.py
The sql/query.py add_q method did a lot of where/having tree hacking to
get complex queries to work correctly. The logic was refactored so that
it should be simpler to understand. The new logic should also produce
leaner WHERE conditions.
The changes cascade somewhat, as some other parts of Django (like
add_filter() and WhereNode) expect boolean trees in certain format or
they fail to work. So to fix the add_q() one must fix utils/tree.py,
some things in add_filter(), WhereNode and so on.
This commit also fixed add_filter to see negate clauses up the path.
A query like .exclude(Q(reversefk__in=a_list)) didn't work similarly to
.filter(~Q(reversefk__in=a_list)). The reason for this is that only
the immediate parent negate clauses were seen by add_filter, and thus a
tree like AND: (NOT AND: (AND: condition)) will not be handled
correctly, as there is one intermediary AND node in the tree. The
example tree is generated by .exclude(~Q(reversefk__in=a_list)).
Still, aggregation lost connectors in OR cases, and F() objects and
aggregates in same filter clause caused GROUP BY problems on some
databases.
Fixed #17600, fixed #13198, fixed #17025, fixed #17000, fixed #11293.
2012-05-25 05:27:24 +08:00
|
|
|
cols = []
|
|
|
|
for node, col in self.cols:
|
2014-01-18 17:09:43 +08:00
|
|
|
if hasattr(node, 'get_group_by_cols'):
|
|
|
|
cols.extend(node.get_group_by_cols())
|
Refactored qs.add_q() and utils/tree.py
The sql/query.py add_q method did a lot of where/having tree hacking to
get complex queries to work correctly. The logic was refactored so that
it should be simpler to understand. The new logic should also produce
leaner WHERE conditions.
The changes cascade somewhat, as some other parts of Django (like
add_filter() and WhereNode) expect boolean trees in certain format or
they fail to work. So to fix the add_q() one must fix utils/tree.py,
some things in add_filter(), WhereNode and so on.
This commit also fixed add_filter to see negate clauses up the path.
A query like .exclude(Q(reversefk__in=a_list)) didn't work similarly to
.filter(~Q(reversefk__in=a_list)). The reason for this is that only
the immediate parent negate clauses were seen by add_filter, and thus a
tree like AND: (NOT AND: (AND: condition)) will not be handled
correctly, as there is one intermediary AND node in the tree. The
example tree is generated by .exclude(~Q(reversefk__in=a_list)).
Still, aggregation lost connectors in OR cases, and F() objects and
aggregates in same filter clause caused GROUP BY problems on some
databases.
Fixed #17600, fixed #13198, fixed #17025, fixed #17000, fixed #11293.
2012-05-25 05:27:24 +08:00
|
|
|
elif isinstance(col, tuple):
|
|
|
|
cols.append(col)
|
|
|
|
return cols
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def prepare(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def as_sql(self, qn, connection):
|
|
|
|
return self.expression.evaluate(self, qn, connection)
|
2009-01-29 18:46:36 +08:00
|
|
|
|
|
|
|
#####################################################
|
2014-04-27 01:18:45 +08:00
|
|
|
# Visitor methods for initial expression preparation #
|
2009-01-29 18:46:36 +08:00
|
|
|
#####################################################
|
|
|
|
|
|
|
|
def prepare_node(self, node, query, allow_joins):
|
|
|
|
for child in node.children:
|
|
|
|
if hasattr(child, 'prepare'):
|
|
|
|
child.prepare(self, query, allow_joins)
|
|
|
|
|
|
|
|
def prepare_leaf(self, node, query, allow_joins):
|
|
|
|
if not allow_joins and LOOKUP_SEP in node.name:
|
|
|
|
raise FieldError("Joined field references are not permitted in this query")
|
|
|
|
|
|
|
|
field_list = node.name.split(LOOKUP_SEP)
|
Refactored qs.add_q() and utils/tree.py
The sql/query.py add_q method did a lot of where/having tree hacking to
get complex queries to work correctly. The logic was refactored so that
it should be simpler to understand. The new logic should also produce
leaner WHERE conditions.
The changes cascade somewhat, as some other parts of Django (like
add_filter() and WhereNode) expect boolean trees in certain format or
they fail to work. So to fix the add_q() one must fix utils/tree.py,
some things in add_filter(), WhereNode and so on.
This commit also fixed add_filter to see negate clauses up the path.
A query like .exclude(Q(reversefk__in=a_list)) didn't work similarly to
.filter(~Q(reversefk__in=a_list)). The reason for this is that only
the immediate parent negate clauses were seen by add_filter, and thus a
tree like AND: (NOT AND: (AND: condition)) will not be handled
correctly, as there is one intermediary AND node in the tree. The
example tree is generated by .exclude(~Q(reversefk__in=a_list)).
Still, aggregation lost connectors in OR cases, and F() objects and
aggregates in same filter clause caused GROUP BY problems on some
databases.
Fixed #17600, fixed #13198, fixed #17025, fixed #17000, fixed #11293.
2012-05-25 05:27:24 +08:00
|
|
|
if node.name in query.aggregates:
|
2012-09-30 22:50:56 +08:00
|
|
|
self.cols.append((node, query.aggregate_select[node.name]))
|
2009-01-29 18:46:36 +08:00
|
|
|
else:
|
|
|
|
try:
|
2014-06-13 16:12:31 +08:00
|
|
|
_, sources, _, join_list, path = query.setup_joins(
|
|
|
|
field_list, query.get_meta(), query.get_initial_alias(),
|
|
|
|
can_reuse=self.reuse)
|
2013-11-05 00:04:57 +08:00
|
|
|
self._used_joins = join_list
|
2013-03-25 00:40:40 +08:00
|
|
|
targets, _, join_list = query.trim_joins(sources, join_list, path)
|
2012-12-21 03:25:48 +08:00
|
|
|
if self.reuse is not None:
|
2012-11-23 02:27:28 +08:00
|
|
|
self.reuse.update(join_list)
|
2013-03-25 00:40:40 +08:00
|
|
|
for t in targets:
|
|
|
|
self.cols.append((node, (join_list[-1], t.column)))
|
2009-01-29 18:46:36 +08:00
|
|
|
except FieldDoesNotExist:
|
|
|
|
raise FieldError("Cannot resolve keyword %r into field. "
|
|
|
|
"Choices are: %s" % (self.name,
|
|
|
|
[f.name for f in self.opts.fields]))
|
|
|
|
|
|
|
|
##################################################
|
2014-04-27 01:18:45 +08:00
|
|
|
# Visitor methods for final expression evaluation #
|
2009-01-29 18:46:36 +08:00
|
|
|
##################################################
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def evaluate_node(self, node, qn, connection):
|
2009-01-29 18:46:36 +08:00
|
|
|
expressions = []
|
|
|
|
expression_params = []
|
|
|
|
for child in node.children:
|
|
|
|
if hasattr(child, 'evaluate'):
|
2009-12-22 23:18:51 +08:00
|
|
|
sql, params = child.evaluate(self, qn, connection)
|
2009-01-29 18:46:36 +08:00
|
|
|
else:
|
2009-02-08 19:14:56 +08:00
|
|
|
sql, params = '%s', (child,)
|
2009-01-29 18:46:36 +08:00
|
|
|
|
2009-09-16 20:09:47 +08:00
|
|
|
if len(getattr(child, 'children', [])) > 1:
|
2009-01-29 18:46:36 +08:00
|
|
|
format = '(%s)'
|
|
|
|
else:
|
|
|
|
format = '%s'
|
|
|
|
|
|
|
|
if sql:
|
|
|
|
expressions.append(format % sql)
|
|
|
|
expression_params.extend(params)
|
|
|
|
|
2009-02-24 19:15:31 +08:00
|
|
|
return connection.ops.combine_expression(node.connector, expressions), expression_params
|
2009-01-29 18:46:36 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def evaluate_leaf(self, node, qn, connection):
|
2012-09-30 22:50:56 +08:00
|
|
|
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")
|
2009-01-29 18:46:36 +08:00
|
|
|
if hasattr(col, 'as_sql'):
|
2013-02-10 23:15:49 +08:00
|
|
|
return col.as_sql(qn, connection)
|
2009-01-29 18:46:36 +08:00
|
|
|
else:
|
2013-02-10 23:15:49 +08:00
|
|
|
return '%s.%s' % (qn(col[0]), qn(col[1])), []
|
2010-12-22 11:34:04 +08:00
|
|
|
|
|
|
|
def evaluate_date_modifier_node(self, node, qn, connection):
|
|
|
|
timedelta = node.children.pop()
|
|
|
|
sql, params = self.evaluate_node(node, qn, connection)
|
2013-12-22 06:03:17 +08:00
|
|
|
node.children.append(timedelta)
|
2010-12-22 11:34:04 +08:00
|
|
|
|
2013-09-22 20:01:57 +08:00
|
|
|
if (timedelta.days == timedelta.seconds == timedelta.microseconds == 0):
|
2010-12-22 11:34:04 +08:00
|
|
|
return sql, params
|
|
|
|
|
|
|
|
return connection.ops.date_interval_sql(sql, node.connector, timedelta), params
|