2010-12-22 11:34:04 +08:00
|
|
|
import datetime
|
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
|
|
|
|
|
|
|
from django.db.models.aggregates import refs_aggregate
|
|
|
|
from django.db.models.constants import LOOKUP_SEP
|
2009-01-29 18:46:36 +08:00
|
|
|
from django.utils import tree
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-01-29 18:46:36 +08:00
|
|
|
class ExpressionNode(tree.Node):
|
|
|
|
"""
|
|
|
|
Base class for all query expressions.
|
|
|
|
"""
|
|
|
|
# Arithmetic connectors
|
|
|
|
ADD = '+'
|
|
|
|
SUB = '-'
|
|
|
|
MUL = '*'
|
|
|
|
DIV = '/'
|
2013-02-22 06:02:18 +08:00
|
|
|
POW = '^'
|
2009-01-29 18:46:36 +08:00
|
|
|
MOD = '%%' # This is a quoted % operator - it is quoted
|
|
|
|
# because it can be used in strings that also
|
|
|
|
# have parameter substitution.
|
|
|
|
|
2012-10-03 23:53:40 +08:00
|
|
|
# Bitwise operators - note that these are generated by .bitand()
|
|
|
|
# and .bitor(), the '&' and '|' are reserved for boolean operator
|
|
|
|
# usage.
|
|
|
|
BITAND = '&'
|
|
|
|
BITOR = '|'
|
2009-01-29 18:46:36 +08:00
|
|
|
|
|
|
|
def __init__(self, children=None, connector=None, negated=False):
|
|
|
|
if children is not None and len(children) > 1 and connector is None:
|
|
|
|
raise TypeError('You have to specify a connector.')
|
|
|
|
super(ExpressionNode, self).__init__(children, connector, negated)
|
|
|
|
|
|
|
|
def _combine(self, other, connector, reversed, node=None):
|
2010-12-22 11:34:04 +08:00
|
|
|
if isinstance(other, datetime.timedelta):
|
|
|
|
return DateModifierNode([self, other], connector)
|
|
|
|
|
2009-01-29 18:46:36 +08:00
|
|
|
if reversed:
|
|
|
|
obj = ExpressionNode([other], connector)
|
|
|
|
obj.add(node or self, connector)
|
|
|
|
else:
|
|
|
|
obj = node or ExpressionNode([self], connector)
|
|
|
|
obj.add(other, connector)
|
|
|
|
return obj
|
|
|
|
|
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
|
|
|
def contains_aggregate(self, existing_aggregates):
|
|
|
|
if self.children:
|
|
|
|
return any(child.contains_aggregate(existing_aggregates)
|
|
|
|
for child in self.children
|
|
|
|
if hasattr(child, 'contains_aggregate'))
|
|
|
|
else:
|
|
|
|
return refs_aggregate(self.name.split(LOOKUP_SEP),
|
|
|
|
existing_aggregates)
|
|
|
|
|
|
|
|
def prepare_database_save(self, unused):
|
|
|
|
return self
|
|
|
|
|
2009-01-29 18:46:36 +08:00
|
|
|
###################
|
|
|
|
# VISITOR METHODS #
|
|
|
|
###################
|
|
|
|
|
|
|
|
def prepare(self, evaluator, query, allow_joins):
|
|
|
|
return evaluator.prepare_node(self, query, allow_joins)
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def evaluate(self, evaluator, qn, connection):
|
|
|
|
return evaluator.evaluate_node(self, qn, connection)
|
2009-01-29 18:46:36 +08:00
|
|
|
|
|
|
|
#############
|
|
|
|
# OPERATORS #
|
|
|
|
#############
|
|
|
|
|
|
|
|
def __add__(self, other):
|
|
|
|
return self._combine(other, self.ADD, False)
|
|
|
|
|
|
|
|
def __sub__(self, other):
|
|
|
|
return self._combine(other, self.SUB, False)
|
|
|
|
|
|
|
|
def __mul__(self, other):
|
|
|
|
return self._combine(other, self.MUL, False)
|
|
|
|
|
2012-08-14 20:43:33 +08:00
|
|
|
def __truediv__(self, other):
|
2009-01-29 18:46:36 +08:00
|
|
|
return self._combine(other, self.DIV, False)
|
2012-11-04 04:43:11 +08:00
|
|
|
|
|
|
|
def __div__(self, other): # Python 2 compatibility
|
|
|
|
return type(self).__truediv__(self, other)
|
2009-01-29 18:46:36 +08:00
|
|
|
|
|
|
|
def __mod__(self, other):
|
|
|
|
return self._combine(other, self.MOD, False)
|
|
|
|
|
2013-02-22 06:02:18 +08:00
|
|
|
def __pow__(self, other):
|
|
|
|
return self._combine(other, self.POW, False)
|
|
|
|
|
2009-01-29 18:46:36 +08:00
|
|
|
def __and__(self, other):
|
2012-10-03 23:53:40 +08:00
|
|
|
raise NotImplementedError(
|
|
|
|
"Use .bitand() and .bitor() for bitwise logical operations."
|
|
|
|
)
|
|
|
|
|
|
|
|
def bitand(self, other):
|
|
|
|
return self._combine(other, self.BITAND, False)
|
2009-01-29 18:46:36 +08:00
|
|
|
|
|
|
|
def __or__(self, other):
|
2012-10-03 23:53:40 +08:00
|
|
|
raise NotImplementedError(
|
|
|
|
"Use .bitand() and .bitor() for bitwise logical operations."
|
|
|
|
)
|
|
|
|
|
|
|
|
def bitor(self, other):
|
|
|
|
return self._combine(other, self.BITOR, False)
|
2009-01-29 18:46:36 +08:00
|
|
|
|
|
|
|
def __radd__(self, other):
|
|
|
|
return self._combine(other, self.ADD, True)
|
|
|
|
|
|
|
|
def __rsub__(self, other):
|
|
|
|
return self._combine(other, self.SUB, True)
|
|
|
|
|
|
|
|
def __rmul__(self, other):
|
|
|
|
return self._combine(other, self.MUL, True)
|
|
|
|
|
2012-08-15 19:27:40 +08:00
|
|
|
def __rtruediv__(self, other):
|
2009-01-29 18:46:36 +08:00
|
|
|
return self._combine(other, self.DIV, True)
|
2012-11-04 04:43:11 +08:00
|
|
|
|
|
|
|
def __rdiv__(self, other): # Python 2 compatibility
|
|
|
|
return type(self).__rtruediv__(self, other)
|
2009-01-29 18:46:36 +08:00
|
|
|
|
|
|
|
def __rmod__(self, other):
|
|
|
|
return self._combine(other, self.MOD, True)
|
|
|
|
|
2013-02-22 06:02:18 +08:00
|
|
|
def __rpow__(self, other):
|
|
|
|
return self._combine(other, self.POW, True)
|
|
|
|
|
2009-01-29 18:46:36 +08:00
|
|
|
def __rand__(self, other):
|
2012-10-03 23:53:40 +08:00
|
|
|
raise NotImplementedError(
|
|
|
|
"Use .bitand() and .bitor() for bitwise logical operations."
|
|
|
|
)
|
2009-01-29 18:46:36 +08:00
|
|
|
|
|
|
|
def __ror__(self, other):
|
2012-10-03 23:53:40 +08:00
|
|
|
raise NotImplementedError(
|
|
|
|
"Use .bitand() and .bitor() for bitwise logical operations."
|
|
|
|
)
|
2009-01-29 18:46:36 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-01-29 18:46:36 +08:00
|
|
|
class F(ExpressionNode):
|
|
|
|
"""
|
|
|
|
An expression representing the value of the given field.
|
|
|
|
"""
|
|
|
|
def __init__(self, name):
|
|
|
|
super(F, self).__init__(None, None, False)
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def prepare(self, evaluator, query, allow_joins):
|
|
|
|
return evaluator.prepare_leaf(self, query, allow_joins)
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def evaluate(self, evaluator, qn, connection):
|
|
|
|
return evaluator.evaluate_leaf(self, qn, connection)
|
2010-12-22 11:34:04 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2010-12-22 11:34:04 +08:00
|
|
|
class DateModifierNode(ExpressionNode):
|
|
|
|
"""
|
|
|
|
Node that implements the following syntax:
|
|
|
|
filter(end_date__gt=F('start_date') + datetime.timedelta(days=3, seconds=200))
|
|
|
|
|
|
|
|
which translates into:
|
|
|
|
POSTGRES:
|
|
|
|
WHERE end_date > (start_date + INTERVAL '3 days 200 seconds')
|
|
|
|
|
|
|
|
MYSQL:
|
|
|
|
WHERE end_date > (start_date + INTERVAL '3 0:0:200:0' DAY_MICROSECOND)
|
|
|
|
|
|
|
|
ORACLE:
|
|
|
|
WHERE end_date > (start_date + INTERVAL '3 00:03:20.000000' DAY(1) TO SECOND(6))
|
|
|
|
|
|
|
|
SQLITE:
|
|
|
|
WHERE end_date > django_format_dtdelta(start_date, "+" "3", "200", "0")
|
|
|
|
(A custom function is used in order to preserve six digits of fractional
|
|
|
|
second information on sqlite, and to format both date and datetime values.)
|
|
|
|
|
2012-11-04 04:43:11 +08:00
|
|
|
Note that microsecond comparisons are not well supported with MySQL, since
|
2010-12-22 11:34:04 +08:00
|
|
|
MySQL does not store microsecond information.
|
|
|
|
|
2012-11-04 04:43:11 +08:00
|
|
|
Only adding and subtracting timedeltas is supported, attempts to use other
|
2010-12-22 11:34:04 +08:00
|
|
|
operations raise a TypeError.
|
|
|
|
"""
|
|
|
|
def __init__(self, children, connector, negated=False):
|
|
|
|
if len(children) != 2:
|
|
|
|
raise TypeError('Must specify a node and a timedelta.')
|
|
|
|
if not isinstance(children[1], datetime.timedelta):
|
|
|
|
raise TypeError('Second child must be a timedelta.')
|
|
|
|
if connector not in (self.ADD, self.SUB):
|
|
|
|
raise TypeError('Connector must be + or -, not %s' % connector)
|
|
|
|
super(DateModifierNode, self).__init__(children, connector, negated)
|
|
|
|
|
|
|
|
def evaluate(self, evaluator, qn, connection):
|
|
|
|
return evaluator.evaluate_date_modifier_node(self, qn, connection)
|