<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from . . import sql , util , exc as sa_exc , schema , log <EOL> from . util import CascadeOptions , _orm_annotate , _orm_deannotate <EOL> from . import dependency <EOL> from . import attributes <EOL> from . . sql . util import ( <EOL> ClauseAdapter , <EOL> join_condition , _shallow_annotate , visit_binary_product , <EOL> _deep_deannotate , selectables_overlap <EOL> ) <EOL> from . . sql import operators , expression , visitors <EOL> from . interfaces import ( MANYTOMANY , MANYTOONE , ONETOMANY , <EOL> StrategizedProperty , PropComparator ) <EOL> from . . inspection import inspect <EOL> from . import mapper as mapperlib <EOL> import collections <EOL> def remote ( expr ) : <EOL> """<STR_LIT>""" <EOL> return _annotate_columns ( expression . _clause_element_as_expr ( expr ) , <EOL> { "<STR_LIT>" : True } ) <EOL> def foreign ( expr ) : <EOL> """<STR_LIT>""" <EOL> return _annotate_columns ( expression . _clause_element_as_expr ( expr ) , <EOL> { "<STR_LIT>" : True } ) <EOL> @ log . class_logger <EOL> @ util . langhelpers . dependency_for ( "<STR_LIT>" ) <EOL> class RelationshipProperty ( StrategizedProperty ) : <EOL> """<STR_LIT>""" <EOL> strategy_wildcard_key = '<STR_LIT>' <EOL> _dependency_processor = None <EOL> def __init__ ( self , argument , <EOL> secondary = None , primaryjoin = None , <EOL> secondaryjoin = None , <EOL> foreign_keys = None , <EOL> uselist = None , <EOL> order_by = False , <EOL> backref = None , <EOL> back_populates = None , <EOL> post_update = False , <EOL> cascade = False , extension = None , <EOL> viewonly = False , lazy = True , <EOL> collection_class = None , passive_deletes = False , <EOL> passive_updates = True , remote_side = None , <EOL> enable_typechecks = True , join_depth = None , <EOL> comparator_factory = None , <EOL> single_parent = False , innerjoin = False , <EOL> distinct_target_key = None , <EOL> doc = None , <EOL> active_history = False , <EOL> cascade_backrefs = True , <EOL> load_on_pending = False , <EOL> strategy_class = None , _local_remote_pairs = None , <EOL> query_class = None , <EOL> info = None ) : <EOL> """<STR_LIT>""" <EOL> self . uselist = uselist <EOL> self . argument = argument <EOL> self . secondary = secondary <EOL> self . primaryjoin = primaryjoin <EOL> self . secondaryjoin = secondaryjoin <EOL> self . post_update = post_update <EOL> self . direction = None <EOL> self . viewonly = viewonly <EOL> self . lazy = lazy <EOL> self . single_parent = single_parent <EOL> self . _user_defined_foreign_keys = foreign_keys <EOL> self . collection_class = collection_class <EOL> self . passive_deletes = passive_deletes <EOL> self . cascade_backrefs = cascade_backrefs <EOL> self . passive_updates = passive_updates <EOL> self . remote_side = remote_side <EOL> self . enable_typechecks = enable_typechecks <EOL> self . query_class = query_class <EOL> self . innerjoin = innerjoin <EOL> self . distinct_target_key = distinct_target_key <EOL> self . doc = doc <EOL> self . active_history = active_history <EOL> self . join_depth = join_depth <EOL> self . local_remote_pairs = _local_remote_pairs <EOL> self . extension = extension <EOL> self . load_on_pending = load_on_pending <EOL> self . comparator_factory = comparator_factory or RelationshipProperty . Comparator <EOL> self . comparator = self . comparator_factory ( self , None ) <EOL> util . set_creation_order ( self ) <EOL> if info is not None : <EOL> self . info = info <EOL> if strategy_class : <EOL> self . strategy_class = strategy_class <EOL> else : <EOL> self . strategy_class = self . _strategy_lookup ( ( "<STR_LIT>" , self . lazy ) ) <EOL> self . _reverse_property = set ( ) <EOL> self . cascade = cascade if cascade is not False else "<STR_LIT>" <EOL> self . order_by = order_by <EOL> self . back_populates = back_populates <EOL> if self . back_populates : <EOL> if backref : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . backref = None <EOL> else : <EOL> self . backref = backref <EOL> def instrument_class ( self , mapper ) : <EOL> attributes . register_descriptor ( <EOL> mapper . class_ , <EOL> self . key , <EOL> comparator = self . comparator_factory ( self , mapper ) , <EOL> parententity = mapper , <EOL> doc = self . doc , <EOL> ) <EOL> class Comparator ( PropComparator ) : <EOL> """<STR_LIT>""" <EOL> _of_type = None <EOL> def __init__ ( <EOL> self , prop , parentmapper , adapt_to_entity = None , of_type = None ) : <EOL> """<STR_LIT>""" <EOL> self . prop = prop <EOL> self . _parentmapper = parentmapper <EOL> self . _adapt_to_entity = adapt_to_entity <EOL> if of_type : <EOL> self . _of_type = of_type <EOL> def adapt_to_entity ( self , adapt_to_entity ) : <EOL> return self . __class__ ( self . property , self . _parentmapper , <EOL> adapt_to_entity = adapt_to_entity , <EOL> of_type = self . _of_type ) <EOL> @ util . memoized_property <EOL> def mapper ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . property . mapper <EOL> @ util . memoized_property <EOL> def _parententity ( self ) : <EOL> return self . property . parent <EOL> def _source_selectable ( self ) : <EOL> if self . _adapt_to_entity : <EOL> return self . _adapt_to_entity . selectable <EOL> else : <EOL> return self . property . parent . _with_polymorphic_selectable <EOL> def __clause_element__ ( self ) : <EOL> adapt_from = self . _source_selectable ( ) <EOL> if self . _of_type : <EOL> of_type = inspect ( self . _of_type ) . mapper <EOL> else : <EOL> of_type = None <EOL> pj , sj , source , dest , secondary , target_adapter = self . property . _create_joins ( <EOL> source_selectable = adapt_from , <EOL> source_polymorphic = True , <EOL> of_type = of_type ) <EOL> if sj is not None : <EOL> return pj & sj <EOL> else : <EOL> return pj <EOL> def of_type ( self , cls ) : <EOL> """<STR_LIT>""" <EOL> return RelationshipProperty . Comparator ( <EOL> self . property , <EOL> self . _parentmapper , <EOL> adapt_to_entity = self . _adapt_to_entity , <EOL> of_type = cls ) <EOL> def in_ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> __hash__ = None <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( other , ( util . NoneType , expression . Null ) ) : <EOL> if self . property . direction in [ ONETOMANY , MANYTOMANY ] : <EOL> return ~ self . _criterion_exists ( ) <EOL> else : <EOL> return _orm_annotate ( self . property . _optimized_compare ( <EOL> None , adapt_source = self . adapter ) ) <EOL> elif self . property . uselist : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> return _orm_annotate ( <EOL> self . property . _optimized_compare ( <EOL> other , adapt_source = self . adapter ) ) <EOL> def _criterion_exists ( self , criterion = None , ** kwargs ) : <EOL> if getattr ( self , '<STR_LIT>' , None ) : <EOL> info = inspect ( self . _of_type ) <EOL> target_mapper , to_selectable , is_aliased_class = info . mapper , info . selectable , info . is_aliased_class <EOL> if self . property . _is_self_referential and not is_aliased_class : <EOL> to_selectable = to_selectable . alias ( ) <EOL> single_crit = target_mapper . _single_table_criterion <EOL> if single_crit is not None : <EOL> if criterion is not None : <EOL> criterion = single_crit & criterion <EOL> else : <EOL> criterion = single_crit <EOL> else : <EOL> is_aliased_class = False <EOL> to_selectable = None <EOL> if self . adapter : <EOL> source_selectable = self . _source_selectable ( ) <EOL> else : <EOL> source_selectable = None <EOL> pj , sj , source , dest , secondary , target_adapter = self . property . _create_joins ( <EOL> dest_polymorphic = True , <EOL> dest_selectable = to_selectable , <EOL> source_selectable = source_selectable ) <EOL> for k in kwargs : <EOL> crit = getattr ( self . property . mapper . class_ , k ) == kwargs [ k ] <EOL> if criterion is None : <EOL> criterion = crit <EOL> else : <EOL> criterion = criterion & crit <EOL> if sj is not None : <EOL> j = _orm_annotate ( pj ) & sj <EOL> else : <EOL> j = _orm_annotate ( pj , exclude = self . property . remote_side ) <EOL> if criterion is not None and target_adapter and not is_aliased_class : <EOL> criterion = target_adapter . traverse ( criterion ) <EOL> if criterion is not None : <EOL> criterion = criterion . _annotate ( <EOL> { '<STR_LIT>' : True } ) <EOL> crit = j & sql . True_ . _ifnone ( criterion ) <EOL> ex = sql . exists ( [ <NUM_LIT:1> ] , crit , from_obj = dest ) . correlate_except ( dest ) <EOL> if secondary is not None : <EOL> ex = ex . correlate_except ( secondary ) <EOL> return ex <EOL> def any ( self , criterion = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not self . property . uselist : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> return self . _criterion_exists ( criterion , ** kwargs ) <EOL> def has ( self , criterion = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . property . uselist : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return self . _criterion_exists ( criterion , ** kwargs ) <EOL> def contains ( self , other , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not self . property . uselist : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> clause = self . property . _optimized_compare ( <EOL> other , adapt_source = self . adapter ) <EOL> if self . property . secondaryjoin is not None : <EOL> clause . negation_clause = self . __negated_contains_or_equals ( other ) <EOL> return clause <EOL> def __negated_contains_or_equals ( self , other ) : <EOL> if self . property . direction == MANYTOONE : <EOL> state = attributes . instance_state ( other ) <EOL> def state_bindparam ( x , state , col ) : <EOL> o = state . obj ( ) <EOL> return sql . bindparam ( <EOL> x , unique = True , callable_ = lambda : <EOL> self . property . mapper . <EOL> _get_committed_attr_by_column ( o , col ) ) <EOL> def adapt ( col ) : <EOL> if self . adapter : <EOL> return self . adapter ( col ) <EOL> else : <EOL> return col <EOL> if self . property . _use_get : <EOL> return sql . and_ ( * [ <EOL> sql . or_ ( <EOL> adapt ( x ) != state_bindparam ( adapt ( x ) , state , y ) , <EOL> adapt ( x ) == None ) <EOL> for ( x , y ) in self . property . local_remote_pairs ] ) <EOL> criterion = sql . and_ ( * [ x == y for ( x , y ) in <EOL> zip ( <EOL> self . property . mapper . primary_key , <EOL> self . property . <EOL> mapper . <EOL> primary_key_from_instance ( other ) ) <EOL> ] ) <EOL> return ~ self . _criterion_exists ( criterion ) <EOL> def __ne__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( other , ( util . NoneType , expression . Null ) ) : <EOL> if self . property . direction == MANYTOONE : <EOL> return sql . or_ ( * [ x != None for x in <EOL> self . property . _calculated_foreign_keys ] ) <EOL> else : <EOL> return self . _criterion_exists ( ) <EOL> elif self . property . uselist : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> return self . __negated_contains_or_equals ( other ) <EOL> @ util . memoized_property <EOL> def property ( self ) : <EOL> if mapperlib . Mapper . _new_mappers : <EOL> mapperlib . Mapper . _configure_all ( ) <EOL> return self . prop <EOL> def compare ( self , op , value , <EOL> value_is_parent = False , <EOL> alias_secondary = True ) : <EOL> if op == operators . eq : <EOL> if value is None : <EOL> if self . uselist : <EOL> return ~ sql . exists ( [ <NUM_LIT:1> ] , self . primaryjoin ) <EOL> else : <EOL> return self . _optimized_compare ( <EOL> None , <EOL> value_is_parent = value_is_parent , <EOL> alias_secondary = alias_secondary ) <EOL> else : <EOL> return self . _optimized_compare ( <EOL> value , <EOL> value_is_parent = value_is_parent , <EOL> alias_secondary = alias_secondary ) <EOL> else : <EOL> return op ( self . comparator , value ) <EOL> def _optimized_compare ( self , value , value_is_parent = False , <EOL> adapt_source = None , <EOL> alias_secondary = True ) : <EOL> if value is not None : <EOL> value = attributes . instance_state ( value ) <EOL> return self . _lazy_strategy . lazy_clause ( <EOL> value , <EOL> reverse_direction = not value_is_parent , <EOL> alias_secondary = alias_secondary , <EOL> adapt_source = adapt_source ) <EOL> def __str__ ( self ) : <EOL> return str ( self . parent . class_ . __name__ ) + "<STR_LIT:.>" + self . key <EOL> def merge ( self , <EOL> session , <EOL> source_state , <EOL> source_dict , <EOL> dest_state , <EOL> dest_dict , <EOL> load , _recursive ) : <EOL> if load : <EOL> for r in self . _reverse_property : <EOL> if ( source_state , r ) in _recursive : <EOL> return <EOL> if "<STR_LIT>" not in self . _cascade : <EOL> return <EOL> if self . key not in source_dict : <EOL> return <EOL> if self . uselist : <EOL> instances = source_state . get_impl ( self . key ) . get ( source_state , source_dict ) <EOL> if hasattr ( instances , '<STR_LIT>' ) : <EOL> instances = instances . _sa_adapter <EOL> if load : <EOL> dest_state . get_impl ( self . key ) . get ( dest_state , dest_dict ) <EOL> dest_list = [ ] <EOL> for current in instances : <EOL> current_state = attributes . instance_state ( current ) <EOL> current_dict = attributes . instance_dict ( current ) <EOL> _recursive [ ( current_state , self ) ] = True <EOL> obj = session . _merge ( current_state , current_dict , <EOL> load = load , _recursive = _recursive ) <EOL> if obj is not None : <EOL> dest_list . append ( obj ) <EOL> if not load : <EOL> coll = attributes . init_state_collection ( dest_state , <EOL> dest_dict , self . key ) <EOL> for c in dest_list : <EOL> coll . append_without_event ( c ) <EOL> else : <EOL> dest_state . get_impl ( self . key ) . _set_iterable ( <EOL> dest_state , dest_dict , dest_list ) <EOL> else : <EOL> current = source_dict [ self . key ] <EOL> if current is not None : <EOL> current_state = attributes . instance_state ( current ) <EOL> current_dict = attributes . instance_dict ( current ) <EOL> _recursive [ ( current_state , self ) ] = True <EOL> obj = session . _merge ( current_state , current_dict , <EOL> load = load , _recursive = _recursive ) <EOL> else : <EOL> obj = None <EOL> if not load : <EOL> dest_dict [ self . key ] = obj <EOL> else : <EOL> dest_state . get_impl ( self . key ) . set ( dest_state , <EOL> dest_dict , obj , None ) <EOL> def _value_as_iterable ( self , state , dict_ , key , <EOL> passive = attributes . PASSIVE_OFF ) : <EOL> """<STR_LIT>""" <EOL> impl = state . manager [ key ] . impl <EOL> x = impl . get ( state , dict_ , passive = passive ) <EOL> if x is attributes . PASSIVE_NO_RESULT or x is None : <EOL> return [ ] <EOL> elif hasattr ( impl , '<STR_LIT>' ) : <EOL> return [ <EOL> ( attributes . instance_state ( o ) , o ) for o in <EOL> impl . get_collection ( state , dict_ , x , passive = passive ) <EOL> ] <EOL> else : <EOL> return [ ( attributes . instance_state ( x ) , x ) ] <EOL> def cascade_iterator ( self , type_ , state , dict_ , <EOL> visited_states , halt_on = None ) : <EOL> if type_ != '<STR_LIT>' or self . passive_deletes : <EOL> passive = attributes . PASSIVE_NO_INITIALIZE <EOL> else : <EOL> passive = attributes . PASSIVE_OFF <EOL> if type_ == '<STR_LIT>' : <EOL> tuples = state . manager [ self . key ] . impl . get_all_pending ( state , dict_ ) <EOL> else : <EOL> tuples = self . _value_as_iterable ( state , dict_ , self . key , <EOL> passive = passive ) <EOL> skip_pending = type_ == '<STR_LIT>' and '<STR_LIT>' not in self . _cascade <EOL> for instance_state , c in tuples : <EOL> if instance_state in visited_states : <EOL> continue <EOL> if c is None : <EOL> continue <EOL> instance_dict = attributes . instance_dict ( c ) <EOL> if halt_on and halt_on ( instance_state ) : <EOL> continue <EOL> if skip_pending and not instance_state . key : <EOL> continue <EOL> instance_mapper = instance_state . manager . mapper <EOL> if not instance_mapper . isa ( self . mapper . class_manager . mapper ) : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> self . key , <EOL> self . parent . class_ , <EOL> c . __class__ <EOL> ) ) <EOL> visited_states . add ( instance_state ) <EOL> yield c , instance_mapper , instance_state , instance_dict <EOL> def _add_reverse_property ( self , key ) : <EOL> other = self . mapper . get_property ( key , _configure_mappers = False ) <EOL> self . _reverse_property . add ( other ) <EOL> other . _reverse_property . add ( self ) <EOL> if not other . mapper . common_parent ( self . parent ) : <EOL> raise sa_exc . ArgumentError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( key , self , other , self . parent ) ) <EOL> if self . direction in ( ONETOMANY , MANYTOONE ) and self . direction == other . direction : <EOL> raise sa_exc . ArgumentError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( other , self , self . direction ) ) <EOL> @ util . memoized_property <EOL> def mapper ( self ) : <EOL> """<STR_LIT>""" <EOL> if util . callable ( self . argument ) and not isinstance ( self . argument , ( type , mapperlib . Mapper ) ) : <EOL> argument = self . argument ( ) <EOL> else : <EOL> argument = self . argument <EOL> if isinstance ( argument , type ) : <EOL> mapper_ = mapperlib . class_mapper ( argument , <EOL> configure = False ) <EOL> elif isinstance ( self . argument , mapperlib . Mapper ) : <EOL> mapper_ = argument <EOL> else : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( self . key , type ( argument ) ) ) <EOL> return mapper_ <EOL> @ util . memoized_property <EOL> @ util . deprecated ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def table ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . target <EOL> def do_init ( self ) : <EOL> self . _check_conflicts ( ) <EOL> self . _process_dependent_arguments ( ) <EOL> self . _setup_join_conditions ( ) <EOL> self . _check_cascade_settings ( self . _cascade ) <EOL> self . _post_init ( ) <EOL> self . _generate_backref ( ) <EOL> super ( RelationshipProperty , self ) . do_init ( ) <EOL> self . _lazy_strategy = self . _get_strategy ( ( ( "<STR_LIT>" , "<STR_LIT>" ) , ) ) <EOL> def _process_dependent_arguments ( self ) : <EOL> """<STR_LIT>""" <EOL> for attr in ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> ) : <EOL> attr_value = getattr ( self , attr ) <EOL> if util . callable ( attr_value ) : <EOL> setattr ( self , attr , attr_value ( ) ) <EOL> for attr in '<STR_LIT>' , '<STR_LIT>' : <EOL> val = getattr ( self , attr ) <EOL> if val is not None : <EOL> setattr ( self , attr , _orm_deannotate ( <EOL> expression . _only_column_elements ( val , attr ) ) <EOL> ) <EOL> if self . order_by is not False and self . order_by is not None : <EOL> self . order_by = [ <EOL> expression . _only_column_elements ( x , "<STR_LIT>" ) <EOL> for x in <EOL> util . to_list ( self . order_by ) ] <EOL> self . _user_defined_foreign_keys = util . column_set ( <EOL> expression . _only_column_elements ( x , "<STR_LIT>" ) <EOL> for x in util . to_column_set ( <EOL> self . _user_defined_foreign_keys <EOL> ) ) <EOL> self . remote_side = util . column_set ( <EOL> expression . _only_column_elements ( x , "<STR_LIT>" ) <EOL> for x in <EOL> util . to_column_set ( self . remote_side ) ) <EOL> self . target = self . mapper . mapped_table <EOL> def _setup_join_conditions ( self ) : <EOL> self . _join_condition = jc = JoinCondition ( <EOL> parent_selectable = self . parent . mapped_table , <EOL> child_selectable = self . mapper . mapped_table , <EOL> parent_local_selectable = self . parent . local_table , <EOL> child_local_selectable = self . mapper . local_table , <EOL> primaryjoin = self . primaryjoin , <EOL> secondary = self . secondary , <EOL> secondaryjoin = self . secondaryjoin , <EOL> parent_equivalents = self . parent . _equivalent_columns , <EOL> child_equivalents = self . mapper . _equivalent_columns , <EOL> consider_as_foreign_keys = self . _user_defined_foreign_keys , <EOL> local_remote_pairs = self . local_remote_pairs , <EOL> remote_side = self . remote_side , <EOL> self_referential = self . _is_self_referential , <EOL> prop = self , <EOL> support_sync = not self . viewonly , <EOL> can_be_synced_fn = self . _columns_are_mapped <EOL> ) <EOL> self . primaryjoin = jc . deannotated_primaryjoin <EOL> self . secondaryjoin = jc . deannotated_secondaryjoin <EOL> self . direction = jc . direction <EOL> self . local_remote_pairs = jc . local_remote_pairs <EOL> self . remote_side = jc . remote_columns <EOL> self . local_columns = jc . local_columns <EOL> self . synchronize_pairs = jc . synchronize_pairs <EOL> self . _calculated_foreign_keys = jc . foreign_key_columns <EOL> self . secondary_synchronize_pairs = jc . secondary_synchronize_pairs <EOL> def _check_conflicts ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_primary ( ) and not mapperlib . class_mapper ( <EOL> self . parent . class_ , <EOL> configure = False ) . has_property ( self . key ) : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( self . key , self . parent . class_ . __name__ , <EOL> self . parent . class_ . __name__ ) ) <EOL> if not self . parent . concrete : <EOL> for inheriting in self . parent . iterate_to_root ( ) : <EOL> if inheriting is not self . parent and inheriting . has_property ( self . key ) : <EOL> util . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( self . key , self . parent , inheriting ) ) <EOL> def _get_cascade ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _cascade <EOL> def _set_cascade ( self , cascade ) : <EOL> cascade = CascadeOptions ( cascade ) <EOL> if '<STR_LIT>' in self . __dict__ : <EOL> self . _check_cascade_settings ( cascade ) <EOL> self . _cascade = cascade <EOL> if self . _dependency_processor : <EOL> self . _dependency_processor . cascade = cascade <EOL> cascade = property ( _get_cascade , _set_cascade ) <EOL> def _check_cascade_settings ( self , cascade ) : <EOL> if cascade . delete_orphan and not self . single_parent and ( self . direction is MANYTOMANY or self . direction <EOL> is MANYTOONE ) : <EOL> raise sa_exc . ArgumentError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % self ) <EOL> if self . direction is MANYTOONE and self . passive_deletes : <EOL> util . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % self ) <EOL> if self . passive_deletes == '<STR_LIT:all>' and ( "<STR_LIT>" in cascade or <EOL> "<STR_LIT>" in cascade ) : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self ) <EOL> if cascade . delete_orphan : <EOL> self . mapper . primary_mapper ( ) . _delete_orphans . append ( <EOL> ( self . key , self . parent . class_ ) <EOL> ) <EOL> def _columns_are_mapped ( self , * cols ) : <EOL> """<STR_LIT>""" <EOL> for c in cols : <EOL> if self . secondary is not None and self . secondary . c . contains_column ( c ) : <EOL> continue <EOL> if not self . parent . mapped_table . c . contains_column ( c ) and not self . target . c . contains_column ( c ) : <EOL> return False <EOL> return True <EOL> def _generate_backref ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_primary ( ) : <EOL> return <EOL> if self . backref is not None and not self . back_populates : <EOL> if isinstance ( self . backref , util . string_types ) : <EOL> backref_key , kwargs = self . backref , { } <EOL> else : <EOL> backref_key , kwargs = self . backref <EOL> mapper = self . mapper . primary_mapper ( ) <EOL> check = set ( mapper . iterate_to_root ( ) ) . union ( mapper . self_and_descendants ) <EOL> for m in check : <EOL> if m . has_property ( backref_key ) : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( backref_key , self , m ) ) <EOL> if self . secondary is not None : <EOL> pj = kwargs . pop ( <EOL> '<STR_LIT>' , <EOL> self . _join_condition . secondaryjoin_minus_local ) <EOL> sj = kwargs . pop ( <EOL> '<STR_LIT>' , <EOL> self . _join_condition . primaryjoin_minus_local ) <EOL> else : <EOL> pj = kwargs . pop ( <EOL> '<STR_LIT>' , <EOL> self . _join_condition . primaryjoin_reverse_remote ) <EOL> sj = kwargs . pop ( '<STR_LIT>' , None ) <EOL> if sj : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> foreign_keys = kwargs . pop ( '<STR_LIT>' , <EOL> self . _user_defined_foreign_keys ) <EOL> parent = self . parent . primary_mapper ( ) <EOL> kwargs . setdefault ( '<STR_LIT>' , self . viewonly ) <EOL> kwargs . setdefault ( '<STR_LIT>' , self . post_update ) <EOL> kwargs . setdefault ( '<STR_LIT>' , self . passive_updates ) <EOL> self . back_populates = backref_key <EOL> relationship = RelationshipProperty ( <EOL> parent , self . secondary , <EOL> pj , sj , <EOL> foreign_keys = foreign_keys , <EOL> back_populates = self . key , <EOL> ** kwargs ) <EOL> mapper . _configure_property ( backref_key , relationship ) <EOL> if self . back_populates : <EOL> self . _add_reverse_property ( self . back_populates ) <EOL> def _post_init ( self ) : <EOL> if self . uselist is None : <EOL> self . uselist = self . direction is not MANYTOONE <EOL> if not self . viewonly : <EOL> self . _dependency_processor = dependency . DependencyProcessor . from_relationship ( self ) <EOL> @ util . memoized_property <EOL> def _use_get ( self ) : <EOL> """<STR_LIT>""" <EOL> strategy = self . _lazy_strategy <EOL> return strategy . use_get <EOL> @ util . memoized_property <EOL> def _is_self_referential ( self ) : <EOL> return self . mapper . common_parent ( self . parent ) <EOL> def _create_joins ( self , source_polymorphic = False , <EOL> source_selectable = None , dest_polymorphic = False , <EOL> dest_selectable = None , of_type = None ) : <EOL> if source_selectable is None : <EOL> if source_polymorphic and self . parent . with_polymorphic : <EOL> source_selectable = self . parent . _with_polymorphic_selectable <EOL> aliased = False <EOL> if dest_selectable is None : <EOL> if dest_polymorphic and self . mapper . with_polymorphic : <EOL> dest_selectable = self . mapper . _with_polymorphic_selectable <EOL> aliased = True <EOL> else : <EOL> dest_selectable = self . mapper . mapped_table <EOL> if self . _is_self_referential and source_selectable is None : <EOL> dest_selectable = dest_selectable . alias ( ) <EOL> aliased = True <EOL> else : <EOL> aliased = True <EOL> dest_mapper = of_type or self . mapper <EOL> single_crit = dest_mapper . _single_table_criterion <EOL> aliased = aliased or ( source_selectable is not None ) <EOL> primaryjoin , secondaryjoin , secondary , target_adapter , dest_selectable = self . _join_condition . join_targets ( <EOL> source_selectable , dest_selectable , aliased , single_crit <EOL> ) <EOL> if source_selectable is None : <EOL> source_selectable = self . parent . local_table <EOL> if dest_selectable is None : <EOL> dest_selectable = self . mapper . local_table <EOL> return ( primaryjoin , secondaryjoin , source_selectable , <EOL> dest_selectable , secondary , target_adapter ) <EOL> def _annotate_columns ( element , annotations ) : <EOL> def clone ( elem ) : <EOL> if isinstance ( elem , expression . ColumnClause ) : <EOL> elem = elem . _annotate ( annotations . copy ( ) ) <EOL> elem . _copy_internals ( clone = clone ) <EOL> return elem <EOL> if element is not None : <EOL> element = clone ( element ) <EOL> return element <EOL> class JoinCondition ( object ) : <EOL> def __init__ ( self , <EOL> parent_selectable , <EOL> child_selectable , <EOL> parent_local_selectable , <EOL> child_local_selectable , <EOL> primaryjoin = None , <EOL> secondary = None , <EOL> secondaryjoin = None , <EOL> parent_equivalents = None , <EOL> child_equivalents = None , <EOL> consider_as_foreign_keys = None , <EOL> local_remote_pairs = None , <EOL> remote_side = None , <EOL> self_referential = False , <EOL> prop = None , <EOL> support_sync = True , <EOL> can_be_synced_fn = lambda * c : True <EOL> ) : <EOL> self . parent_selectable = parent_selectable <EOL> self . parent_local_selectable = parent_local_selectable <EOL> self . child_selectable = child_selectable <EOL> self . child_local_selectable = child_local_selectable <EOL> self . parent_equivalents = parent_equivalents <EOL> self . child_equivalents = child_equivalents <EOL> self . primaryjoin = primaryjoin <EOL> self . secondaryjoin = secondaryjoin <EOL> self . secondary = secondary <EOL> self . consider_as_foreign_keys = consider_as_foreign_keys <EOL> self . _local_remote_pairs = local_remote_pairs <EOL> self . _remote_side = remote_side <EOL> self . prop = prop <EOL> self . self_referential = self_referential <EOL> self . support_sync = support_sync <EOL> self . can_be_synced_fn = can_be_synced_fn <EOL> self . _determine_joins ( ) <EOL> self . _annotate_fks ( ) <EOL> self . _annotate_remote ( ) <EOL> self . _annotate_local ( ) <EOL> self . _setup_pairs ( ) <EOL> self . _check_foreign_cols ( self . primaryjoin , True ) <EOL> if self . secondaryjoin is not None : <EOL> self . _check_foreign_cols ( self . secondaryjoin , False ) <EOL> self . _determine_direction ( ) <EOL> self . _check_remote_side ( ) <EOL> self . _log_joins ( ) <EOL> def _log_joins ( self ) : <EOL> if self . prop is None : <EOL> return <EOL> log = self . prop . logger <EOL> log . info ( '<STR_LIT>' , self . prop , <EOL> self . primaryjoin ) <EOL> log . info ( '<STR_LIT>' , self . prop , <EOL> self . secondaryjoin ) <EOL> log . info ( '<STR_LIT>' , self . prop , <EOL> '<STR_LIT:U+002C>' . join ( '<STR_LIT>' % ( l , r ) for ( l , r ) in <EOL> self . synchronize_pairs ) ) <EOL> log . info ( '<STR_LIT>' , self . prop , <EOL> '<STR_LIT:U+002C>' . join ( '<STR_LIT>' % ( l , r ) for ( l , r ) in <EOL> self . secondary_synchronize_pairs or [ ] ) ) <EOL> log . info ( '<STR_LIT>' , self . prop , <EOL> '<STR_LIT:U+002C>' . join ( '<STR_LIT>' % ( l , r ) for ( l , r ) in <EOL> self . local_remote_pairs ) ) <EOL> log . info ( '<STR_LIT>' , self . prop , <EOL> '<STR_LIT:U+002C>' . join ( '<STR_LIT:%s>' % col for col in self . remote_columns ) <EOL> ) <EOL> log . info ( '<STR_LIT>' , self . prop , <EOL> '<STR_LIT:U+002C>' . join ( '<STR_LIT:%s>' % col for col in self . local_columns ) <EOL> ) <EOL> log . info ( '<STR_LIT>' , self . prop , <EOL> self . direction ) <EOL> def _determine_joins ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . secondaryjoin is not None and self . secondary is None : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . prop ) <EOL> try : <EOL> consider_as_foreign_keys = self . consider_as_foreign_keys or None <EOL> if self . secondary is not None : <EOL> if self . secondaryjoin is None : <EOL> self . secondaryjoin = join_condition ( <EOL> self . child_selectable , <EOL> self . secondary , <EOL> a_subset = self . child_local_selectable , <EOL> consider_as_foreign_keys = consider_as_foreign_keys <EOL> ) <EOL> if self . primaryjoin is None : <EOL> self . primaryjoin = join_condition ( <EOL> self . parent_selectable , <EOL> self . secondary , <EOL> a_subset = self . parent_local_selectable , <EOL> consider_as_foreign_keys = consider_as_foreign_keys <EOL> ) <EOL> else : <EOL> if self . primaryjoin is None : <EOL> self . primaryjoin = join_condition ( <EOL> self . parent_selectable , <EOL> self . child_selectable , <EOL> a_subset = self . parent_local_selectable , <EOL> consider_as_foreign_keys = consider_as_foreign_keys <EOL> ) <EOL> except sa_exc . NoForeignKeysError : <EOL> if self . secondary is not None : <EOL> raise sa_exc . NoForeignKeysError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( self . prop , self . secondary ) ) <EOL> else : <EOL> raise sa_exc . NoForeignKeysError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . prop ) <EOL> except sa_exc . AmbiguousForeignKeysError : <EOL> if self . secondary is not None : <EOL> raise sa_exc . AmbiguousForeignKeysError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( self . prop , self . secondary ) ) <EOL> else : <EOL> raise sa_exc . AmbiguousForeignKeysError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % self . prop ) <EOL> @ property <EOL> def primaryjoin_minus_local ( self ) : <EOL> return _deep_deannotate ( self . primaryjoin , values = ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> @ property <EOL> def secondaryjoin_minus_local ( self ) : <EOL> return _deep_deannotate ( self . secondaryjoin , <EOL> values = ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> @ util . memoized_property <EOL> def primaryjoin_reverse_remote ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _has_remote_annotations : <EOL> def replace ( element ) : <EOL> if "<STR_LIT>" in element . _annotations : <EOL> v = element . _annotations . copy ( ) <EOL> del v [ '<STR_LIT>' ] <EOL> v [ '<STR_LIT>' ] = True <EOL> return element . _with_annotations ( v ) <EOL> elif "<STR_LIT>" in element . _annotations : <EOL> v = element . _annotations . copy ( ) <EOL> del v [ '<STR_LIT>' ] <EOL> v [ '<STR_LIT>' ] = True <EOL> return element . _with_annotations ( v ) <EOL> return visitors . replacement_traverse ( <EOL> self . primaryjoin , { } , replace ) <EOL> else : <EOL> if self . _has_foreign_annotations : <EOL> return _deep_deannotate ( self . primaryjoin , <EOL> values = ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> else : <EOL> return _deep_deannotate ( self . primaryjoin ) <EOL> def _has_annotation ( self , clause , annotation ) : <EOL> for col in visitors . iterate ( clause , { } ) : <EOL> if annotation in col . _annotations : <EOL> return True <EOL> else : <EOL> return False <EOL> @ util . memoized_property <EOL> def _has_foreign_annotations ( self ) : <EOL> return self . _has_annotation ( self . primaryjoin , "<STR_LIT>" ) <EOL> @ util . memoized_property <EOL> def _has_remote_annotations ( self ) : <EOL> return self . _has_annotation ( self . primaryjoin , "<STR_LIT>" ) <EOL> def _annotate_fks ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _has_foreign_annotations : <EOL> return <EOL> if self . consider_as_foreign_keys : <EOL> self . _annotate_from_fk_list ( ) <EOL> else : <EOL> self . _annotate_present_fks ( ) <EOL> def _annotate_from_fk_list ( self ) : <EOL> def check_fk ( col ) : <EOL> if col in self . consider_as_foreign_keys : <EOL> return col . _annotate ( { "<STR_LIT>" : True } ) <EOL> self . primaryjoin = visitors . replacement_traverse ( <EOL> self . primaryjoin , <EOL> { } , <EOL> check_fk <EOL> ) <EOL> if self . secondaryjoin is not None : <EOL> self . secondaryjoin = visitors . replacement_traverse ( <EOL> self . secondaryjoin , <EOL> { } , <EOL> check_fk <EOL> ) <EOL> def _annotate_present_fks ( self ) : <EOL> if self . secondary is not None : <EOL> secondarycols = util . column_set ( self . secondary . c ) <EOL> else : <EOL> secondarycols = set ( ) <EOL> def is_foreign ( a , b ) : <EOL> if isinstance ( a , schema . Column ) and isinstance ( b , schema . Column ) : <EOL> if a . references ( b ) : <EOL> return a <EOL> elif b . references ( a ) : <EOL> return b <EOL> if secondarycols : <EOL> if a in secondarycols and b not in secondarycols : <EOL> return a <EOL> elif b in secondarycols and a not in secondarycols : <EOL> return b <EOL> def visit_binary ( binary ) : <EOL> if not isinstance ( binary . left , sql . ColumnElement ) or not isinstance ( binary . right , sql . ColumnElement ) : <EOL> return <EOL> if "<STR_LIT>" not in binary . left . _annotations and "<STR_LIT>" not in binary . right . _annotations : <EOL> col = is_foreign ( binary . left , binary . right ) <EOL> if col is not None : <EOL> if col . compare ( binary . left ) : <EOL> binary . left = binary . left . _annotate ( <EOL> { "<STR_LIT>" : True } ) <EOL> elif col . compare ( binary . right ) : <EOL> binary . right = binary . right . _annotate ( <EOL> { "<STR_LIT>" : True } ) <EOL> self . primaryjoin = visitors . cloned_traverse ( <EOL> self . primaryjoin , <EOL> { } , <EOL> { "<STR_LIT>" : visit_binary } <EOL> ) <EOL> if self . secondaryjoin is not None : <EOL> self . secondaryjoin = visitors . cloned_traverse ( <EOL> self . secondaryjoin , <EOL> { } , <EOL> { "<STR_LIT>" : visit_binary } <EOL> ) <EOL> def _refers_to_parent_table ( self ) : <EOL> """<STR_LIT>""" <EOL> pt = self . parent_selectable <EOL> mt = self . child_selectable <EOL> result = [ False ] <EOL> def visit_binary ( binary ) : <EOL> c , f = binary . left , binary . right <EOL> if ( <EOL> isinstance ( c , expression . ColumnClause ) and <EOL> isinstance ( f , expression . ColumnClause ) and <EOL> pt . is_derived_from ( c . table ) and <EOL> pt . is_derived_from ( f . table ) and <EOL> mt . is_derived_from ( c . table ) and <EOL> mt . is_derived_from ( f . table ) <EOL> ) : <EOL> result [ <NUM_LIT:0> ] = True <EOL> visitors . traverse ( <EOL> self . primaryjoin , <EOL> { } , <EOL> { "<STR_LIT>" : visit_binary } <EOL> ) <EOL> return result [ <NUM_LIT:0> ] <EOL> def _tables_overlap ( self ) : <EOL> """<STR_LIT>""" <EOL> return selectables_overlap ( <EOL> self . parent_selectable , self . child_selectable ) <EOL> def _annotate_remote ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _has_remote_annotations : <EOL> return <EOL> if self . secondary is not None : <EOL> self . _annotate_remote_secondary ( ) <EOL> elif self . _local_remote_pairs or self . _remote_side : <EOL> self . _annotate_remote_from_args ( ) <EOL> elif self . _refers_to_parent_table ( ) : <EOL> self . _annotate_selfref ( lambda col : "<STR_LIT>" in col . _annotations , False ) <EOL> elif self . _tables_overlap ( ) : <EOL> self . _annotate_remote_with_overlap ( ) <EOL> else : <EOL> self . _annotate_remote_distinct_selectables ( ) <EOL> def _annotate_remote_secondary ( self ) : <EOL> """<STR_LIT>""" <EOL> def repl ( element ) : <EOL> if self . secondary . c . contains_column ( element ) : <EOL> return element . _annotate ( { "<STR_LIT>" : True } ) <EOL> self . primaryjoin = visitors . replacement_traverse ( <EOL> self . primaryjoin , { } , repl ) <EOL> self . secondaryjoin = visitors . replacement_traverse ( <EOL> self . secondaryjoin , { } , repl ) <EOL> def _annotate_selfref ( self , fn , remote_side_given ) : <EOL> """<STR_LIT>""" <EOL> def visit_binary ( binary ) : <EOL> equated = binary . left . compare ( binary . right ) <EOL> if isinstance ( binary . left , expression . ColumnClause ) and isinstance ( binary . right , expression . ColumnClause ) : <EOL> if fn ( binary . left ) : <EOL> binary . left = binary . left . _annotate ( { "<STR_LIT>" : True } ) <EOL> if fn ( binary . right ) and not equated : <EOL> binary . right = binary . right . _annotate ( <EOL> { "<STR_LIT>" : True } ) <EOL> elif not remote_side_given : <EOL> self . _warn_non_column_elements ( ) <EOL> self . primaryjoin = visitors . cloned_traverse ( <EOL> self . primaryjoin , { } , <EOL> { "<STR_LIT>" : visit_binary } ) <EOL> def _annotate_remote_from_args ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _local_remote_pairs : <EOL> if self . _remote_side : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> remote_side = [ r for ( l , r ) in self . _local_remote_pairs ] <EOL> else : <EOL> remote_side = self . _remote_side <EOL> if self . _refers_to_parent_table ( ) : <EOL> self . _annotate_selfref ( lambda col : col in remote_side , True ) <EOL> else : <EOL> def repl ( element ) : <EOL> if element in remote_side : <EOL> return element . _annotate ( { "<STR_LIT>" : True } ) <EOL> self . primaryjoin = visitors . replacement_traverse ( <EOL> self . primaryjoin , { } , repl ) <EOL> def _annotate_remote_with_overlap ( self ) : <EOL> """<STR_LIT>""" <EOL> def visit_binary ( binary ) : <EOL> binary . left , binary . right = proc_left_right ( binary . left , <EOL> binary . right ) <EOL> binary . right , binary . left = proc_left_right ( binary . right , <EOL> binary . left ) <EOL> def proc_left_right ( left , right ) : <EOL> if isinstance ( left , expression . ColumnClause ) and isinstance ( right , expression . ColumnClause ) : <EOL> if self . child_selectable . c . contains_column ( right ) and self . parent_selectable . c . contains_column ( left ) : <EOL> right = right . _annotate ( { "<STR_LIT>" : True } ) <EOL> else : <EOL> self . _warn_non_column_elements ( ) <EOL> return left , right <EOL> self . primaryjoin = visitors . cloned_traverse ( <EOL> self . primaryjoin , { } , <EOL> { "<STR_LIT>" : visit_binary } ) <EOL> def _annotate_remote_distinct_selectables ( self ) : <EOL> """<STR_LIT>""" <EOL> def repl ( element ) : <EOL> if self . child_selectable . c . contains_column ( element ) and ( not self . parent_local_selectable . c . <EOL> contains_column ( element ) or <EOL> self . child_local_selectable . c . <EOL> contains_column ( element ) ) : <EOL> return element . _annotate ( { "<STR_LIT>" : True } ) <EOL> self . primaryjoin = visitors . replacement_traverse ( <EOL> self . primaryjoin , { } , repl ) <EOL> def _warn_non_column_elements ( self ) : <EOL> util . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % self . prop <EOL> ) <EOL> def _annotate_local ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _has_annotation ( self . primaryjoin , "<STR_LIT>" ) : <EOL> return <EOL> if self . _local_remote_pairs : <EOL> local_side = util . column_set ( [ l for ( l , r ) <EOL> in self . _local_remote_pairs ] ) <EOL> else : <EOL> local_side = util . column_set ( self . parent_selectable . c ) <EOL> def locals_ ( elem ) : <EOL> if "<STR_LIT>" not in elem . _annotations and elem in local_side : <EOL> return elem . _annotate ( { "<STR_LIT>" : True } ) <EOL> self . primaryjoin = visitors . replacement_traverse ( <EOL> self . primaryjoin , { } , locals_ <EOL> ) <EOL> def _check_remote_side ( self ) : <EOL> if not self . local_remote_pairs : <EOL> raise sa_exc . ArgumentError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( self . prop , ) ) <EOL> def _check_foreign_cols ( self , join_condition , primary ) : <EOL> """<STR_LIT>""" <EOL> can_sync = False <EOL> foreign_cols = self . _gather_columns_with_annotation ( <EOL> join_condition , "<STR_LIT>" ) <EOL> has_foreign = bool ( foreign_cols ) <EOL> if primary : <EOL> can_sync = bool ( self . synchronize_pairs ) <EOL> else : <EOL> can_sync = bool ( self . secondary_synchronize_pairs ) <EOL> if self . support_sync and can_sync or ( not self . support_sync and has_foreign ) : <EOL> return <EOL> if self . support_sync and has_foreign and not can_sync : <EOL> err = "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" % ( <EOL> primary and '<STR_LIT>' or '<STR_LIT>' , <EOL> join_condition , <EOL> self . prop <EOL> ) <EOL> err += "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" <EOL> raise sa_exc . ArgumentError ( err ) <EOL> else : <EOL> err = "<STR_LIT>" "<STR_LIT>" % ( <EOL> primary and '<STR_LIT>' or '<STR_LIT>' , <EOL> join_condition , <EOL> self . prop <EOL> ) <EOL> err += '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' <EOL> raise sa_exc . ArgumentError ( err ) <EOL> def _determine_direction ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . secondaryjoin is not None : <EOL> self . direction = MANYTOMANY <EOL> else : <EOL> parentcols = util . column_set ( self . parent_selectable . c ) <EOL> targetcols = util . column_set ( self . child_selectable . c ) <EOL> onetomany_fk = targetcols . intersection ( <EOL> self . foreign_key_columns ) <EOL> manytoone_fk = parentcols . intersection ( <EOL> self . foreign_key_columns ) <EOL> if onetomany_fk and manytoone_fk : <EOL> onetomany_local = self . _gather_columns_with_annotation ( <EOL> self . primaryjoin , "<STR_LIT>" , "<STR_LIT>" ) <EOL> manytoone_local = set ( [ c for c in <EOL> self . _gather_columns_with_annotation ( <EOL> self . primaryjoin , <EOL> "<STR_LIT>" ) <EOL> if "<STR_LIT>" not in c . _annotations ] ) <EOL> if onetomany_local and manytoone_local : <EOL> self_equated = self . remote_columns . intersection ( <EOL> self . local_columns <EOL> ) <EOL> onetomany_local = onetomany_local . difference ( self_equated ) <EOL> manytoone_local = manytoone_local . difference ( self_equated ) <EOL> if onetomany_local and not manytoone_local : <EOL> self . direction = ONETOMANY <EOL> elif manytoone_local and not onetomany_local : <EOL> self . direction = MANYTOONE <EOL> else : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . prop ) <EOL> elif onetomany_fk : <EOL> self . direction = ONETOMANY <EOL> elif manytoone_fk : <EOL> self . direction = MANYTOONE <EOL> else : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . prop ) <EOL> def _deannotate_pairs ( self , collection ) : <EOL> """<STR_LIT>""" <EOL> return [ ( x . _deannotate ( ) , y . _deannotate ( ) ) <EOL> for x , y in collection ] <EOL> def _setup_pairs ( self ) : <EOL> sync_pairs = [ ] <EOL> lrp = util . OrderedSet ( [ ] ) <EOL> secondary_sync_pairs = [ ] <EOL> def go ( joincond , collection ) : <EOL> def visit_binary ( binary , left , right ) : <EOL> if "<STR_LIT>" in right . _annotations and "<STR_LIT>" not in left . _annotations and self . can_be_synced_fn ( left ) : <EOL> lrp . add ( ( left , right ) ) <EOL> elif "<STR_LIT>" in left . _annotations and "<STR_LIT>" not in right . _annotations and self . can_be_synced_fn ( right ) : <EOL> lrp . add ( ( right , left ) ) <EOL> if binary . operator is operators . eq and self . can_be_synced_fn ( left , right ) : <EOL> if "<STR_LIT>" in right . _annotations : <EOL> collection . append ( ( left , right ) ) <EOL> elif "<STR_LIT>" in left . _annotations : <EOL> collection . append ( ( right , left ) ) <EOL> visit_binary_product ( visit_binary , joincond ) <EOL> for joincond , collection in [ <EOL> ( self . primaryjoin , sync_pairs ) , <EOL> ( self . secondaryjoin , secondary_sync_pairs ) <EOL> ] : <EOL> if joincond is None : <EOL> continue <EOL> go ( joincond , collection ) <EOL> self . local_remote_pairs = self . _deannotate_pairs ( lrp ) <EOL> self . synchronize_pairs = self . _deannotate_pairs ( sync_pairs ) <EOL> self . secondary_synchronize_pairs = self . _deannotate_pairs ( secondary_sync_pairs ) <EOL> @ util . memoized_property <EOL> def remote_columns ( self ) : <EOL> return self . _gather_join_annotations ( "<STR_LIT>" ) <EOL> @ util . memoized_property <EOL> def local_columns ( self ) : <EOL> return self . _gather_join_annotations ( "<STR_LIT>" ) <EOL> @ util . memoized_property <EOL> def foreign_key_columns ( self ) : <EOL> return self . _gather_join_annotations ( "<STR_LIT>" ) <EOL> @ util . memoized_property <EOL> def deannotated_primaryjoin ( self ) : <EOL> return _deep_deannotate ( self . primaryjoin ) <EOL> @ util . memoized_property <EOL> def deannotated_secondaryjoin ( self ) : <EOL> if self . secondaryjoin is not None : <EOL> return _deep_deannotate ( self . secondaryjoin ) <EOL> else : <EOL> return None <EOL> def _gather_join_annotations ( self , annotation ) : <EOL> s = set ( <EOL> self . _gather_columns_with_annotation ( <EOL> self . primaryjoin , annotation ) <EOL> ) <EOL> if self . secondaryjoin is not None : <EOL> s . update ( <EOL> self . _gather_columns_with_annotation ( <EOL> self . secondaryjoin , annotation ) <EOL> ) <EOL> return set ( [ x . _deannotate ( ) for x in s ] ) <EOL> def _gather_columns_with_annotation ( self , clause , * annotation ) : <EOL> annotation = set ( annotation ) <EOL> return set ( [ <EOL> col for col in visitors . iterate ( clause , { } ) <EOL> if annotation . issubset ( col . _annotations ) <EOL> ] ) <EOL> def join_targets ( self , source_selectable , <EOL> dest_selectable , <EOL> aliased , <EOL> single_crit = None ) : <EOL> """<STR_LIT>""" <EOL> dest_selectable = _shallow_annotate ( <EOL> dest_selectable , <EOL> { '<STR_LIT>' : True } ) <EOL> primaryjoin , secondaryjoin , secondary = self . primaryjoin , self . secondaryjoin , self . secondary <EOL> if single_crit is not None : <EOL> if secondaryjoin is not None : <EOL> secondaryjoin = secondaryjoin & single_crit <EOL> else : <EOL> primaryjoin = primaryjoin & single_crit <EOL> if aliased : <EOL> if secondary is not None : <EOL> secondary = secondary . alias ( flat = True ) <EOL> primary_aliasizer = ClauseAdapter ( secondary ) <EOL> secondary_aliasizer = ClauseAdapter ( dest_selectable , <EOL> equivalents = self . child_equivalents ) . chain ( primary_aliasizer ) <EOL> if source_selectable is not None : <EOL> primary_aliasizer = ClauseAdapter ( secondary ) . chain ( ClauseAdapter ( <EOL> source_selectable , <EOL> equivalents = self . parent_equivalents ) ) <EOL> secondaryjoin = secondary_aliasizer . traverse ( secondaryjoin ) <EOL> else : <EOL> primary_aliasizer = ClauseAdapter ( <EOL> dest_selectable , <EOL> exclude_fn = _ColInAnnotations ( "<STR_LIT>" ) , <EOL> equivalents = self . child_equivalents ) <EOL> if source_selectable is not None : <EOL> primary_aliasizer . chain ( <EOL> ClauseAdapter ( source_selectable , <EOL> exclude_fn = _ColInAnnotations ( "<STR_LIT>" ) , <EOL> equivalents = self . parent_equivalents ) ) <EOL> secondary_aliasizer = None <EOL> primaryjoin = primary_aliasizer . traverse ( primaryjoin ) <EOL> target_adapter = secondary_aliasizer or primary_aliasizer <EOL> target_adapter . exclude_fn = None <EOL> else : <EOL> target_adapter = None <EOL> return primaryjoin , secondaryjoin , secondary , target_adapter , dest_selectable <EOL> def create_lazy_clause ( self , reverse_direction = False ) : <EOL> binds = util . column_dict ( ) <EOL> lookup = collections . defaultdict ( list ) <EOL> equated_columns = util . column_dict ( ) <EOL> if reverse_direction and self . secondaryjoin is None : <EOL> for l , r in self . local_remote_pairs : <EOL> lookup [ r ] . append ( ( r , l ) ) <EOL> equated_columns [ l ] = r <EOL> else : <EOL> for l , r in self . local_remote_pairs : <EOL> lookup [ l ] . append ( ( l , r ) ) <EOL> equated_columns [ r ] = l <EOL> def col_to_bind ( col ) : <EOL> if ( reverse_direction and col in lookup ) or ( not reverse_direction and "<STR_LIT>" in col . _annotations ) : <EOL> if col in lookup : <EOL> for tobind , equated in lookup [ col ] : <EOL> if equated in binds : <EOL> return None <EOL> if col not in binds : <EOL> binds [ col ] = sql . bindparam ( <EOL> None , None , type_ = col . type , unique = True ) <EOL> return binds [ col ] <EOL> return None <EOL> lazywhere = self . primaryjoin <EOL> if self . secondaryjoin is None or not reverse_direction : <EOL> lazywhere = visitors . replacement_traverse ( <EOL> lazywhere , { } , col_to_bind ) <EOL> if self . secondaryjoin is not None : <EOL> secondaryjoin = self . secondaryjoin <EOL> if reverse_direction : <EOL> secondaryjoin = visitors . replacement_traverse ( <EOL> secondaryjoin , { } , col_to_bind ) <EOL> lazywhere = sql . and_ ( lazywhere , secondaryjoin ) <EOL> bind_to_col = dict ( ( binds [ col ] . key , col ) for col in binds ) <EOL> lazywhere = _deep_deannotate ( lazywhere ) <EOL> return lazywhere , bind_to_col , equated_columns <EOL> class _ColInAnnotations ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def __call__ ( self , c ) : <EOL> return self . name in c . _annotations </s>
