def _create_joins( self, source_polymorphic=False, source_selectable=None, dest_polymorphic=False, dest_selectable=None, of_type=None, ): key = util.WeakCompositeKey(source_polymorphic, source_selectable, dest_polymorphic, dest_selectable, of_type) try: return self.__join_cache[key] except KeyError: pass if source_selectable is None: if source_polymorphic and self.parent.with_polymorphic: source_selectable = self.parent._with_polymorphic_selectable aliased = False if dest_selectable is None: if dest_polymorphic and self.mapper.with_polymorphic: dest_selectable = self.mapper._with_polymorphic_selectable aliased = True else: dest_selectable = self.mapper.mapped_table if self._is_self_referential() and source_selectable is None: dest_selectable = dest_selectable.alias() aliased = True else: aliased = True aliased = aliased or bool(source_selectable) primaryjoin, secondaryjoin, secondary = self.primaryjoin, self.secondaryjoin, self.secondary # adjust the join condition for single table inheritance, # in the case that the join is to a subclass # this is analgous to the "_adjust_for_single_table_inheritance()" # method in Query. dest_mapper = of_type or self.mapper single_crit = dest_mapper._single_table_criterion if single_crit: if secondaryjoin: secondaryjoin = secondaryjoin & single_crit else: primaryjoin = primaryjoin & single_crit if aliased: if secondary: secondary = secondary.alias() primary_aliasizer = ClauseAdapter(secondary) if dest_selectable: secondary_aliasizer = ClauseAdapter( dest_selectable, equivalents=self.mapper._equivalent_columns ).chain(primary_aliasizer) else: secondary_aliasizer = primary_aliasizer if source_selectable: primary_aliasizer = ClauseAdapter(secondary).chain( ClauseAdapter(source_selectable, equivalents=self.parent._equivalent_columns) ) secondaryjoin = secondary_aliasizer.traverse(secondaryjoin) else: if dest_selectable: primary_aliasizer = ClauseAdapter( dest_selectable, exclude=self.local_side, equivalents=self.mapper._equivalent_columns ) if source_selectable: primary_aliasizer.chain( ClauseAdapter( source_selectable, exclude=self.remote_side, equivalents=self.parent._equivalent_columns ) ) elif source_selectable: primary_aliasizer = ClauseAdapter( source_selectable, exclude=self.remote_side, equivalents=self.parent._equivalent_columns ) secondary_aliasizer = None primaryjoin = primary_aliasizer.traverse(primaryjoin) target_adapter = secondary_aliasizer or primary_aliasizer target_adapter.include = target_adapter.exclude = None else: target_adapter = None self.__join_cache[key] = ret = ( primaryjoin, secondaryjoin, (source_selectable or self.parent.local_table), (dest_selectable or self.mapper.local_table), secondary, target_adapter, ) return ret
def _create_joins(self, source_polymorphic=False, source_selectable=None, dest_polymorphic=False, dest_selectable=None, of_type=None): if source_selectable is None: if source_polymorphic and self.parent.with_polymorphic: source_selectable = self.parent._with_polymorphic_selectable aliased = False if dest_selectable is None: if dest_polymorphic and self.mapper.with_polymorphic: dest_selectable = self.mapper._with_polymorphic_selectable aliased = True else: dest_selectable = self.mapper.mapped_table if self._is_self_referential() and source_selectable is None: dest_selectable = dest_selectable.alias() aliased = True else: aliased = True aliased = aliased or bool(source_selectable) primaryjoin, secondaryjoin, secondary = self.primaryjoin, self.secondaryjoin, self.secondary # adjust the join condition for single table inheritance, # in the case that the join is to a subclass # this is analgous to the "_adjust_for_single_table_inheritance()" # method in Query. dest_mapper = of_type or self.mapper single_crit = dest_mapper._single_table_criterion if single_crit: if secondaryjoin: secondaryjoin = secondaryjoin & single_crit else: primaryjoin = primaryjoin & single_crit if aliased: if secondary: secondary = secondary.alias() primary_aliasizer = ClauseAdapter(secondary) if dest_selectable: secondary_aliasizer = ClauseAdapter(dest_selectable, equivalents=self.mapper._equivalent_columns).chain(primary_aliasizer) else: secondary_aliasizer = primary_aliasizer if source_selectable: primary_aliasizer = ClauseAdapter(secondary).chain(ClauseAdapter(source_selectable, equivalents=self.parent._equivalent_columns)) secondaryjoin = secondary_aliasizer.traverse(secondaryjoin) else: if dest_selectable: primary_aliasizer = ClauseAdapter(dest_selectable, exclude=self.local_side, equivalents=self.mapper._equivalent_columns) if source_selectable: primary_aliasizer.chain(ClauseAdapter(source_selectable, exclude=self.remote_side, equivalents=self.parent._equivalent_columns)) elif source_selectable: primary_aliasizer = ClauseAdapter(source_selectable, exclude=self.remote_side, equivalents=self.parent._equivalent_columns) secondary_aliasizer = None primaryjoin = primary_aliasizer.traverse(primaryjoin) target_adapter = secondary_aliasizer or primary_aliasizer target_adapter.include = target_adapter.exclude = None else: target_adapter = None return (primaryjoin, secondaryjoin, (source_selectable or self.parent.local_table), (dest_selectable or self.mapper.local_table), secondary, target_adapter)
def _create_joins(self, source_polymorphic=False, source_selectable=None, dest_polymorphic=False, dest_selectable=None): key = util.WeakCompositeKey(source_polymorphic, source_selectable, dest_polymorphic, dest_selectable) try: return self.__join_cache[key] except KeyError: pass if source_selectable is None: if source_polymorphic and self.parent.with_polymorphic: source_selectable = self.parent._with_polymorphic_selectable aliased = False if dest_selectable is None: if dest_polymorphic and self.mapper.with_polymorphic: dest_selectable = self.mapper._with_polymorphic_selectable aliased = True else: dest_selectable = self.mapper.mapped_table if self._is_self_referential() and source_selectable is None: dest_selectable = dest_selectable.alias() aliased = True else: aliased = True aliased = aliased or bool(source_selectable) primaryjoin, secondaryjoin, secondary = self.primaryjoin, self.secondaryjoin, self.secondary # adjust the join condition for single table inheritance, # in the case that the join is to a subclass # this is analgous to the "_adjust_for_single_table_inheritance()" # method in Query. if self.mapper.single and self.mapper.inherits and self.mapper.polymorphic_on and self.mapper.polymorphic_identity is not None: crit = self.mapper.polymorphic_on.in_( m.polymorphic_identity for m in self.mapper.polymorphic_iterator()) if secondaryjoin: secondaryjoin = secondaryjoin & crit else: primaryjoin = primaryjoin & crit if aliased: if secondary: secondary = secondary.alias() primary_aliasizer = ClauseAdapter(secondary) if dest_selectable: secondary_aliasizer = ClauseAdapter( dest_selectable, equivalents=self.mapper._equivalent_columns).chain( primary_aliasizer) else: secondary_aliasizer = primary_aliasizer if source_selectable: primary_aliasizer = ClauseAdapter(secondary).chain( ClauseAdapter( source_selectable, equivalents=self.parent._equivalent_columns)) secondaryjoin = secondary_aliasizer.traverse(secondaryjoin) else: if dest_selectable: primary_aliasizer = ClauseAdapter( dest_selectable, exclude=self.local_side, equivalents=self.mapper._equivalent_columns) if source_selectable: primary_aliasizer.chain( ClauseAdapter( source_selectable, exclude=self.remote_side, equivalents=self.parent._equivalent_columns)) elif source_selectable: primary_aliasizer = ClauseAdapter( source_selectable, exclude=self.remote_side, equivalents=self.parent._equivalent_columns) secondary_aliasizer = None primaryjoin = primary_aliasizer.traverse(primaryjoin) target_adapter = secondary_aliasizer or primary_aliasizer target_adapter.include = target_adapter.exclude = None else: target_adapter = None self.__join_cache[key] = ret = (primaryjoin, secondaryjoin, (source_selectable or self.parent.local_table), (dest_selectable or self.mapper.local_table), secondary, target_adapter) return ret