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
|
|
|
|
|
|
|
|
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-09-30 22:50:56 +08:00
|
|
|
self.cols = []
|
2009-01-29 18:46:36 +08:00
|
|
|
|
|
|
|
self.contains_aggregate = False
|
2012-11-23 02:27:28 +08:00
|
|
|
self.reuse = reuse
|
2009-01-29 18:46:36 +08:00
|
|
|
self.expression.prepare(self, query, allow_joins)
|
|
|
|
|
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
|
|
|
|
|
|
|
def relabel_aliases(self, change_map):
|
2012-09-30 22:50:56 +08:00
|
|
|
new_cols = []
|
|
|
|
for node, col in self.cols:
|
2010-11-23 02:00:01 +08:00
|
|
|
if hasattr(col, "relabel_aliases"):
|
|
|
|
col.relabel_aliases(change_map)
|
2012-09-30 22:50:56 +08:00
|
|
|
new_cols.append((node, col))
|
2010-11-23 02:00:01 +08:00
|
|
|
else:
|
2012-09-30 22:50:56 +08:00
|
|
|
new_cols.append((node,
|
|
|
|
(change_map.get(col[0], col[0]), col[1])))
|
|
|
|
self.cols = new_cols
|
2009-01-29 18:46:36 +08:00
|
|
|
|
|
|
|
#####################################################
|
|
|
|
# Vistor methods for initial expression preparation #
|
|
|
|
#####################################################
|
|
|
|
|
|
|
|
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)
|
|
|
|
if (len(field_list) == 1 and
|
|
|
|
node.name in query.aggregate_select.keys()):
|
|
|
|
self.contains_aggregate = True
|
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:
|
2012-08-25 21:33:07 +08:00
|
|
|
field, source, opts, join_list, path = query.setup_joins(
|
2009-01-29 18:46:36 +08:00
|
|
|
field_list, query.get_meta(),
|
2012-11-23 02:27:28 +08:00
|
|
|
query.get_initial_alias(), self.reuse)
|
2012-08-25 21:33:07 +08:00
|
|
|
col, _, join_list = query.trim_joins(source, 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)
|
2012-09-30 22:50:56 +08:00
|
|
|
self.cols.append((node, (join_list[-1], col)))
|
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]))
|
|
|
|
|
|
|
|
##################################################
|
|
|
|
# Vistor methods for final expression evaluation #
|
|
|
|
##################################################
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
if timedelta.days == 0 and timedelta.seconds == 0 and \
|
|
|
|
timedelta.microseconds == 0:
|
|
|
|
return sql, params
|
|
|
|
|
|
|
|
return connection.ops.date_interval_sql(sql, node.connector, timedelta), params
|