<s> """<STR_LIT>""" <EOL> from itertools import chain <EOL> from . import ( <EOL> attributes , interfaces , object_mapper , persistence , <EOL> exc as orm_exc , loading <EOL> ) <EOL> from . base import _entity_descriptor , _is_aliased_class , _is_mapped_class , _orm_columns , _generative <EOL> from . path_registry import PathRegistry <EOL> from . util import ( <EOL> AliasedClass , ORMAdapter , join as orm_join , with_parent , aliased <EOL> ) <EOL> from . . import sql , util , log , exc as sa_exc , inspect , inspection <EOL> from . . sql . expression import _interpret_as_from <EOL> from . . sql import ( <EOL> util as sql_util , <EOL> expression , visitors <EOL> ) <EOL> from . . sql . base import ColumnCollection <EOL> from . import properties <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> _path_registry = PathRegistry . root <EOL> @ inspection . _self_inspects <EOL> @ log . class_logger <EOL> class Query ( object ) : <EOL> """<STR_LIT>""" <EOL> _enable_eagerloads = True <EOL> _enable_assertions = True <EOL> _with_labels = False <EOL> _criterion = None <EOL> _yield_per = None <EOL> _order_by = False <EOL> _group_by = False <EOL> _having = None <EOL> _distinct = False <EOL> _prefixes = None <EOL> _offset = None <EOL> _limit = None <EOL> _for_update_arg = None <EOL> _statement = None <EOL> _correlate = frozenset ( ) <EOL> _populate_existing = False <EOL> _invoke_all_eagers = True <EOL> _version_check = False <EOL> _autoflush = True <EOL> _only_load_props = None <EOL> _refresh_state = None <EOL> _from_obj = ( ) <EOL> _join_entities = ( ) <EOL> _select_from_entity = None <EOL> _mapper_adapter_map = { } <EOL> _filter_aliases = None <EOL> _from_obj_alias = None <EOL> _joinpath = _joinpoint = util . immutabledict ( ) <EOL> _execution_options = util . immutabledict ( ) <EOL> _params = util . immutabledict ( ) <EOL> _attributes = util . immutabledict ( ) <EOL> _with_options = ( ) <EOL> _with_hints = ( ) <EOL> _enable_single_crit = True <EOL> _current_path = _path_registry <EOL> def __init__ ( self , entities , session = None ) : <EOL> self . session = session <EOL> self . _polymorphic_adapters = { } <EOL> self . _set_entities ( entities ) <EOL> def _set_entities ( self , entities , entity_wrapper = None ) : <EOL> if entity_wrapper is None : <EOL> entity_wrapper = _QueryEntity <EOL> self . _entities = [ ] <EOL> self . _primary_entity = None <EOL> for ent in util . to_list ( entities ) : <EOL> entity_wrapper ( self , ent ) <EOL> self . _set_entity_selectables ( self . _entities ) <EOL> def _set_entity_selectables ( self , entities ) : <EOL> self . _mapper_adapter_map = d = self . _mapper_adapter_map . copy ( ) <EOL> for ent in entities : <EOL> for entity in ent . entities : <EOL> if entity not in d : <EOL> ext_info = inspect ( entity ) <EOL> if not ext_info . is_aliased_class and ext_info . mapper . with_polymorphic : <EOL> if ext_info . mapper . mapped_table not in self . _polymorphic_adapters : <EOL> self . _mapper_loads_polymorphically_with ( <EOL> ext_info . mapper , <EOL> sql_util . ColumnAdapter ( <EOL> ext_info . selectable , <EOL> ext_info . mapper . _equivalent_columns <EOL> ) <EOL> ) <EOL> aliased_adapter = None <EOL> elif ext_info . is_aliased_class : <EOL> aliased_adapter = sql_util . ColumnAdapter ( <EOL> ext_info . selectable , <EOL> ext_info . mapper . _equivalent_columns <EOL> ) <EOL> else : <EOL> aliased_adapter = None <EOL> d [ entity ] = ( <EOL> ext_info , <EOL> aliased_adapter <EOL> ) <EOL> ent . setup_entity ( * d [ entity ] ) <EOL> def _mapper_loads_polymorphically_with ( self , mapper , adapter ) : <EOL> for m2 in mapper . _with_polymorphic_mappers or [ mapper ] : <EOL> self . _polymorphic_adapters [ m2 ] = adapter <EOL> for m in m2 . iterate_to_root ( ) : <EOL> self . _polymorphic_adapters [ m . local_table ] = adapter <EOL> def _set_select_from ( self , obj , set_base_alias ) : <EOL> fa = [ ] <EOL> select_from_alias = None <EOL> for from_obj in obj : <EOL> info = inspect ( from_obj ) <EOL> if hasattr ( info , '<STR_LIT>' ) and ( info . is_mapper or info . is_aliased_class ) : <EOL> self . _select_from_entity = from_obj <EOL> if set_base_alias : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> fa . append ( info . selectable ) <EOL> elif not info . is_selectable : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> if isinstance ( from_obj , expression . SelectBase ) : <EOL> from_obj = from_obj . alias ( ) <EOL> if set_base_alias : <EOL> select_from_alias = from_obj <EOL> fa . append ( from_obj ) <EOL> self . _from_obj = tuple ( fa ) <EOL> if set_base_alias and len ( self . _from_obj ) == <NUM_LIT:1> and isinstance ( select_from_alias , expression . Alias ) : <EOL> equivs = self . __all_equivs ( ) <EOL> self . _from_obj_alias = sql_util . ColumnAdapter ( <EOL> self . _from_obj [ <NUM_LIT:0> ] , equivs ) <EOL> def _reset_polymorphic_adapter ( self , mapper ) : <EOL> for m2 in mapper . _with_polymorphic_mappers : <EOL> self . _polymorphic_adapters . pop ( m2 , None ) <EOL> for m in m2 . iterate_to_root ( ) : <EOL> self . _polymorphic_adapters . pop ( m . local_table , None ) <EOL> def _adapt_polymorphic_element ( self , element ) : <EOL> if "<STR_LIT>" in element . _annotations : <EOL> search = element . _annotations [ '<STR_LIT>' ] <EOL> alias = self . _polymorphic_adapters . get ( search , None ) <EOL> if alias : <EOL> return alias . adapt_clause ( element ) <EOL> if isinstance ( element , expression . FromClause ) : <EOL> search = element <EOL> elif hasattr ( element , '<STR_LIT>' ) : <EOL> search = element . table <EOL> else : <EOL> return None <EOL> alias = self . _polymorphic_adapters . get ( search , None ) <EOL> if alias : <EOL> return alias . adapt_clause ( element ) <EOL> def _adapt_col_list ( self , cols ) : <EOL> return [ <EOL> self . _adapt_clause ( <EOL> expression . _literal_as_text ( o ) , <EOL> True , True ) <EOL> for o in cols <EOL> ] <EOL> @ _generative ( ) <EOL> def _adapt_all_clauses ( self ) : <EOL> self . _orm_only_adapt = False <EOL> def _adapt_clause ( self , clause , as_filter , orm_only ) : <EOL> """<STR_LIT>""" <EOL> adapters = [ ] <EOL> orm_only = getattr ( self , '<STR_LIT>' , orm_only ) <EOL> if as_filter and self . _filter_aliases : <EOL> for fa in self . _filter_aliases . _visitor_iterator : <EOL> adapters . append ( <EOL> ( <EOL> orm_only , fa . replace <EOL> ) <EOL> ) <EOL> if self . _from_obj_alias : <EOL> adapters . append ( <EOL> ( <EOL> getattr ( self , '<STR_LIT>' , orm_only ) , <EOL> self . _from_obj_alias . replace <EOL> ) <EOL> ) <EOL> if self . _polymorphic_adapters : <EOL> adapters . append ( <EOL> ( <EOL> orm_only , self . _adapt_polymorphic_element <EOL> ) <EOL> ) <EOL> if not adapters : <EOL> return clause <EOL> def replace ( elem ) : <EOL> for _orm_only , adapter in adapters : <EOL> if not _orm_only or '<STR_LIT>' in elem . _annotations or "<STR_LIT>" in elem . _annotations : <EOL> e = adapter ( elem ) <EOL> if e is not None : <EOL> return e <EOL> return visitors . replacement_traverse ( <EOL> clause , <EOL> { } , <EOL> replace <EOL> ) <EOL> def _entity_zero ( self ) : <EOL> return self . _entities [ <NUM_LIT:0> ] <EOL> def _mapper_zero ( self ) : <EOL> return self . _select_from_entity or self . _entity_zero ( ) . entity_zero <EOL> @ property <EOL> def _mapper_entities ( self ) : <EOL> for ent in self . _entities : <EOL> if isinstance ( ent , _MapperEntity ) : <EOL> yield ent <EOL> def _joinpoint_zero ( self ) : <EOL> return self . _joinpoint . get ( <EOL> '<STR_LIT>' , <EOL> self . _mapper_zero ( ) <EOL> ) <EOL> def _mapper_zero_or_none ( self ) : <EOL> if self . _primary_entity : <EOL> return self . _primary_entity . mapper <EOL> else : <EOL> return None <EOL> def _only_mapper_zero ( self , rationale = None ) : <EOL> if len ( self . _entities ) > <NUM_LIT:1> : <EOL> raise sa_exc . InvalidRequestError ( <EOL> rationale or <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> return self . _mapper_zero ( ) <EOL> def _only_full_mapper_zero ( self , methname ) : <EOL> if self . _entities != [ self . _primary_entity ] : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % methname ) <EOL> return self . _primary_entity . entity_zero <EOL> def _only_entity_zero ( self , rationale = None ) : <EOL> if len ( self . _entities ) > <NUM_LIT:1> : <EOL> raise sa_exc . InvalidRequestError ( <EOL> rationale or <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> return self . _entity_zero ( ) <EOL> def __all_equivs ( self ) : <EOL> equivs = { } <EOL> for ent in self . _mapper_entities : <EOL> equivs . update ( ent . mapper . _equivalent_columns ) <EOL> return equivs <EOL> def _get_condition ( self ) : <EOL> return self . _no_criterion_condition ( <EOL> "<STR_LIT>" , order_by = False , distinct = False ) <EOL> def _get_existing_condition ( self ) : <EOL> self . _no_criterion_assertion ( "<STR_LIT>" , order_by = False , distinct = False ) <EOL> def _no_criterion_assertion ( self , meth , order_by = True , distinct = True ) : <EOL> if not self . _enable_assertions : <EOL> return <EOL> if self . _criterion is not None or self . _statement is not None or self . _from_obj or self . _limit is not None or self . _offset is not None or self . _group_by or ( order_by and self . _order_by ) or ( distinct and self . _distinct ) : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % meth ) <EOL> def _no_criterion_condition ( self , meth , order_by = True , distinct = True ) : <EOL> self . _no_criterion_assertion ( meth , order_by , distinct ) <EOL> self . _from_obj = ( ) <EOL> self . _statement = self . _criterion = None <EOL> self . _order_by = self . _group_by = self . _distinct = False <EOL> def _no_clauseelement_condition ( self , meth ) : <EOL> if not self . _enable_assertions : <EOL> return <EOL> if self . _order_by : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % meth ) <EOL> self . _no_criterion_condition ( meth ) <EOL> def _no_statement_condition ( self , meth ) : <EOL> if not self . _enable_assertions : <EOL> return <EOL> if self . _statement is not None : <EOL> raise sa_exc . InvalidRequestError ( <EOL> ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % meth ) <EOL> def _no_limit_offset ( self , meth ) : <EOL> if not self . _enable_assertions : <EOL> return <EOL> if self . _limit is not None or self . _offset is not None : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( meth , meth ) <EOL> ) <EOL> def _no_select_modifiers ( self , meth ) : <EOL> if not self . _enable_assertions : <EOL> return <EOL> for attr , methname , notset in ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , False ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , False ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , False ) , <EOL> ) : <EOL> if getattr ( self , attr ) is not notset : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" % <EOL> ( meth , methname ) <EOL> ) <EOL> def _get_options ( self , populate_existing = None , <EOL> version_check = None , <EOL> only_load_props = None , <EOL> refresh_state = None ) : <EOL> if populate_existing : <EOL> self . _populate_existing = populate_existing <EOL> if version_check : <EOL> self . _version_check = version_check <EOL> if refresh_state : <EOL> self . _refresh_state = refresh_state <EOL> if only_load_props : <EOL> self . _only_load_props = set ( only_load_props ) <EOL> return self <EOL> def _clone ( self ) : <EOL> cls = self . __class__ <EOL> q = cls . __new__ ( cls ) <EOL> q . __dict__ = self . __dict__ . copy ( ) <EOL> return q <EOL> @ property <EOL> def statement ( self ) : <EOL> """<STR_LIT>""" <EOL> stmt = self . _compile_context ( labels = self . _with_labels ) . statement <EOL> if self . _params : <EOL> stmt = stmt . params ( self . _params ) <EOL> return stmt . _annotate ( { '<STR_LIT>' : True } ) <EOL> def subquery ( self , name = None , with_labels = False , reduce_columns = False ) : <EOL> """<STR_LIT>""" <EOL> q = self . enable_eagerloads ( False ) <EOL> if with_labels : <EOL> q = q . with_labels ( ) <EOL> q = q . statement <EOL> if reduce_columns : <EOL> q = q . reduce_columns ( ) <EOL> return q . alias ( name = name ) <EOL> def cte ( self , name = None , recursive = False ) : <EOL> """<STR_LIT>""" <EOL> return self . enable_eagerloads ( False ) . statement . cte ( name = name , recursive = recursive ) <EOL> def label ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . enable_eagerloads ( False ) . statement . label ( name ) <EOL> def as_scalar ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . enable_eagerloads ( False ) . statement . as_scalar ( ) <EOL> @ property <EOL> def selectable ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __clause_element__ ( ) <EOL> def __clause_element__ ( self ) : <EOL> return self . enable_eagerloads ( False ) . with_labels ( ) . statement <EOL> @ _generative ( ) <EOL> def enable_eagerloads ( self , value ) : <EOL> """<STR_LIT>""" <EOL> self . _enable_eagerloads = value <EOL> @ _generative ( ) <EOL> def with_labels ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _with_labels = True <EOL> @ _generative ( ) <EOL> def enable_assertions ( self , value ) : <EOL> """<STR_LIT>""" <EOL> self . _enable_assertions = value <EOL> @ property <EOL> def whereclause ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _criterion <EOL> @ _generative ( ) <EOL> def _with_current_path ( self , path ) : <EOL> """<STR_LIT>""" <EOL> self . _current_path = path <EOL> @ _generative ( _no_clauseelement_condition ) <EOL> def with_polymorphic ( self , <EOL> cls_or_mappers , <EOL> selectable = None , <EOL> polymorphic_on = None ) : <EOL> """<STR_LIT>""" <EOL> if not self . _primary_entity : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" ) <EOL> entity = self . _entities [ <NUM_LIT:0> ] . _clone ( ) <EOL> self . _entities = [ entity ] + self . _entities [ <NUM_LIT:1> : ] <EOL> entity . set_with_polymorphic ( self , <EOL> cls_or_mappers , <EOL> selectable = selectable , <EOL> polymorphic_on = polymorphic_on ) <EOL> @ _generative ( ) <EOL> def yield_per ( self , count ) : <EOL> """<STR_LIT>""" <EOL> self . _yield_per = count <EOL> self . _execution_options = self . _execution_options . union ( <EOL> { "<STR_LIT>" : True } ) <EOL> def get ( self , ident ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( ident , '<STR_LIT>' ) : <EOL> ident = ident . __composite_values__ ( ) <EOL> ident = util . to_list ( ident ) <EOL> mapper = self . _only_full_mapper_zero ( "<STR_LIT>" ) <EOL> if len ( ident ) != len ( mapper . primary_key ) : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> '<STR_LIT:U+002C>' . join ( "<STR_LIT>" % c for c in mapper . primary_key ) ) <EOL> key = mapper . identity_key_from_primary_key ( ident ) <EOL> if not self . _populate_existing and not mapper . always_refresh and self . _for_update_arg is None : <EOL> instance = loading . get_from_identity ( <EOL> self . session , key , attributes . PASSIVE_OFF ) <EOL> if instance is not None : <EOL> self . _get_existing_condition ( ) <EOL> if not issubclass ( instance . __class__ , mapper . class_ ) : <EOL> return None <EOL> return instance <EOL> return loading . load_on_ident ( self , key ) <EOL> @ _generative ( ) <EOL> def correlate ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> self . _correlate = self . _correlate . union ( <EOL> _interpret_as_from ( s ) <EOL> if s is not None else None <EOL> for s in args ) <EOL> @ _generative ( ) <EOL> def autoflush ( self , setting ) : <EOL> """<STR_LIT>""" <EOL> self . _autoflush = setting <EOL> @ _generative ( ) <EOL> def populate_existing ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _populate_existing = True <EOL> @ _generative ( ) <EOL> def _with_invoke_all_eagers ( self , value ) : <EOL> """<STR_LIT>""" <EOL> self . _invoke_all_eagers = value <EOL> def with_parent ( self , instance , property = None ) : <EOL> """<STR_LIT>""" <EOL> if property is None : <EOL> mapper = object_mapper ( instance ) <EOL> for prop in mapper . iterate_properties : <EOL> if isinstance ( prop , properties . RelationshipProperty ) and prop . mapper is self . _mapper_zero ( ) : <EOL> property = prop <EOL> break <EOL> else : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( <EOL> self . _mapper_zero ( ) . class_ . __name__ , <EOL> instance . __class__ . __name__ ) <EOL> ) <EOL> return self . filter ( with_parent ( instance , property ) ) <EOL> @ _generative ( ) <EOL> def add_entity ( self , entity , alias = None ) : <EOL> """<STR_LIT>""" <EOL> if alias is not None : <EOL> entity = aliased ( entity , alias ) <EOL> self . _entities = list ( self . _entities ) <EOL> m = _MapperEntity ( self , entity ) <EOL> self . _set_entity_selectables ( [ m ] ) <EOL> @ _generative ( ) <EOL> def with_session ( self , session ) : <EOL> """<STR_LIT>""" <EOL> self . session = session <EOL> def from_self ( self , * entities ) : <EOL> """<STR_LIT>""" <EOL> fromclause = self . with_labels ( ) . enable_eagerloads ( False ) . _set_enable_single_crit ( False ) . statement . correlate ( None ) <EOL> q = self . _from_selectable ( fromclause ) <EOL> if entities : <EOL> q . _set_entities ( entities ) <EOL> return q <EOL> @ _generative ( ) <EOL> def _set_enable_single_crit ( self , val ) : <EOL> self . _enable_single_crit = val <EOL> @ _generative ( ) <EOL> def _from_selectable ( self , fromclause ) : <EOL> for attr in ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) : <EOL> self . __dict__ . pop ( attr , None ) <EOL> self . _set_select_from ( [ fromclause ] , True ) <EOL> self . _orm_only_from_obj_alias = False <EOL> old_entities = self . _entities <EOL> self . _entities = [ ] <EOL> for e in old_entities : <EOL> e . adapt_to_selectable ( self , self . _from_obj [ <NUM_LIT:0> ] ) <EOL> def values ( self , * columns ) : <EOL> """<STR_LIT>""" <EOL> if not columns : <EOL> return iter ( ( ) ) <EOL> q = self . _clone ( ) <EOL> q . _set_entities ( columns , entity_wrapper = _ColumnEntity ) <EOL> if not q . _yield_per : <EOL> q . _yield_per = <NUM_LIT:10> <EOL> return iter ( q ) <EOL> _values = values <EOL> def value ( self , column ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return next ( self . values ( column ) ) [ <NUM_LIT:0> ] <EOL> except StopIteration : <EOL> return None <EOL> @ _generative ( ) <EOL> def with_entities ( self , * entities ) : <EOL> """<STR_LIT>""" <EOL> self . _set_entities ( entities ) <EOL> @ _generative ( ) <EOL> def add_columns ( self , * column ) : <EOL> """<STR_LIT>""" <EOL> self . _entities = list ( self . _entities ) <EOL> l = len ( self . _entities ) <EOL> for c in column : <EOL> _ColumnEntity ( self , c ) <EOL> self . _set_entity_selectables ( self . _entities [ l : ] ) <EOL> @ util . pending_deprecation ( "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> False ) <EOL> def add_column ( self , column ) : <EOL> """<STR_LIT>""" <EOL> return self . add_columns ( column ) <EOL> def options ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> return self . _options ( False , * args ) <EOL> def _conditional_options ( self , * args ) : <EOL> return self . _options ( True , * args ) <EOL> @ _generative ( ) <EOL> def _options ( self , conditional , * args ) : <EOL> self . _attributes = self . _attributes . copy ( ) <EOL> opts = tuple ( util . flatten_iterator ( args ) ) <EOL> self . _with_options = self . _with_options + opts <EOL> if conditional : <EOL> for opt in opts : <EOL> opt . process_query_conditionally ( self ) <EOL> else : <EOL> for opt in opts : <EOL> opt . process_query ( self ) <EOL> def with_transformation ( self , fn ) : <EOL> """<STR_LIT>""" <EOL> return fn ( self ) <EOL> @ _generative ( ) <EOL> def with_hint ( self , selectable , text , dialect_name = '<STR_LIT:*>' ) : <EOL> """<STR_LIT>""" <EOL> selectable = inspect ( selectable ) . selectable <EOL> self . _with_hints += ( ( selectable , text , dialect_name ) , ) <EOL> @ _generative ( ) <EOL> def execution_options ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . _execution_options = self . _execution_options . union ( kwargs ) <EOL> @ _generative ( ) <EOL> def with_lockmode ( self , mode ) : <EOL> """<STR_LIT>""" <EOL> self . _for_update_arg = LockmodeArg . parse_legacy_query ( mode ) <EOL> @ _generative ( ) <EOL> def with_for_update ( self , read = False , nowait = False , of = None ) : <EOL> """<STR_LIT>""" <EOL> self . _for_update_arg = LockmodeArg ( read = read , nowait = nowait , of = of ) <EOL> @ _generative ( ) <EOL> def params ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> kwargs . update ( args [ <NUM_LIT:0> ] ) <EOL> elif len ( args ) > <NUM_LIT:0> : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . _params = self . _params . copy ( ) <EOL> self . _params . update ( kwargs ) <EOL> @ _generative ( _no_statement_condition , _no_limit_offset ) <EOL> def filter ( self , * criterion ) : <EOL> """<STR_LIT>""" <EOL> for criterion in list ( criterion ) : <EOL> criterion = expression . _literal_as_text ( criterion ) <EOL> criterion = self . _adapt_clause ( criterion , True , True ) <EOL> if self . _criterion is not None : <EOL> self . _criterion = self . _criterion & criterion <EOL> else : <EOL> self . _criterion = criterion <EOL> def filter_by ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> clauses = [ _entity_descriptor ( self . _joinpoint_zero ( ) , key ) == value <EOL> for key , value in kwargs . items ( ) ] <EOL> return self . filter ( sql . and_ ( * clauses ) ) <EOL> @ _generative ( _no_statement_condition , _no_limit_offset ) <EOL> def order_by ( self , * criterion ) : <EOL> """<STR_LIT>""" <EOL> if len ( criterion ) == <NUM_LIT:1> : <EOL> if criterion [ <NUM_LIT:0> ] is False : <EOL> if '<STR_LIT>' in self . __dict__ : <EOL> del self . _order_by <EOL> return <EOL> if criterion [ <NUM_LIT:0> ] is None : <EOL> self . _order_by = None <EOL> return <EOL> criterion = self . _adapt_col_list ( criterion ) <EOL> if self . _order_by is False or self . _order_by is None : <EOL> self . _order_by = criterion <EOL> else : <EOL> self . _order_by = self . _order_by + criterion <EOL> @ _generative ( _no_statement_condition , _no_limit_offset ) <EOL> def group_by ( self , * criterion ) : <EOL> """<STR_LIT>""" <EOL> criterion = list ( chain ( * [ _orm_columns ( c ) for c in criterion ] ) ) <EOL> criterion = self . _adapt_col_list ( criterion ) <EOL> if self . _group_by is False : <EOL> self . _group_by = criterion <EOL> else : <EOL> self . _group_by = self . _group_by + criterion <EOL> @ _generative ( _no_statement_condition , _no_limit_offset ) <EOL> def having ( self , criterion ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( criterion , util . string_types ) : <EOL> criterion = sql . text ( criterion ) <EOL> if criterion is not None and not isinstance ( criterion , sql . ClauseElement ) : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> criterion = self . _adapt_clause ( criterion , True , True ) <EOL> if self . _having is not None : <EOL> self . _having = self . _having & criterion <EOL> else : <EOL> self . _having = criterion <EOL> def union ( self , * q ) : <EOL> """<STR_LIT>""" <EOL> return self . _from_selectable ( <EOL> expression . union ( * ( [ self ] + list ( q ) ) ) ) <EOL> def union_all ( self , * q ) : <EOL> """<STR_LIT>""" <EOL> return self . _from_selectable ( <EOL> expression . union_all ( * ( [ self ] + list ( q ) ) ) <EOL> ) <EOL> def intersect ( self , * q ) : <EOL> """<STR_LIT>""" <EOL> return self . _from_selectable ( <EOL> expression . intersect ( * ( [ self ] + list ( q ) ) ) <EOL> ) <EOL> def intersect_all ( self , * q ) : <EOL> """<STR_LIT>""" <EOL> return self . _from_selectable ( <EOL> expression . intersect_all ( * ( [ self ] + list ( q ) ) ) <EOL> ) <EOL> def except_ ( self , * q ) : <EOL> """<STR_LIT>""" <EOL> return self . _from_selectable ( <EOL> expression . except_ ( * ( [ self ] + list ( q ) ) ) <EOL> ) <EOL> def except_all ( self , * q ) : <EOL> """<STR_LIT>""" <EOL> return self . _from_selectable ( <EOL> expression . except_all ( * ( [ self ] + list ( q ) ) ) <EOL> ) <EOL> def join ( self , * props , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> aliased , from_joinpoint = kwargs . pop ( '<STR_LIT>' , False ) , kwargs . pop ( '<STR_LIT>' , False ) <EOL> if kwargs : <EOL> raise TypeError ( "<STR_LIT>" % <EOL> '<STR_LIT:U+002CU+0020>' . join ( sorted ( kwargs ) ) ) <EOL> return self . _join ( props , <EOL> outerjoin = False , create_aliases = aliased , <EOL> from_joinpoint = from_joinpoint ) <EOL> def outerjoin ( self , * props , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> aliased , from_joinpoint = kwargs . pop ( '<STR_LIT>' , False ) , kwargs . pop ( '<STR_LIT>' , False ) <EOL> if kwargs : <EOL> raise TypeError ( "<STR_LIT>" % <EOL> '<STR_LIT:U+002CU+0020>' . join ( sorted ( kwargs ) ) ) <EOL> return self . _join ( props , <EOL> outerjoin = True , create_aliases = aliased , <EOL> from_joinpoint = from_joinpoint ) <EOL> def _update_joinpoint ( self , jp ) : <EOL> self . _joinpoint = jp <EOL> while '<STR_LIT>' in jp : <EOL> f , prev = jp [ '<STR_LIT>' ] <EOL> prev = prev . copy ( ) <EOL> prev [ f ] = jp <EOL> jp [ '<STR_LIT>' ] = ( f , prev ) <EOL> jp = prev <EOL> self . _joinpath = jp <EOL> @ _generative ( _no_statement_condition , _no_limit_offset ) <EOL> def _join ( self , keys , outerjoin , create_aliases , from_joinpoint ) : <EOL> """<STR_LIT>""" <EOL> if not from_joinpoint : <EOL> self . _reset_joinpoint ( ) <EOL> if len ( keys ) == <NUM_LIT:2> and isinstance ( keys [ <NUM_LIT:0> ] , ( expression . FromClause , <EOL> type , AliasedClass ) ) and isinstance ( keys [ <NUM_LIT:1> ] , ( str , expression . ClauseElement , <EOL> interfaces . PropComparator ) ) : <EOL> keys = ( keys , ) <EOL> for arg1 in util . to_list ( keys ) : <EOL> if isinstance ( arg1 , tuple ) : <EOL> arg1 , arg2 = arg1 <EOL> else : <EOL> arg2 = None <EOL> if isinstance ( <EOL> arg1 , ( interfaces . PropComparator , util . string_types ) ) : <EOL> right_entity , onclause = arg2 , arg1 <EOL> else : <EOL> right_entity , onclause = arg1 , arg2 <EOL> left_entity = prop = None <EOL> if isinstance ( onclause , interfaces . PropComparator ) : <EOL> of_type = getattr ( onclause , '<STR_LIT>' , None ) <EOL> else : <EOL> of_type = None <EOL> if isinstance ( onclause , util . string_types ) : <EOL> left_entity = self . _joinpoint_zero ( ) <EOL> descriptor = _entity_descriptor ( left_entity , onclause ) <EOL> onclause = descriptor <EOL> elif from_joinpoint and isinstance ( onclause , interfaces . PropComparator ) : <EOL> left_entity = onclause . _parententity <EOL> info = inspect ( self . _joinpoint_zero ( ) ) <EOL> left_mapper , left_selectable , left_is_aliased = getattr ( info , '<STR_LIT>' , None ) , info . selectable , getattr ( info , '<STR_LIT>' , None ) <EOL> if left_mapper is left_entity : <EOL> left_entity = self . _joinpoint_zero ( ) <EOL> descriptor = _entity_descriptor ( left_entity , <EOL> onclause . key ) <EOL> onclause = descriptor <EOL> if isinstance ( onclause , interfaces . PropComparator ) : <EOL> if right_entity is None : <EOL> if of_type : <EOL> right_entity = of_type <EOL> else : <EOL> right_entity = onclause . property . mapper <EOL> left_entity = onclause . _parententity <EOL> prop = onclause . property <EOL> if not isinstance ( onclause , attributes . QueryableAttribute ) : <EOL> onclause = prop <EOL> if not create_aliases : <EOL> edge = ( left_entity , right_entity , prop . key ) <EOL> if edge in self . _joinpoint : <EOL> jp = self . _joinpoint [ edge ] . copy ( ) <EOL> jp [ '<STR_LIT>' ] = ( edge , self . _joinpoint ) <EOL> self . _update_joinpoint ( jp ) <EOL> continue <EOL> elif onclause is not None and right_entity is None : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> self . _join_left_to_right ( <EOL> left_entity , <EOL> right_entity , onclause , <EOL> outerjoin , create_aliases , prop ) <EOL> def _join_left_to_right ( self , left , right , <EOL> onclause , outerjoin , create_aliases , prop ) : <EOL> """<STR_LIT>""" <EOL> self . _polymorphic_adapters = self . _polymorphic_adapters . copy ( ) <EOL> if left is None : <EOL> if self . _from_obj : <EOL> left = self . _from_obj [ <NUM_LIT:0> ] <EOL> elif self . _entities : <EOL> left = self . _entities [ <NUM_LIT:0> ] . entity_zero_or_selectable <EOL> if left is None : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . _entities [ <NUM_LIT:0> ] ) <EOL> if left is right and not create_aliases : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( left , right ) ) <EOL> l_info = inspect ( left ) <EOL> r_info = inspect ( right ) <EOL> overlap = False <EOL> if not create_aliases : <EOL> right_mapper = getattr ( r_info , "<STR_LIT>" , None ) <EOL> if right_mapper and ( <EOL> right_mapper . with_polymorphic or <EOL> isinstance ( right_mapper . mapped_table , expression . Join ) <EOL> ) : <EOL> for from_obj in self . _from_obj or [ l_info . selectable ] : <EOL> if sql_util . selectables_overlap ( <EOL> l_info . selectable , from_obj ) and sql_util . selectables_overlap ( <EOL> from_obj , r_info . selectable ) : <EOL> overlap = True <EOL> break <EOL> elif sql_util . selectables_overlap ( l_info . selectable , <EOL> r_info . selectable ) : <EOL> overlap = True <EOL> if overlap and l_info . selectable is r_info . selectable : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" % <EOL> l_info . selectable ) <EOL> right , onclause = self . _prepare_right_side ( <EOL> r_info , right , onclause , <EOL> create_aliases , <EOL> prop , overlap ) <EOL> if not create_aliases and prop : <EOL> self . _update_joinpoint ( { <EOL> '<STR_LIT>' : right , <EOL> '<STR_LIT>' : ( ( left , right , prop . key ) , self . _joinpoint ) <EOL> } ) <EOL> else : <EOL> self . _joinpoint = { '<STR_LIT>' : right } <EOL> self . _join_to_left ( l_info , left , right , onclause , outerjoin ) <EOL> def _prepare_right_side ( self , r_info , right , onclause , create_aliases , <EOL> prop , overlap ) : <EOL> info = r_info <EOL> right_mapper , right_selectable , right_is_aliased = getattr ( info , '<STR_LIT>' , None ) , info . selectable , getattr ( info , '<STR_LIT>' , False ) <EOL> if right_mapper : <EOL> self . _join_entities += ( info , ) <EOL> if right_mapper and prop and not right_mapper . common_parent ( prop . mapper ) : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( right , onclause ) <EOL> ) <EOL> if not right_mapper and prop : <EOL> right_mapper = prop . mapper <EOL> need_adapter = False <EOL> if right_mapper and right is right_selectable : <EOL> if not right_selectable . is_derived_from ( <EOL> right_mapper . mapped_table ) : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" % <EOL> ( right_selectable . description , <EOL> right_mapper . mapped_table . description ) ) <EOL> if isinstance ( right_selectable , expression . SelectBase ) : <EOL> right_selectable = right_selectable . alias ( ) <EOL> need_adapter = True <EOL> right = aliased ( right_mapper , right_selectable ) <EOL> aliased_entity = right_mapper and not right_is_aliased and ( <EOL> right_mapper . with_polymorphic and isinstance ( <EOL> right_mapper . _with_polymorphic_selectable , <EOL> expression . Alias ) <EOL> or <EOL> overlap <EOL> ) <EOL> if not need_adapter and ( create_aliases or aliased_entity ) : <EOL> right = aliased ( right , flat = True ) <EOL> need_adapter = True <EOL> if need_adapter : <EOL> self . _filter_aliases = ORMAdapter ( <EOL> right , <EOL> equivalents = right_mapper and <EOL> right_mapper . _equivalent_columns or { } , <EOL> chain_to = self . _filter_aliases ) <EOL> if isinstance ( onclause , expression . ClauseElement ) : <EOL> onclause = self . _adapt_clause ( onclause , True , True ) <EOL> if aliased_entity and not create_aliases : <EOL> self . _mapper_loads_polymorphically_with ( <EOL> right_mapper , <EOL> ORMAdapter ( <EOL> right , <EOL> equivalents = right_mapper . _equivalent_columns <EOL> ) <EOL> ) <EOL> return right , onclause <EOL> def _join_to_left ( self , l_info , left , right , onclause , outerjoin ) : <EOL> info = l_info <EOL> left_mapper = getattr ( info , '<STR_LIT>' , None ) <EOL> left_selectable = info . selectable <EOL> if self . _from_obj : <EOL> replace_clause_index , clause = sql_util . find_join_source ( <EOL> self . _from_obj , <EOL> left_selectable ) <EOL> if clause is not None : <EOL> try : <EOL> clause = orm_join ( clause , <EOL> right , <EOL> onclause , isouter = outerjoin ) <EOL> except sa_exc . ArgumentError as ae : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( right , ae ) ) <EOL> self . _from_obj = self . _from_obj [ : replace_clause_index ] + ( clause , ) + self . _from_obj [ replace_clause_index + <NUM_LIT:1> : ] <EOL> return <EOL> if left_mapper : <EOL> for ent in self . _entities : <EOL> if ent . corresponds_to ( left ) : <EOL> clause = ent . selectable <EOL> break <EOL> else : <EOL> clause = left <EOL> else : <EOL> clause = left_selectable <EOL> assert clause is not None <EOL> try : <EOL> clause = orm_join ( clause , right , onclause , isouter = outerjoin ) <EOL> except sa_exc . ArgumentError as ae : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( right , ae ) ) <EOL> self . _from_obj = self . _from_obj + ( clause , ) <EOL> def _reset_joinpoint ( self ) : <EOL> self . _joinpoint = self . _joinpath <EOL> self . _filter_aliases = None <EOL> @ _generative ( _no_statement_condition ) <EOL> def reset_joinpoint ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _reset_joinpoint ( ) <EOL> @ _generative ( _no_clauseelement_condition ) <EOL> def select_from ( self , * from_obj ) : <EOL> """<STR_LIT>""" <EOL> self . _set_select_from ( from_obj , False ) <EOL> @ _generative ( _no_clauseelement_condition ) <EOL> def select_entity_from ( self , from_obj ) : <EOL> """<STR_LIT>""" <EOL> self . _set_select_from ( [ from_obj ] , True ) <EOL> def __getitem__ ( self , item ) : <EOL> if isinstance ( item , slice ) : <EOL> start , stop , step = util . decode_slice ( item ) <EOL> if isinstance ( stop , int ) and isinstance ( start , int ) and stop - start <= <NUM_LIT:0> : <EOL> return [ ] <EOL> elif ( isinstance ( start , int ) and start < <NUM_LIT:0> ) or ( isinstance ( stop , int ) and stop < <NUM_LIT:0> ) : <EOL> return list ( self ) [ item ] <EOL> res = self . slice ( start , stop ) <EOL> if step is not None : <EOL> return list ( res ) [ None : None : item . step ] <EOL> else : <EOL> return list ( res ) <EOL> else : <EOL> if item == - <NUM_LIT:1> : <EOL> return list ( self ) [ - <NUM_LIT:1> ] <EOL> else : <EOL> return list ( self [ item : item + <NUM_LIT:1> ] ) [ <NUM_LIT:0> ] <EOL> @ _generative ( _no_statement_condition ) <EOL> def slice ( self , start , stop ) : <EOL> """<STR_LIT>""" <EOL> if start is not None and stop is not None : <EOL> self . _offset = ( self . _offset or <NUM_LIT:0> ) + start <EOL> self . _limit = stop - start <EOL> elif start is None and stop is not None : <EOL> self . _limit = stop <EOL> elif start is not None and stop is None : <EOL> self . _offset = ( self . _offset or <NUM_LIT:0> ) + start <EOL> if self . _offset == <NUM_LIT:0> : <EOL> self . _offset = None <EOL> @ _generative ( _no_statement_condition ) <EOL> def limit ( self , limit ) : <EOL> """<STR_LIT>""" <EOL> self . _limit = limit <EOL> @ _generative ( _no_statement_condition ) <EOL> def offset ( self , offset ) : <EOL> """<STR_LIT>""" <EOL> self . _offset = offset <EOL> @ _generative ( _no_statement_condition ) <EOL> def distinct ( self , * criterion ) : <EOL> """<STR_LIT>""" <EOL> if not criterion : <EOL> self . _distinct = True <EOL> else : <EOL> criterion = self . _adapt_col_list ( criterion ) <EOL> if isinstance ( self . _distinct , list ) : <EOL> self . _distinct += criterion <EOL> else : <EOL> self . _distinct = criterion <EOL> @ _generative ( ) <EOL> def prefix_with ( self , * prefixes ) : <EOL> """<STR_LIT>""" <EOL> if self . _prefixes : <EOL> self . _prefixes += prefixes <EOL> else : <EOL> self . _prefixes = prefixes <EOL> def all ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self ) <EOL> @ _generative ( _no_clauseelement_condition ) <EOL> def from_statement ( self , statement ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( statement , util . string_types ) : <EOL> statement = sql . text ( statement ) <EOL> if not isinstance ( statement , <EOL> ( expression . TextClause , <EOL> expression . SelectBase ) ) : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . _statement = statement <EOL> def first ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _statement is not None : <EOL> ret = list ( self ) [ <NUM_LIT:0> : <NUM_LIT:1> ] <EOL> else : <EOL> ret = list ( self [ <NUM_LIT:0> : <NUM_LIT:1> ] ) <EOL> if len ( ret ) > <NUM_LIT:0> : <EOL> return ret [ <NUM_LIT:0> ] <EOL> else : <EOL> return None <EOL> def one ( self ) : <EOL> """<STR_LIT>""" <EOL> ret = list ( self ) <EOL> l = len ( ret ) <EOL> if l == <NUM_LIT:1> : <EOL> return ret [ <NUM_LIT:0> ] <EOL> elif l == <NUM_LIT:0> : <EOL> raise orm_exc . NoResultFound ( "<STR_LIT>" ) <EOL> else : <EOL> raise orm_exc . MultipleResultsFound ( <EOL> "<STR_LIT>" ) <EOL> def scalar ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ret = self . one ( ) <EOL> if not isinstance ( ret , tuple ) : <EOL> return ret <EOL> return ret [ <NUM_LIT:0> ] <EOL> except orm_exc . NoResultFound : <EOL> return None <EOL> def __iter__ ( self ) : <EOL> context = self . _compile_context ( ) <EOL> context . statement . use_labels = True <EOL> if self . _autoflush and not self . _populate_existing : <EOL> self . session . _autoflush ( ) <EOL> return self . _execute_and_instances ( context ) <EOL> def _connection_from_session ( self , ** kw ) : <EOL> conn = self . session . connection ( <EOL> ** kw ) <EOL> if self . _execution_options : <EOL> conn = conn . execution_options ( ** self . _execution_options ) <EOL> return conn <EOL> def _execute_and_instances ( self , querycontext ) : <EOL> conn = self . _connection_from_session ( <EOL> mapper = self . _mapper_zero_or_none ( ) , <EOL> clause = querycontext . statement , <EOL> close_with_result = True ) <EOL> result = conn . execute ( querycontext . statement , self . _params ) <EOL> return loading . instances ( self , result , querycontext ) <EOL> @ property <EOL> def column_descriptions ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ <EOL> { <EOL> '<STR_LIT:name>' : ent . _label_name , <EOL> '<STR_LIT:type>' : ent . type , <EOL> '<STR_LIT>' : getattr ( ent , '<STR_LIT>' , False ) , <EOL> '<STR_LIT>' : ent . expr <EOL> } <EOL> for ent in self . _entities <EOL> ] <EOL> def instances ( self , cursor , __context = None ) : <EOL> """<STR_LIT>""" <EOL> context = __context <EOL> if context is None : <EOL> context = QueryContext ( self ) <EOL> return loading . instances ( self , cursor , context ) <EOL> def merge_result ( self , iterator , load = True ) : <EOL> """<STR_LIT>""" <EOL> return loading . merge_result ( self , iterator , load ) <EOL> @ property <EOL> def _select_args ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . _limit , <EOL> '<STR_LIT>' : self . _offset , <EOL> '<STR_LIT>' : self . _distinct , <EOL> '<STR_LIT>' : self . _prefixes , <EOL> '<STR_LIT>' : self . _group_by or None , <EOL> '<STR_LIT>' : self . _having <EOL> } <EOL> @ property <EOL> def _should_nest_selectable ( self ) : <EOL> kwargs = self . _select_args <EOL> return ( kwargs . get ( '<STR_LIT>' ) is not None or <EOL> kwargs . get ( '<STR_LIT>' ) is not None or <EOL> kwargs . get ( '<STR_LIT>' , False ) ) <EOL> def exists ( self ) : <EOL> """<STR_LIT>""" <EOL> return sql . exists ( self . add_columns ( '<STR_LIT:1>' ) . with_labels ( ) . <EOL> statement . with_only_columns ( [ '<STR_LIT:1>' ] ) ) <EOL> def count ( self ) : <EOL> """<STR_LIT>""" <EOL> col = sql . func . count ( sql . literal_column ( '<STR_LIT:*>' ) ) <EOL> return self . from_self ( col ) . scalar ( ) <EOL> def delete ( self , synchronize_session = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> delete_op = persistence . BulkDelete . factory ( <EOL> self , synchronize_session ) <EOL> delete_op . exec_ ( ) <EOL> return delete_op . rowcount <EOL> def update ( self , values , synchronize_session = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> update_op = persistence . BulkUpdate . factory ( <EOL> self , synchronize_session , values ) <EOL> update_op . exec_ ( ) <EOL> return update_op . rowcount <EOL> def _compile_context ( self , labels = True ) : <EOL> context = QueryContext ( self ) <EOL> if context . statement is not None : <EOL> return context <EOL> context . labels = labels <EOL> context . _for_update_arg = self . _for_update_arg <EOL> for entity in self . _entities : <EOL> entity . setup_context ( self , context ) <EOL> for rec in context . create_eager_joins : <EOL> strategy = rec [ <NUM_LIT:0> ] <EOL> strategy ( * rec [ <NUM_LIT:1> : ] ) <EOL> if context . from_clause : <EOL> context . froms = list ( context . from_clause ) <EOL> else : <EOL> context . froms = context . froms <EOL> if self . _enable_single_crit : <EOL> self . _adjust_for_single_inheritance ( context ) <EOL> if not context . primary_columns : <EOL> if self . _only_load_props : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if context . multi_row_eager_loaders and self . _should_nest_selectable : <EOL> context . statement = self . _compound_eager_statement ( context ) <EOL> else : <EOL> context . statement = self . _simple_statement ( context ) <EOL> return context <EOL> def _compound_eager_statement ( self , context ) : <EOL> if context . order_by : <EOL> order_by_col_expr = list ( <EOL> chain ( * [ <EOL> sql_util . unwrap_order_by ( o ) <EOL> for o in context . order_by <EOL> ] ) <EOL> ) <EOL> else : <EOL> context . order_by = None <EOL> order_by_col_expr = [ ] <EOL> inner = sql . select ( <EOL> context . primary_columns + order_by_col_expr , <EOL> context . whereclause , <EOL> from_obj = context . froms , <EOL> use_labels = context . labels , <EOL> order_by = context . order_by , <EOL> ** self . _select_args <EOL> ) <EOL> for hint in self . _with_hints : <EOL> inner = inner . with_hint ( * hint ) <EOL> if self . _correlate : <EOL> inner = inner . correlate ( * self . _correlate ) <EOL> inner = inner . alias ( ) <EOL> equivs = self . __all_equivs ( ) <EOL> context . adapter = sql_util . ColumnAdapter ( inner , equivs ) <EOL> statement = sql . select ( <EOL> [ inner ] + context . secondary_columns , <EOL> use_labels = context . labels ) <EOL> statement . _for_update_arg = context . _for_update_arg <EOL> from_clause = inner <EOL> for eager_join in context . eager_joins . values ( ) : <EOL> from_clause = sql_util . splice_joins ( <EOL> from_clause , <EOL> eager_join , eager_join . stop_on ) <EOL> statement . append_from ( from_clause ) <EOL> if context . order_by : <EOL> statement . append_order_by ( <EOL> * context . adapter . copy_and_process ( <EOL> context . order_by <EOL> ) <EOL> ) <EOL> statement . append_order_by ( * context . eager_order_by ) <EOL> return statement <EOL> def _simple_statement ( self , context ) : <EOL> if not context . order_by : <EOL> context . order_by = None <EOL> if self . _distinct and context . order_by : <EOL> order_by_col_expr = list ( <EOL> chain ( * [ <EOL> sql_util . unwrap_order_by ( o ) <EOL> for o in context . order_by <EOL> ] ) <EOL> ) <EOL> context . primary_columns += order_by_col_expr <EOL> context . froms += tuple ( context . eager_joins . values ( ) ) <EOL> statement = sql . select ( <EOL> context . primary_columns + <EOL> context . secondary_columns , <EOL> context . whereclause , <EOL> from_obj = context . froms , <EOL> use_labels = context . labels , <EOL> order_by = context . order_by , <EOL> ** self . _select_args <EOL> ) <EOL> statement . _for_update_arg = context . _for_update_arg <EOL> for hint in self . _with_hints : <EOL> statement = statement . with_hint ( * hint ) <EOL> if self . _correlate : <EOL> statement = statement . correlate ( * self . _correlate ) <EOL> if context . eager_order_by : <EOL> statement . append_order_by ( * context . eager_order_by ) <EOL> return statement <EOL> def _adjust_for_single_inheritance ( self , context ) : <EOL> """<STR_LIT>""" <EOL> for ( ext_info , adapter ) in set ( self . _mapper_adapter_map . values ( ) ) : <EOL> if ext_info in self . _join_entities : <EOL> continue <EOL> single_crit = ext_info . mapper . _single_table_criterion <EOL> if single_crit is not None : <EOL> if adapter : <EOL> single_crit = adapter . traverse ( single_crit ) <EOL> single_crit = self . _adapt_clause ( single_crit , False , False ) <EOL> context . whereclause = sql . and_ ( <EOL> sql . True_ . _ifnone ( context . whereclause ) , <EOL> single_crit ) <EOL> def __str__ ( self ) : <EOL> return str ( self . _compile_context ( ) . statement ) <EOL> from . . sql . selectable import ForUpdateArg <EOL> class LockmodeArg ( ForUpdateArg ) : <EOL> @ classmethod <EOL> def parse_legacy_query ( self , mode ) : <EOL> if mode in ( None , False ) : <EOL> return None <EOL> if mode == "<STR_LIT>" : <EOL> read = True <EOL> nowait = False <EOL> elif mode == "<STR_LIT>" : <EOL> read = nowait = False <EOL> elif mode == "<STR_LIT>" : <EOL> nowait = True <EOL> read = False <EOL> else : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" % mode ) <EOL> return LockmodeArg ( read = read , nowait = nowait ) <EOL> class _QueryEntity ( object ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , * args , ** kwargs ) : <EOL> if cls is _QueryEntity : <EOL> entity = args [ <NUM_LIT:1> ] <EOL> if not isinstance ( entity , util . string_types ) and _is_mapped_class ( entity ) : <EOL> cls = _MapperEntity <EOL> elif isinstance ( entity , Bundle ) : <EOL> cls = _BundleEntity <EOL> else : <EOL> cls = _ColumnEntity <EOL> return object . __new__ ( cls ) <EOL> def _clone ( self ) : <EOL> q = self . __class__ . __new__ ( self . __class__ ) <EOL> q . __dict__ = self . __dict__ . copy ( ) <EOL> return q <EOL> class _MapperEntity ( _QueryEntity ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , query , entity ) : <EOL> if not query . _primary_entity : <EOL> query . _primary_entity = self <EOL> query . _entities . append ( self ) <EOL> self . entities = [ entity ] <EOL> self . expr = entity <EOL> supports_single_entity = True <EOL> def setup_entity ( self , ext_info , aliased_adapter ) : <EOL> self . mapper = ext_info . mapper <EOL> self . aliased_adapter = aliased_adapter <EOL> self . selectable = ext_info . selectable <EOL> self . is_aliased_class = ext_info . is_aliased_class <EOL> self . _with_polymorphic = ext_info . with_polymorphic_mappers <EOL> self . _polymorphic_discriminator = ext_info . polymorphic_on <EOL> self . entity_zero = ext_info <EOL> if ext_info . is_aliased_class : <EOL> self . _label_name = self . entity_zero . name <EOL> else : <EOL> self . _label_name = self . mapper . class_ . __name__ <EOL> self . path = self . entity_zero . _path_registry <EOL> self . custom_rows = bool ( self . mapper . dispatch . append_result ) <EOL> def set_with_polymorphic ( self , query , cls_or_mappers , <EOL> selectable , polymorphic_on ) : <EOL> """<STR_LIT>""" <EOL> if self . is_aliased_class : <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> if cls_or_mappers is None : <EOL> query . _reset_polymorphic_adapter ( self . mapper ) <EOL> return <EOL> mappers , from_obj = self . mapper . _with_polymorphic_args ( <EOL> cls_or_mappers , selectable ) <EOL> self . _with_polymorphic = mappers <EOL> self . _polymorphic_discriminator = polymorphic_on <EOL> self . selectable = from_obj <EOL> query . _mapper_loads_polymorphically_with ( <EOL> self . mapper , sql_util . ColumnAdapter ( <EOL> from_obj , self . mapper . _equivalent_columns ) ) <EOL> filter_fn = id <EOL> @ property <EOL> def type ( self ) : <EOL> return self . mapper . class_ <EOL> @ property <EOL> def entity_zero_or_selectable ( self ) : <EOL> return self . entity_zero <EOL> def corresponds_to ( self , entity ) : <EOL> if entity . is_aliased_class : <EOL> if self . is_aliased_class : <EOL> if entity . _base_alias is self . entity_zero . _base_alias : <EOL> return True <EOL> return False <EOL> elif self . is_aliased_class : <EOL> if self . entity_zero . _use_mapper_path : <EOL> return entity in self . _with_polymorphic <EOL> else : <EOL> return entity is self . entity_zero <EOL> return entity . common_parent ( self . entity_zero ) <EOL> def adapt_to_selectable ( self , query , sel ) : <EOL> query . _entities . append ( self ) <EOL> def _get_entity_clauses ( self , query , context ) : <EOL> adapter = None <EOL> if not self . is_aliased_class : <EOL> if query . _polymorphic_adapters : <EOL> adapter = query . _polymorphic_adapters . get ( self . mapper , None ) <EOL> else : <EOL> adapter = self . aliased_adapter <EOL> if adapter : <EOL> if query . _from_obj_alias : <EOL> ret = adapter . wrap ( query . _from_obj_alias ) <EOL> else : <EOL> ret = adapter <EOL> else : <EOL> ret = query . _from_obj_alias <EOL> return ret <EOL> def row_processor ( self , query , context , custom_rows ) : <EOL> adapter = self . _get_entity_clauses ( query , context ) <EOL> if context . adapter and adapter : <EOL> adapter = adapter . wrap ( context . adapter ) <EOL> elif not adapter : <EOL> adapter = context . adapter <EOL> if not adapter and self . mapper . _requires_row_aliasing : <EOL> adapter = sql_util . ColumnAdapter ( <EOL> self . selectable , <EOL> self . mapper . _equivalent_columns ) <EOL> if query . _primary_entity is self : <EOL> _instance = loading . instance_processor ( <EOL> self . mapper , <EOL> context , <EOL> self . path , <EOL> adapter , <EOL> only_load_props = query . _only_load_props , <EOL> refresh_state = context . refresh_state , <EOL> polymorphic_discriminator = self . _polymorphic_discriminator <EOL> ) <EOL> else : <EOL> _instance = loading . instance_processor ( <EOL> self . mapper , <EOL> context , <EOL> self . path , <EOL> adapter , <EOL> polymorphic_discriminator = self . _polymorphic_discriminator <EOL> ) <EOL> return _instance , self . _label_name <EOL> def setup_context ( self , query , context ) : <EOL> adapter = self . _get_entity_clauses ( query , context ) <EOL> context . froms += ( self . selectable , ) <EOL> if context . order_by is False and self . mapper . order_by : <EOL> context . order_by = self . mapper . order_by <EOL> if adapter : <EOL> context . order_by = adapter . adapt_list ( <EOL> util . to_list ( <EOL> context . order_by <EOL> ) <EOL> ) <EOL> if self . _with_polymorphic : <EOL> poly_properties = self . mapper . _iterate_polymorphic_properties ( <EOL> self . _with_polymorphic ) <EOL> else : <EOL> poly_properties = self . mapper . _polymorphic_properties <EOL> for value in poly_properties : <EOL> if query . _only_load_props and value . key not in query . _only_load_props : <EOL> continue <EOL> value . setup ( <EOL> context , <EOL> self , <EOL> self . path , <EOL> adapter , <EOL> only_load_props = query . _only_load_props , <EOL> column_collection = context . primary_columns <EOL> ) <EOL> if self . _polymorphic_discriminator is not None and self . _polymorphic_discriminator is not self . mapper . polymorphic_on : <EOL> if adapter : <EOL> pd = adapter . columns [ self . _polymorphic_discriminator ] <EOL> else : <EOL> pd = self . _polymorphic_discriminator <EOL> context . primary_columns . append ( pd ) <EOL> def __str__ ( self ) : <EOL> return str ( self . mapper ) <EOL> @ inspection . _self_inspects <EOL> class Bundle ( object ) : <EOL> """<STR_LIT>""" <EOL> single_entity = False <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , * exprs , ** kw ) : <EOL> """<STR_LIT>""" <EOL> self . name = self . _label = name <EOL> self . exprs = exprs <EOL> self . c = self . columns = ColumnCollection ( ) <EOL> self . columns . update ( ( getattr ( col , "<STR_LIT:key>" , col . _label ) , col ) <EOL> for col in exprs ) <EOL> self . single_entity = kw . pop ( '<STR_LIT>' , self . single_entity ) <EOL> columns = None <EOL> """<STR_LIT>""" <EOL> c = None <EOL> """<STR_LIT>""" <EOL> def _clone ( self ) : <EOL> cloned = self . __class__ . __new__ ( self . __class__ ) <EOL> cloned . __dict__ . update ( self . __dict__ ) <EOL> return cloned <EOL> def __clause_element__ ( self ) : <EOL> return expression . ClauseList ( group = False , * self . c ) <EOL> @ property <EOL> def clauses ( self ) : <EOL> return self . __clause_element__ ( ) . clauses <EOL> def label ( self , name ) : <EOL> """<STR_LIT>""" <EOL> cloned = self . _clone ( ) <EOL> cloned . name = name <EOL> return cloned <EOL> def create_row_processor ( self , query , procs , labels ) : <EOL> """<STR_LIT>""" <EOL> def proc ( row , result ) : <EOL> return util . KeyedTuple ( <EOL> [ proc ( row , None ) for proc in procs ] , labels ) <EOL> return proc <EOL> class _BundleEntity ( _QueryEntity ) : <EOL> def __init__ ( self , query , bundle , setup_entities = True ) : <EOL> query . _entities . append ( self ) <EOL> self . bundle = self . expr = bundle <EOL> self . type = type ( bundle ) <EOL> self . _label_name = bundle . name <EOL> self . _entities = [ ] <EOL> if setup_entities : <EOL> for expr in bundle . exprs : <EOL> if isinstance ( expr , Bundle ) : <EOL> _BundleEntity ( self , expr ) <EOL> else : <EOL> _ColumnEntity ( self , expr , namespace = self ) <EOL> self . entities = ( ) <EOL> self . filter_fn = lambda item : item <EOL> self . supports_single_entity = self . bundle . single_entity <EOL> custom_rows = False <EOL> @ property <EOL> def entity_zero ( self ) : <EOL> for ent in self . _entities : <EOL> ezero = ent . entity_zero <EOL> if ezero is not None : <EOL> return ezero <EOL> else : <EOL> return None <EOL> def corresponds_to ( self , entity ) : <EOL> return False <EOL> @ property <EOL> def entity_zero_or_selectable ( self ) : <EOL> for ent in self . _entities : <EOL> ezero = ent . entity_zero_or_selectable <EOL> if ezero is not None : <EOL> return ezero <EOL> else : <EOL> return None <EOL> def adapt_to_selectable ( self , query , sel ) : <EOL> c = _BundleEntity ( query , self . bundle , setup_entities = False ) <EOL> for ent in self . _entities : <EOL> ent . adapt_to_selectable ( c , sel ) <EOL> def setup_entity ( self , ext_info , aliased_adapter ) : <EOL> for ent in self . _entities : <EOL> ent . setup_entity ( ext_info , aliased_adapter ) <EOL> def setup_context ( self , query , context ) : <EOL> for ent in self . _entities : <EOL> ent . setup_context ( query , context ) <EOL> def row_processor ( self , query , context , custom_rows ) : <EOL> procs , labels = zip ( <EOL> * [ ent . row_processor ( query , context , custom_rows ) <EOL> for ent in self . _entities ] <EOL> ) <EOL> proc = self . bundle . create_row_processor ( query , procs , labels ) <EOL> return proc , self . _label_name <EOL> class _ColumnEntity ( _QueryEntity ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , query , column , namespace = None ) : <EOL> self . expr = column <EOL> self . namespace = namespace <EOL> if isinstance ( column , util . string_types ) : <EOL> column = sql . literal_column ( column ) <EOL> self . _label_name = column . name <EOL> elif isinstance ( column , ( <EOL> attributes . QueryableAttribute , <EOL> interfaces . PropComparator <EOL> ) ) : <EOL> self . _label_name = column . key <EOL> column = column . _query_clause_element ( ) <EOL> else : <EOL> self . _label_name = getattr ( column , '<STR_LIT:key>' , None ) <EOL> if not isinstance ( column , expression . ColumnElement ) and hasattr ( column , '<STR_LIT>' ) : <EOL> for c in column . _select_iterable : <EOL> if c is column : <EOL> break <EOL> _ColumnEntity ( query , c , namespace = column ) <EOL> else : <EOL> return <EOL> elif isinstance ( column , Bundle ) : <EOL> _BundleEntity ( query , column ) <EOL> return <EOL> if not isinstance ( column , sql . ColumnElement ) : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( column , ) <EOL> ) <EOL> self . type = type_ = column . type <EOL> if type_ . hashable : <EOL> self . filter_fn = lambda item : item <EOL> else : <EOL> counter = util . counter ( ) <EOL> self . filter_fn = lambda item : counter ( ) <EOL> if not column . _label and not getattr ( column , '<STR_LIT>' , False ) : <EOL> column = column . label ( self . _label_name ) <EOL> query . _entities . append ( self ) <EOL> self . column = column <EOL> self . froms = set ( ) <EOL> self . actual_froms = actual_froms = set ( column . _from_objects ) <EOL> self . entities = util . OrderedSet ( <EOL> elem . _annotations [ '<STR_LIT>' ] <EOL> for elem in visitors . iterate ( column , { } ) <EOL> if '<STR_LIT>' in elem . _annotations <EOL> and actual_froms . intersection ( elem . _from_objects ) <EOL> ) <EOL> if self . entities : <EOL> self . entity_zero = list ( self . entities ) [ <NUM_LIT:0> ] <EOL> elif self . namespace is not None : <EOL> self . entity_zero = self . namespace <EOL> else : <EOL> self . entity_zero = None <EOL> supports_single_entity = False <EOL> custom_rows = False <EOL> @ property <EOL> def entity_zero_or_selectable ( self ) : <EOL> if self . entity_zero is not None : <EOL> return self . entity_zero <EOL> elif self . actual_froms : <EOL> return list ( self . actual_froms ) [ <NUM_LIT:0> ] <EOL> else : <EOL> return None <EOL> def adapt_to_selectable ( self , query , sel ) : <EOL> c = _ColumnEntity ( query , sel . corresponding_column ( self . column ) ) <EOL> c . _label_name = self . _label_name <EOL> c . entity_zero = self . entity_zero <EOL> c . entities = self . entities <EOL> def setup_entity ( self , ext_info , aliased_adapter ) : <EOL> if '<STR_LIT>' not in self . __dict__ : <EOL> self . selectable = ext_info . selectable <EOL> self . froms . add ( ext_info . selectable ) <EOL> def corresponds_to ( self , entity ) : <EOL> if self . entity_zero is None : <EOL> return False <EOL> elif _is_aliased_class ( entity ) : <EOL> return entity is self . entity_zero <EOL> else : <EOL> return not _is_aliased_class ( self . entity_zero ) and entity . common_parent ( self . entity_zero ) <EOL> def _resolve_expr_against_query_aliases ( self , query , expr , context ) : <EOL> return query . _adapt_clause ( expr , False , True ) <EOL> def row_processor ( self , query , context , custom_rows ) : <EOL> column = self . _resolve_expr_against_query_aliases ( <EOL> query , self . column , context ) <EOL> if context . adapter : <EOL> column = context . adapter . columns [ column ] <EOL> def proc ( row , result ) : <EOL> return row [ column ] <EOL> return proc , self . _label_name <EOL> def setup_context ( self , query , context ) : <EOL> column = self . _resolve_expr_against_query_aliases ( <EOL> query , self . column , context ) <EOL> context . froms += tuple ( self . froms ) <EOL> context . primary_columns . append ( column ) <EOL> def __str__ ( self ) : <EOL> return str ( self . column ) <EOL> class QueryContext ( object ) : <EOL> multi_row_eager_loaders = False <EOL> adapter = None <EOL> froms = ( ) <EOL> for_update = None <EOL> def __init__ ( self , query ) : <EOL> if query . _statement is not None : <EOL> if isinstance ( query . _statement , expression . SelectBase ) and not query . _statement . _textual and not query . _statement . use_labels : <EOL> self . statement = query . _statement . apply_labels ( ) <EOL> else : <EOL> self . statement = query . _statement <EOL> else : <EOL> self . statement = None <EOL> self . from_clause = query . _from_obj <EOL> self . whereclause = query . _criterion <EOL> self . order_by = query . _order_by <EOL> self . query = query <EOL> self . session = query . session <EOL> self . populate_existing = query . _populate_existing <EOL> self . invoke_all_eagers = query . _invoke_all_eagers <EOL> self . version_check = query . _version_check <EOL> self . refresh_state = query . _refresh_state <EOL> self . primary_columns = [ ] <EOL> self . secondary_columns = [ ] <EOL> self . eager_order_by = [ ] <EOL> self . eager_joins = { } <EOL> self . create_eager_joins = [ ] <EOL> self . propagate_options = set ( o for o in query . _with_options if <EOL> o . propagate_to_loaders ) <EOL> self . attributes = query . _attributes . copy ( ) <EOL> class AliasOption ( interfaces . MapperOption ) : <EOL> def __init__ ( self , alias ) : <EOL> """<STR_LIT>""" <EOL> self . alias = alias <EOL> def process_query ( self , query ) : <EOL> if isinstance ( self . alias , util . string_types ) : <EOL> alias = query . _mapper_zero ( ) . mapped_table . alias ( self . alias ) <EOL> else : <EOL> alias = self . alias <EOL> query . _from_obj_alias = sql_util . ColumnAdapter ( alias ) </s>
