mirror of
https://github.com/django/django.git
synced 2025-07-06 10:49:17 +00:00
queryset-refactor: Changed all tree and filter "connections" to "connectors" so
that database connections and constraint connectors look different in the code. George Vilches pointed out this was slightly confusing previously. git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@6866 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
4ea96c0468
commit
cc0078494e
@ -257,13 +257,13 @@ class Query(object):
|
|||||||
params.extend(self.extra_params)
|
params.extend(self.extra_params)
|
||||||
return ' '.join(result), tuple(params)
|
return ' '.join(result), tuple(params)
|
||||||
|
|
||||||
def combine(self, rhs, connection):
|
def combine(self, rhs, connector):
|
||||||
"""
|
"""
|
||||||
Merge the 'rhs' query into the current one (with any 'rhs' effects
|
Merge the 'rhs' query into the current one (with any 'rhs' effects
|
||||||
being applied *after* (that is, "to the right of") anything in the
|
being applied *after* (that is, "to the right of") anything in the
|
||||||
current query. 'rhs' is not modified during a call to this function.
|
current query. 'rhs' is not modified during a call to this function.
|
||||||
|
|
||||||
The 'connection' parameter describes how to connect filters from the
|
The 'connector' parameter describes how to connect filters from the
|
||||||
'rhs' query.
|
'rhs' query.
|
||||||
"""
|
"""
|
||||||
assert self.model == rhs.model, \
|
assert self.model == rhs.model, \
|
||||||
@ -276,7 +276,7 @@ class Query(object):
|
|||||||
# Work out how to relabel the rhs aliases, if necessary.
|
# Work out how to relabel the rhs aliases, if necessary.
|
||||||
change_map = {}
|
change_map = {}
|
||||||
used = {}
|
used = {}
|
||||||
conjunction = (connection == AND)
|
conjunction = (connector == AND)
|
||||||
first = True
|
first = True
|
||||||
for alias in rhs.tables:
|
for alias in rhs.tables:
|
||||||
if not rhs.alias_map[alias][ALIAS_REFCOUNT]:
|
if not rhs.alias_map[alias][ALIAS_REFCOUNT]:
|
||||||
@ -321,7 +321,7 @@ class Query(object):
|
|||||||
w.add([alias, pk.column, pk, 'isnull', False], AND)
|
w.add([alias, pk.column, pk, 'isnull', False], AND)
|
||||||
else:
|
else:
|
||||||
w = WhereNode()
|
w = WhereNode()
|
||||||
self.where.add(w, connection)
|
self.where.add(w, connector)
|
||||||
|
|
||||||
# Selection columns and extra extensions are those provided by 'rhs'.
|
# Selection columns and extra extensions are those provided by 'rhs'.
|
||||||
self.select = []
|
self.select = []
|
||||||
@ -465,7 +465,7 @@ class Query(object):
|
|||||||
result = []
|
result = []
|
||||||
for field in ordering:
|
for field in ordering:
|
||||||
if field == '?':
|
if field == '?':
|
||||||
result.append(self.connection.ops.random_function_sql())
|
result.append(self.connector.ops.random_function_sql())
|
||||||
continue
|
continue
|
||||||
if isinstance(field, int):
|
if isinstance(field, int):
|
||||||
if field < 0:
|
if field < 0:
|
||||||
@ -664,7 +664,7 @@ class Query(object):
|
|||||||
self.fill_related_selections(f.rel.to._meta, alias, cur_depth + 1,
|
self.fill_related_selections(f.rel.to._meta, alias, cur_depth + 1,
|
||||||
used)
|
used)
|
||||||
|
|
||||||
def add_filter(self, filter_expr, connection=AND, negate=False):
|
def add_filter(self, filter_expr, connector=AND, negate=False):
|
||||||
"""
|
"""
|
||||||
Add a single filter to the query.
|
Add a single filter to the query.
|
||||||
"""
|
"""
|
||||||
@ -694,13 +694,13 @@ class Query(object):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
field, target, unused, join_list, nullable = self.setup_joins(parts,
|
field, target, unused, join_list, nullable = self.setup_joins(parts,
|
||||||
opts, alias, (connection == AND))
|
opts, alias, (connector == AND))
|
||||||
except TypeError, e:
|
except TypeError, e:
|
||||||
if len(parts) != 1 or parts[0] not in self.extra_select:
|
if len(parts) != 1 or parts[0] not in self.extra_select:
|
||||||
raise e
|
raise e
|
||||||
# Filtering on some alias from extra(select=...)
|
# Filtering on some alias from extra(select=...)
|
||||||
self.where.add([None, parts[0], None, lookup_type, value],
|
self.where.add([None, parts[0], None, lookup_type, value],
|
||||||
connection)
|
connector)
|
||||||
return
|
return
|
||||||
col = target.column
|
col = target.column
|
||||||
alias = join_list[-1][-1]
|
alias = join_list[-1][-1]
|
||||||
@ -724,7 +724,7 @@ class Query(object):
|
|||||||
# efficient at the database level.
|
# efficient at the database level.
|
||||||
self.promote_alias(join_list[-1][0])
|
self.promote_alias(join_list[-1][0])
|
||||||
|
|
||||||
self.where.add([alias, col, field, lookup_type, value], connection)
|
self.where.add([alias, col, field, lookup_type, value], connector)
|
||||||
if negate:
|
if negate:
|
||||||
flag = False
|
flag = False
|
||||||
for pos, null in enumerate(nullable):
|
for pos, null in enumerate(nullable):
|
||||||
@ -750,11 +750,11 @@ class Query(object):
|
|||||||
|
|
||||||
for child in q_object.children:
|
for child in q_object.children:
|
||||||
if isinstance(child, Node):
|
if isinstance(child, Node):
|
||||||
self.where.start_subtree(q_object.connection)
|
self.where.start_subtree(q_object.connector)
|
||||||
self.add_q(child)
|
self.add_q(child)
|
||||||
self.where.end_subtree()
|
self.where.end_subtree()
|
||||||
else:
|
else:
|
||||||
self.add_filter(child, q_object.connection, q_object.negated)
|
self.add_filter(child, q_object.connector, q_object.negated)
|
||||||
|
|
||||||
def setup_joins(self, names, opts, alias, dupe_multis):
|
def setup_joins(self, names, opts, alias, dupe_multis):
|
||||||
"""
|
"""
|
||||||
|
@ -57,14 +57,14 @@ class WhereNode(tree.Node):
|
|||||||
sql, params = self.make_atom(child, qn)
|
sql, params = self.make_atom(child, qn)
|
||||||
format = '%s'
|
format = '%s'
|
||||||
except EmptyResultSet:
|
except EmptyResultSet:
|
||||||
if self.connection == AND and not node.negated:
|
if self.connector == AND and not node.negated:
|
||||||
# We can bail out early in this particular case (only).
|
# We can bail out early in this particular case (only).
|
||||||
raise
|
raise
|
||||||
sql = None
|
sql = None
|
||||||
if sql:
|
if sql:
|
||||||
result.append(format % sql)
|
result.append(format % sql)
|
||||||
result_params.extend(params)
|
result_params.extend(params)
|
||||||
conn = ' %s ' % node.connection
|
conn = ' %s ' % node.connector
|
||||||
return conn.join(result), result_params
|
return conn.join(result), result_params
|
||||||
|
|
||||||
def make_atom(self, child, qn):
|
def make_atom(self, child, qn):
|
||||||
|
@ -11,25 +11,25 @@ class Node(object):
|
|||||||
connection (the root) with the children being either leaf nodes or other
|
connection (the root) with the children being either leaf nodes or other
|
||||||
Node instances.
|
Node instances.
|
||||||
"""
|
"""
|
||||||
# Standard connection type. Clients usually won't use this at all and
|
# Standard connector type. Clients usually won't use this at all and
|
||||||
# subclasses will usually override the value.
|
# subclasses will usually override the value.
|
||||||
default = 'DEFAULT'
|
default = 'DEFAULT'
|
||||||
|
|
||||||
def __init__(self, children=None, connection=None):
|
def __init__(self, children=None, connector=None):
|
||||||
self.children = children and children[:] or []
|
self.children = children and children[:] or []
|
||||||
self.connection = connection or self.default
|
self.connector = connector or self.default
|
||||||
self.subtree_parents = []
|
self.subtree_parents = []
|
||||||
self.negated = False
|
self.negated = False
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return '(%s: %s)' % (self.connection, ', '.join([str(c) for c in
|
return '(%s: %s)' % (self.connector, ', '.join([str(c) for c in
|
||||||
self.children]))
|
self.children]))
|
||||||
|
|
||||||
def __deepcopy__(self, memodict):
|
def __deepcopy__(self, memodict):
|
||||||
"""
|
"""
|
||||||
Utility method used by copy.deepcopy().
|
Utility method used by copy.deepcopy().
|
||||||
"""
|
"""
|
||||||
obj = self.__class__(connection=self.connection)
|
obj = self.__class__(connector=self.connector)
|
||||||
obj.children = copy.deepcopy(self.children, memodict)
|
obj.children = copy.deepcopy(self.children, memodict)
|
||||||
obj.subtree_parents = copy.deepcopy(self.subtree_parents, memodict)
|
obj.subtree_parents = copy.deepcopy(self.subtree_parents, memodict)
|
||||||
obj.negated = self.negated
|
obj.negated = self.negated
|
||||||
@ -56,33 +56,33 @@ class Node(object):
|
|||||||
def add(self, node, conn_type):
|
def add(self, node, conn_type):
|
||||||
"""
|
"""
|
||||||
Adds a new node to the tree. If the conn_type is the same as the root's
|
Adds a new node to the tree. If the conn_type is the same as the root's
|
||||||
current connection type, the node is added to the first level.
|
current connector type, the node is added to the first level.
|
||||||
Otherwise, the whole tree is pushed down one level and a new root
|
Otherwise, the whole tree is pushed down one level and a new root
|
||||||
connection is created, connecting the existing tree and the new node.
|
connector is created, connecting the existing tree and the new node.
|
||||||
"""
|
"""
|
||||||
if len(self.children) < 2:
|
if len(self.children) < 2:
|
||||||
self.connection = conn_type
|
self.connector = conn_type
|
||||||
if self.connection == conn_type:
|
if self.connector == conn_type:
|
||||||
if isinstance(node, Node) and (node.connection == conn_type
|
if isinstance(node, Node) and (node.connector == conn_type
|
||||||
or len(node) == 1):
|
or len(node) == 1):
|
||||||
self.children.extend(node.children)
|
self.children.extend(node.children)
|
||||||
else:
|
else:
|
||||||
self.children.append(node)
|
self.children.append(node)
|
||||||
else:
|
else:
|
||||||
obj = Node(self.children, self.connection)
|
obj = Node(self.children, self.connector)
|
||||||
self.connection = conn_type
|
self.connector = conn_type
|
||||||
self.children = [obj, node]
|
self.children = [obj, node]
|
||||||
|
|
||||||
def negate(self):
|
def negate(self):
|
||||||
"""
|
"""
|
||||||
Negate the sense of the root connection.
|
Negate the sense of the root connector.
|
||||||
|
|
||||||
Interpreting the meaning of this negate is up to client code. This
|
Interpreting the meaning of this negate is up to client code. This
|
||||||
method is useful for implementing "not" arrangements.
|
method is useful for implementing "not" arrangements.
|
||||||
"""
|
"""
|
||||||
self.children = [NegatedNode(self.children, self.connection,
|
self.children = [NegatedNode(self.children, self.connector,
|
||||||
old_state=self.negated)]
|
old_state=self.negated)]
|
||||||
self.connection = self.default
|
self.connector = self.default
|
||||||
|
|
||||||
def start_subtree(self, conn_type):
|
def start_subtree(self, conn_type):
|
||||||
"""
|
"""
|
||||||
@ -91,13 +91,13 @@ class Node(object):
|
|||||||
connected correctly to any existing nodes in the tree.
|
connected correctly to any existing nodes in the tree.
|
||||||
"""
|
"""
|
||||||
if len(self.children) == 1:
|
if len(self.children) == 1:
|
||||||
self.connection = conn_type
|
self.connector = conn_type
|
||||||
elif self.connection != conn_type:
|
elif self.connector != conn_type:
|
||||||
self.children = [Node(self.children, self.connection)]
|
self.children = [Node(self.children, self.connector)]
|
||||||
self.connection = conn_type
|
self.connector = conn_type
|
||||||
|
|
||||||
self.subtree_parents.append(Node(self.children, self.connection))
|
self.subtree_parents.append(Node(self.children, self.connector))
|
||||||
self.connection = self.default
|
self.connector = self.default
|
||||||
self.children = []
|
self.children = []
|
||||||
|
|
||||||
def end_subtree(self):
|
def end_subtree(self):
|
||||||
@ -108,17 +108,17 @@ class Node(object):
|
|||||||
the current instances state to be the parent.
|
the current instances state to be the parent.
|
||||||
"""
|
"""
|
||||||
obj = self.subtree_parents.pop()
|
obj = self.subtree_parents.pop()
|
||||||
node = Node(self.children, self.connection)
|
node = Node(self.children, self.connector)
|
||||||
self.connection = obj.connection
|
self.connector = obj.connector
|
||||||
self.children = obj.children
|
self.children = obj.children
|
||||||
self.children.append(node)
|
self.children.append(node)
|
||||||
|
|
||||||
class NegatedNode(Node):
|
class NegatedNode(Node):
|
||||||
"""
|
"""
|
||||||
A class that indicates the connection type should be negated (whatever that
|
A class that indicates the connector type should be negated (whatever that
|
||||||
means -- it's up to the client) when used by the client code.
|
means -- it's up to the client) when used by the client code.
|
||||||
"""
|
"""
|
||||||
def __init__(self, children=None, connection=None, old_state=True):
|
def __init__(self, children=None, connector=None, old_state=True):
|
||||||
super(NegatedNode, self).__init__(children, connection)
|
super(NegatedNode, self).__init__(children, connector)
|
||||||
self.negated = not old_state
|
self.negated = not old_state
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user