diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2013-06-04 18:23:06 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2013-06-04 18:23:06 -0400 |
| commit | 11578ba709ff3e5f2a2a2a9f92bf6fdc2ee6d328 (patch) | |
| tree | 4202758c520dd62e85f18dc37da60d5774d375c8 /lib/sqlalchemy | |
| parent | 58e917fe2de2f4b643f3bf2468f51aef27e67747 (diff) | |
| download | sqlalchemy-11578ba709ff3e5f2a2a2a9f92bf6fdc2ee6d328.tar.gz | |
- improve overlapping selectables, apply to both query and relationship
- clean up inspect() calls within query._join()
- make sure join.alias(flat) propagates
- fix almost all assertion tests
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/orm/query.py | 32 | ||||
| -rw-r--r-- | lib/sqlalchemy/orm/relationships.py | 8 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/expression.py | 3 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/util.py | 23 |
4 files changed, 37 insertions, 29 deletions
diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index 8ab81cb13..39ed8d8bf 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -1797,14 +1797,6 @@ class Query(object): right_entity, onclause, outerjoin, create_aliases, prop) - def _tables_overlap(self, left, right): - """Return True if parent/child tables have some overlap.""" - - return bool( - set(sql_util.find_tables(left)).intersection( - sql_util.find_tables(right) - ) - ) def _join_left_to_right(self, left, right, onclause, outerjoin, create_aliases, prop): @@ -1825,14 +1817,19 @@ class Query(object): "are the same entity" % (left, right)) - # TODO: get the l_info, r_info passed into - # the methods so inspect() doesnt need to be called again l_info = inspect(left) r_info = inspect(right) - overlap = self._tables_overlap(l_info.selectable, r_info.selectable) + + overlap = not create_aliases and \ + sql_util.selectables_overlap(l_info.selectable, + r_info.selectable) + if overlap and l_info.selectable is r_info.selectable: + raise sa_exc.InvalidRequestError( + "Can't join table/selectable '%s' to itself" % + l_info.selectable) right, onclause = self._prepare_right_side( - right, onclause, + r_info, right, onclause, create_aliases, prop, overlap) @@ -1846,10 +1843,11 @@ class Query(object): else: self._joinpoint = {'_joinpoint_entity': right} - self._join_to_left(left, right, onclause, outerjoin) + self._join_to_left(l_info, left, right, onclause, outerjoin) - def _prepare_right_side(self, right, onclause, create_aliases, prop, overlap): - info = inspect(right) + def _prepare_right_side(self, r_info, right, onclause, create_aliases, + prop, overlap): + info = r_info right_mapper, right_selectable, right_is_aliased = \ getattr(info, 'mapper', None), \ @@ -1931,8 +1929,8 @@ class Query(object): return right, onclause - def _join_to_left(self, left, right, onclause, outerjoin): - info = inspect(left) + def _join_to_left(self, l_info, left, right, onclause, outerjoin): + info = l_info left_mapper = getattr(info, 'mapper', None) left_selectable = info.selectable diff --git a/lib/sqlalchemy/orm/relationships.py b/lib/sqlalchemy/orm/relationships.py index 95fa28613..33377d3ec 100644 --- a/lib/sqlalchemy/orm/relationships.py +++ b/lib/sqlalchemy/orm/relationships.py @@ -17,7 +17,7 @@ from .. import sql, util, exc as sa_exc, schema from ..sql.util import ( ClauseAdapter, join_condition, _shallow_annotate, visit_binary_product, - _deep_deannotate, find_tables + _deep_deannotate, find_tables, selectables_overlap ) from ..sql import operators, expression, visitors from .interfaces import MANYTOMANY, MANYTOONE, ONETOMANY @@ -404,11 +404,7 @@ class JoinCondition(object): def _tables_overlap(self): """Return True if parent/child tables have some overlap.""" - return bool( - set(find_tables(self.parent_selectable)).intersection( - find_tables(self.child_selectable) - ) - ) + return selectables_overlap(self.parent_selectable, self.child_selectable) def _annotate_remote(self): """Annotate the primaryjoin and secondaryjoin diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py index 633a3ddba..e7ef3cb72 100644 --- a/lib/sqlalchemy/sql/expression.py +++ b/lib/sqlalchemy/sql/expression.py @@ -4001,7 +4001,8 @@ class Join(FromClause): """ if flat: assert name is None, "Can't send name argument with flat" - left_a, right_a = self.left.alias(), self.right.alias() + left_a, right_a = self.left.alias(flat=True), \ + self.right.alias(flat=True) adapter = sqlutil.ClauseAdapter(left_a).\ chain(sqlutil.ClauseAdapter(right_a)) diff --git a/lib/sqlalchemy/sql/util.py b/lib/sqlalchemy/sql/util.py index c80693706..6f4d27e1b 100644 --- a/lib/sqlalchemy/sql/util.py +++ b/lib/sqlalchemy/sql/util.py @@ -200,15 +200,28 @@ def clause_is_present(clause, search): """ - stack = [search] - while stack: - elem = stack.pop() + for elem in surface_selectables(search): if clause == elem: # use == here so that Annotated's compare return True - elif isinstance(elem, expression.Join): + else: + return False + +def surface_selectables(clause): + stack = [clause] + while stack: + elem = stack.pop() + yield elem + if isinstance(elem, expression.Join): stack.extend((elem.left, elem.right)) - return False +def selectables_overlap(left, right): + """Return True if left/right have some overlapping selectable""" + + return bool( + set(surface_selectables(left)).intersection( + surface_selectables(right) + ) + ) def bind_values(clause): """Return an ordered list of "bound" values in the given clause. |
