def _create_joins(self, source_polymorphic=False, source_selectable=None, dest_polymorphic=False, dest_selectable=None): if source_selectable is None: if source_polymorphic and self.parent.with_polymorphic: source_selectable = self.parent._with_polymorphic_selectable() else: source_selectable = None if dest_selectable is None: if dest_polymorphic and self.mapper.with_polymorphic: dest_selectable = self.mapper._with_polymorphic_selectable() else: dest_selectable = self.mapper.mapped_table if self._is_self_referential(): if dest_selectable: dest_selectable = dest_selectable.alias() else: dest_selectable = self.mapper.mapped_table.alias() primaryjoin = self.primaryjoin if source_selectable: if self.direction in (ONETOMANY, MANYTOMANY): primaryjoin = ClauseAdapter( source_selectable, exclude=self.foreign_keys, equivalents=self.parent._equivalent_columns).traverse( primaryjoin) else: primaryjoin = ClauseAdapter( source_selectable, include=self.foreign_keys, equivalents=self.parent._equivalent_columns).traverse( primaryjoin) secondaryjoin = self.secondaryjoin target_adapter = None if dest_selectable: if self.direction == ONETOMANY: target_adapter = ClauseAdapter( dest_selectable, include=self.foreign_keys, equivalents=self.mapper._equivalent_columns) elif self.direction == MANYTOMANY: target_adapter = ClauseAdapter( dest_selectable, equivalents=self.mapper._equivalent_columns) else: target_adapter = ClauseAdapter( dest_selectable, exclude=self.foreign_keys, equivalents=self.mapper._equivalent_columns) if secondaryjoin: secondaryjoin = target_adapter.traverse(secondaryjoin) else: primaryjoin = target_adapter.traverse(primaryjoin) target_adapter.include = target_adapter.exclude = None return primaryjoin, secondaryjoin, source_selectable or self.parent.local_table, dest_selectable or self.mapper.local_table, target_adapter
def __join_against(self, frommapper, fromselectable, toselectable, primary, secondary): if fromselectable is None: fromselectable = frommapper.local_table parent_equivalents = frommapper._equivalent_columns if primary: primaryjoin = self.primaryjoin if fromselectable is not frommapper.local_table: if self.direction is ONETOMANY: primaryjoin = ClauseAdapter(fromselectable, exclude=self.foreign_keys, equivalents=parent_equivalents).traverse(primaryjoin) elif self.direction is MANYTOONE: primaryjoin = ClauseAdapter(fromselectable, include=self.foreign_keys, equivalents=parent_equivalents).traverse(primaryjoin) elif self.secondaryjoin: primaryjoin = ClauseAdapter(fromselectable, exclude=self.foreign_keys, equivalents=parent_equivalents).traverse(primaryjoin) if secondary: secondaryjoin = self.secondaryjoin return primaryjoin & secondaryjoin else: return primaryjoin elif secondary: return self.secondaryjoin else: raise AssertionError("illegal condition")
def _create_joins( self, source_polymorphic=False, source_selectable=None, dest_polymorphic=False, dest_selectable=None ): if source_selectable is None: if source_polymorphic and self.parent.with_polymorphic: source_selectable = self.parent._with_polymorphic_selectable() else: source_selectable = None if dest_selectable is None: if dest_polymorphic and self.mapper.with_polymorphic: dest_selectable = self.mapper._with_polymorphic_selectable() else: dest_selectable = self.mapper.mapped_table if self._is_self_referential(): if dest_selectable: dest_selectable = dest_selectable.alias() else: dest_selectable = self.mapper.mapped_table.alias() primaryjoin = self.primaryjoin if source_selectable: if self.direction in (ONETOMANY, MANYTOMANY): primaryjoin = ClauseAdapter( source_selectable, exclude=self.foreign_keys, equivalents=self.parent._equivalent_columns ).traverse(primaryjoin) else: primaryjoin = ClauseAdapter( source_selectable, include=self.foreign_keys, equivalents=self.parent._equivalent_columns ).traverse(primaryjoin) secondaryjoin = self.secondaryjoin target_adapter = None if dest_selectable: if self.direction == ONETOMANY: target_adapter = ClauseAdapter( dest_selectable, include=self.foreign_keys, equivalents=self.mapper._equivalent_columns ) elif self.direction == MANYTOMANY: target_adapter = ClauseAdapter(dest_selectable, equivalents=self.mapper._equivalent_columns) else: target_adapter = ClauseAdapter( dest_selectable, exclude=self.foreign_keys, equivalents=self.mapper._equivalent_columns ) if secondaryjoin: secondaryjoin = target_adapter.traverse(secondaryjoin) else: primaryjoin = target_adapter.traverse(primaryjoin) target_adapter.include = target_adapter.exclude = None return ( primaryjoin, secondaryjoin, source_selectable or self.parent.local_table, dest_selectable or self.mapper.local_table, target_adapter, )
def adapt(adapt_with, expression): if isinstance(expression.expression, sa.Column): cols = get_attrs(adapt_with) return getattr(cols, expression.name) if not hasattr(adapt_with, 'is_derived_from'): adapt_with = sa.inspect(adapt_with).selectable return ClauseAdapter(adapt_with).traverse(expression.expression)
def adapt_attribute(self, attr_name): cols = get_attrs(self.from_obj) hybrids = get_hybrid_properties(self.model).keys() if attr_name in hybrids: return ClauseAdapter(self.from_obj).traverse( getattr(self.model, attr_name)) else: return getattr(cols, attr_name)
def adapt_attribute(self, attr_name): cols = get_attrs(self.from_obj) hybrids = get_hybrid_properties(self.model).keys() if ( attr_name in hybrids or attr_name in self.column_property_expressions ): column = ClauseAdapter(self.from_obj).traverse( getattr(self.model, attr_name) ) else: column = getattr(cols, attr_name) return self.format_column(column)
def adapted_descriptors(self): return ( get_all_descriptors(self.from_obj).items() + [ ( key, ClauseAdapter(self.from_obj).traverse( getattr(self.model, key) ) ) for key in get_hybrid_properties(self.model).keys() ] )
def _join_and_criterion(self, criterion=None, **kwargs): adapt_against = None if getattr(self, '_of_type', None): target_mapper = self._of_type to_selectable = target_mapper.mapped_table adapt_against = to_selectable else: target_mapper = self.prop.mapper to_selectable = None adapt_against = None if self.prop._is_self_referential(): pj = self.prop.primary_join_against(self.prop.parent, None) sj = self.prop.secondary_join_against(self.prop.parent, toselectable=to_selectable) pac = PropertyAliasedClauses(self.prop, pj, sj) j = pac.primaryjoin if pac.secondaryjoin: j = j & pac.secondaryjoin else: j = self.prop.full_join_against(self.prop.parent, None, toselectable=to_selectable) for k in kwargs: crit = (getattr(self.prop.mapper.class_, k) == kwargs[k]) if criterion is None: criterion = crit else: criterion = criterion & crit if criterion: if adapt_against: criterion = ClauseAdapter(adapt_against).traverse(criterion) if self.prop._is_self_referential(): criterion = pac.adapt_clause(criterion) return j, criterion, to_selectable
def adapt_expr(expr, *selectables): for selectable in selectables: expr = ClauseAdapter(selectable).traverse(expr) return expr
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, 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): 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