<s> """<STR_LIT>""" <EOL> from . . import exc , util <EOL> from . base import _from_objects , ColumnSet <EOL> from . import operators , visitors <EOL> from itertools import chain <EOL> from collections import deque <EOL> from . elements import BindParameter , ColumnClause , ColumnElement , Null , UnaryExpression , literal_column , Label <EOL> from . selectable import ScalarSelect , Join , FromClause , FromGrouping <EOL> from . schema import Column <EOL> join_condition = util . langhelpers . public_factory ( <EOL> Join . _join_condition , <EOL> "<STR_LIT>" ) <EOL> from . annotation import _shallow_annotate , _deep_annotate , _deep_deannotate <EOL> from . elements import _find_columns <EOL> from . ddl import sort_tables <EOL> def find_join_source ( clauses , join_to ) : <EOL> """<STR_LIT>""" <EOL> selectables = list ( _from_objects ( join_to ) ) <EOL> for i , f in enumerate ( clauses ) : <EOL> for s in selectables : <EOL> if f . is_derived_from ( s ) : <EOL> return i , f <EOL> else : <EOL> return None , None <EOL> def visit_binary_product ( fn , expr ) : <EOL> """<STR_LIT>""" <EOL> stack = [ ] <EOL> def visit ( element ) : <EOL> if isinstance ( element , ScalarSelect ) : <EOL> yield element <EOL> elif element . __visit_name__ == '<STR_LIT>' and operators . is_comparison ( element . operator ) : <EOL> stack . insert ( <NUM_LIT:0> , element ) <EOL> for l in visit ( element . left ) : <EOL> for r in visit ( element . right ) : <EOL> fn ( stack [ <NUM_LIT:0> ] , l , r ) <EOL> stack . pop ( <NUM_LIT:0> ) <EOL> for elem in element . get_children ( ) : <EOL> visit ( elem ) <EOL> else : <EOL> if isinstance ( element , ColumnClause ) : <EOL> yield element <EOL> for elem in element . get_children ( ) : <EOL> for e in visit ( elem ) : <EOL> yield e <EOL> list ( visit ( expr ) ) <EOL> def find_tables ( clause , check_columns = False , <EOL> include_aliases = False , include_joins = False , <EOL> include_selects = False , include_crud = False ) : <EOL> """<STR_LIT>""" <EOL> tables = [ ] <EOL> _visitors = { } <EOL> if include_selects : <EOL> _visitors [ '<STR_LIT>' ] = _visitors [ '<STR_LIT>' ] = tables . append <EOL> if include_joins : <EOL> _visitors [ '<STR_LIT>' ] = tables . append <EOL> if include_aliases : <EOL> _visitors [ '<STR_LIT>' ] = tables . append <EOL> if include_crud : <EOL> _visitors [ '<STR_LIT>' ] = _visitors [ '<STR_LIT>' ] = _visitors [ '<STR_LIT>' ] = lambda ent : tables . append ( ent . table ) <EOL> if check_columns : <EOL> def visit_column ( column ) : <EOL> tables . append ( column . table ) <EOL> _visitors [ '<STR_LIT>' ] = visit_column <EOL> _visitors [ '<STR_LIT>' ] = tables . append <EOL> visitors . traverse ( clause , { '<STR_LIT>' : False } , _visitors ) <EOL> return tables <EOL> def unwrap_order_by ( clause ) : <EOL> """<STR_LIT>""" <EOL> cols = util . column_set ( ) <EOL> stack = deque ( [ clause ] ) <EOL> while stack : <EOL> t = stack . popleft ( ) <EOL> if isinstance ( t , ColumnElement ) and ( <EOL> not isinstance ( t , UnaryExpression ) or <EOL> not operators . is_ordering_modifier ( t . modifier ) <EOL> ) : <EOL> cols . add ( t ) <EOL> else : <EOL> for c in t . get_children ( ) : <EOL> stack . append ( c ) <EOL> return cols <EOL> def clause_is_present ( clause , search ) : <EOL> """<STR_LIT>""" <EOL> for elem in surface_selectables ( search ) : <EOL> if clause == elem : <EOL> return True <EOL> else : <EOL> return False <EOL> def surface_selectables ( clause ) : <EOL> stack = [ clause ] <EOL> while stack : <EOL> elem = stack . pop ( ) <EOL> yield elem <EOL> if isinstance ( elem , Join ) : <EOL> stack . extend ( ( elem . left , elem . right ) ) <EOL> elif isinstance ( elem , FromGrouping ) : <EOL> stack . append ( elem . element ) <EOL> def selectables_overlap ( left , right ) : <EOL> """<STR_LIT>""" <EOL> return bool ( <EOL> set ( surface_selectables ( left ) ) . intersection ( <EOL> surface_selectables ( right ) <EOL> ) <EOL> ) <EOL> def bind_values ( clause ) : <EOL> """<STR_LIT>""" <EOL> v = [ ] <EOL> def visit_bindparam ( bind ) : <EOL> v . append ( bind . effective_value ) <EOL> visitors . traverse ( clause , { } , { '<STR_LIT>' : visit_bindparam } ) <EOL> return v <EOL> def _quote_ddl_expr ( element ) : <EOL> if isinstance ( element , util . string_types ) : <EOL> element = element . replace ( "<STR_LIT:'>" , "<STR_LIT>" ) <EOL> return "<STR_LIT>" % element <EOL> else : <EOL> return repr ( element ) <EOL> class _repr_params ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , params , batches ) : <EOL> self . params = params <EOL> self . batches = batches <EOL> def __repr__ ( self ) : <EOL> if isinstance ( self . params , ( list , tuple ) ) and len ( self . params ) > self . batches and isinstance ( self . params [ <NUM_LIT:0> ] , ( list , dict , tuple ) ) : <EOL> msg = "<STR_LIT>" <EOL> return '<STR_LIT:U+0020>' . join ( ( <EOL> repr ( self . params [ : self . batches - <NUM_LIT:2> ] ) [ <NUM_LIT:0> : - <NUM_LIT:1> ] , <EOL> msg % ( self . batches , len ( self . params ) ) , <EOL> repr ( self . params [ - <NUM_LIT:2> : ] ) [ <NUM_LIT:1> : ] <EOL> ) ) <EOL> else : <EOL> return repr ( self . params ) <EOL> def adapt_criterion_to_null ( crit , nulls ) : <EOL> """<STR_LIT>""" <EOL> def visit_binary ( binary ) : <EOL> if isinstance ( binary . left , BindParameter ) and binary . left . _identifying_key in nulls : <EOL> binary . left = binary . right <EOL> binary . right = Null ( ) <EOL> binary . operator = operators . is_ <EOL> binary . negate = operators . isnot <EOL> elif isinstance ( binary . right , BindParameter ) and binary . right . _identifying_key in nulls : <EOL> binary . right = Null ( ) <EOL> binary . operator = operators . is_ <EOL> binary . negate = operators . isnot <EOL> return visitors . cloned_traverse ( crit , { } , { '<STR_LIT>' : visit_binary } ) <EOL> def splice_joins ( left , right , stop_on = None ) : <EOL> if left is None : <EOL> return right <EOL> stack = [ ( right , None ) ] <EOL> adapter = ClauseAdapter ( left ) <EOL> ret = None <EOL> while stack : <EOL> ( right , prevright ) = stack . pop ( ) <EOL> if isinstance ( right , Join ) and right is not stop_on : <EOL> right = right . _clone ( ) <EOL> right . _reset_exported ( ) <EOL> right . onclause = adapter . traverse ( right . onclause ) <EOL> stack . append ( ( right . left , right ) ) <EOL> else : <EOL> right = adapter . traverse ( right ) <EOL> if prevright is not None : <EOL> prevright . left = right <EOL> if ret is None : <EOL> ret = right <EOL> return ret <EOL> def reduce_columns ( columns , * clauses , ** kw ) : <EOL> """<STR_LIT>""" <EOL> ignore_nonexistent_tables = kw . pop ( '<STR_LIT>' , False ) <EOL> only_synonyms = kw . pop ( '<STR_LIT>' , False ) <EOL> columns = util . ordered_column_set ( columns ) <EOL> omit = util . column_set ( ) <EOL> for col in columns : <EOL> for fk in chain ( * [ c . foreign_keys for c in col . proxy_set ] ) : <EOL> for c in columns : <EOL> if c is col : <EOL> continue <EOL> try : <EOL> fk_col = fk . column <EOL> except exc . NoReferencedColumnError : <EOL> if ignore_nonexistent_tables : <EOL> continue <EOL> else : <EOL> raise <EOL> except exc . NoReferencedTableError : <EOL> if ignore_nonexistent_tables : <EOL> continue <EOL> else : <EOL> raise <EOL> if fk_col . shares_lineage ( c ) and ( not only_synonyms or <EOL> c . name == col . name ) : <EOL> omit . add ( col ) <EOL> break <EOL> if clauses : <EOL> def visit_binary ( binary ) : <EOL> if binary . operator == operators . eq : <EOL> cols = util . column_set ( <EOL> chain ( * [ c . proxy_set for c in columns . difference ( omit ) ] ) ) <EOL> if binary . left in cols and binary . right in cols : <EOL> for c in reversed ( columns ) : <EOL> if c . shares_lineage ( binary . right ) and ( not only_synonyms or <EOL> c . name == binary . left . name ) : <EOL> omit . add ( c ) <EOL> break <EOL> for clause in clauses : <EOL> if clause is not None : <EOL> visitors . traverse ( clause , { } , { '<STR_LIT>' : visit_binary } ) <EOL> return ColumnSet ( columns . difference ( omit ) ) <EOL> def criterion_as_pairs ( expression , consider_as_foreign_keys = None , <EOL> consider_as_referenced_keys = None , any_operator = False ) : <EOL> """<STR_LIT>""" <EOL> if consider_as_foreign_keys and consider_as_referenced_keys : <EOL> raise exc . ArgumentError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def col_is ( a , b ) : <EOL> return a . compare ( b ) <EOL> def visit_binary ( binary ) : <EOL> if not any_operator and binary . operator is not operators . eq : <EOL> return <EOL> if not isinstance ( binary . left , ColumnElement ) or not isinstance ( binary . right , ColumnElement ) : <EOL> return <EOL> if consider_as_foreign_keys : <EOL> if binary . left in consider_as_foreign_keys and ( col_is ( binary . right , binary . left ) or <EOL> binary . right not in consider_as_foreign_keys ) : <EOL> pairs . append ( ( binary . right , binary . left ) ) <EOL> elif binary . right in consider_as_foreign_keys and ( col_is ( binary . left , binary . right ) or <EOL> binary . left not in consider_as_foreign_keys ) : <EOL> pairs . append ( ( binary . left , binary . right ) ) <EOL> elif consider_as_referenced_keys : <EOL> if binary . left in consider_as_referenced_keys and ( col_is ( binary . right , binary . left ) or <EOL> binary . right not in consider_as_referenced_keys ) : <EOL> pairs . append ( ( binary . left , binary . right ) ) <EOL> elif binary . right in consider_as_referenced_keys and ( col_is ( binary . left , binary . right ) or <EOL> binary . left not in consider_as_referenced_keys ) : <EOL> pairs . append ( ( binary . right , binary . left ) ) <EOL> else : <EOL> if isinstance ( binary . left , Column ) and isinstance ( binary . right , Column ) : <EOL> if binary . left . references ( binary . right ) : <EOL> pairs . append ( ( binary . right , binary . left ) ) <EOL> elif binary . right . references ( binary . left ) : <EOL> pairs . append ( ( binary . left , binary . right ) ) <EOL> pairs = [ ] <EOL> visitors . traverse ( expression , { } , { '<STR_LIT>' : visit_binary } ) <EOL> return pairs <EOL> class AliasedRow ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , row , map ) : <EOL> if isinstance ( row , AliasedRow ) : <EOL> self . row = row . row <EOL> else : <EOL> self . row = row <EOL> self . map = map <EOL> def __contains__ ( self , key ) : <EOL> return self . map [ key ] in self . row <EOL> def has_key ( self , key ) : <EOL> return key in self <EOL> def __getitem__ ( self , key ) : <EOL> return self . row [ self . map [ key ] ] <EOL> def keys ( self ) : <EOL> return self . row . keys ( ) <EOL> class ClauseAdapter ( visitors . ReplacingCloningVisitor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , selectable , equivalents = None , <EOL> include = None , exclude = None , <EOL> include_fn = None , exclude_fn = None , <EOL> adapt_on_names = False ) : <EOL> self . __traverse_options__ = { '<STR_LIT>' : [ selectable ] } <EOL> self . selectable = selectable <EOL> if include : <EOL> assert not include_fn <EOL> self . include_fn = lambda e : e in include <EOL> else : <EOL> self . include_fn = include_fn <EOL> if exclude : <EOL> assert not exclude_fn <EOL> self . exclude_fn = lambda e : e in exclude <EOL> else : <EOL> self . exclude_fn = exclude_fn <EOL> self . equivalents = util . column_dict ( equivalents or { } ) <EOL> self . adapt_on_names = adapt_on_names <EOL> def _corresponding_column ( self , col , require_embedded , <EOL> _seen = util . EMPTY_SET ) : <EOL> newcol = self . selectable . corresponding_column ( <EOL> col , <EOL> require_embedded = require_embedded ) <EOL> if newcol is None and col in self . equivalents and col not in _seen : <EOL> for equiv in self . equivalents [ col ] : <EOL> newcol = self . _corresponding_column ( <EOL> equiv , require_embedded = require_embedded , <EOL> _seen = _seen . union ( [ col ] ) ) <EOL> if newcol is not None : <EOL> return newcol <EOL> if self . adapt_on_names and newcol is None : <EOL> newcol = self . selectable . c . get ( col . name ) <EOL> return newcol <EOL> magic_flag = False <EOL> def replace ( self , col ) : <EOL> if not self . magic_flag and isinstance ( col , FromClause ) and self . selectable . is_derived_from ( col ) : <EOL> return self . selectable <EOL> elif not isinstance ( col , ColumnElement ) : <EOL> return None <EOL> elif self . include_fn and not self . include_fn ( col ) : <EOL> return None <EOL> elif self . exclude_fn and self . exclude_fn ( col ) : <EOL> return None <EOL> else : <EOL> return self . _corresponding_column ( col , True ) <EOL> class ColumnAdapter ( ClauseAdapter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , selectable , equivalents = None , <EOL> chain_to = None , include = None , <EOL> exclude = None , adapt_required = False ) : <EOL> ClauseAdapter . __init__ ( self , selectable , equivalents , <EOL> include , exclude ) <EOL> if chain_to : <EOL> self . chain ( chain_to ) <EOL> self . columns = util . populate_column_dict ( self . _locate_col ) <EOL> self . adapt_required = adapt_required <EOL> def wrap ( self , adapter ) : <EOL> ac = self . __class__ . __new__ ( self . __class__ ) <EOL> ac . __dict__ = self . __dict__ . copy ( ) <EOL> ac . _locate_col = ac . _wrap ( ac . _locate_col , adapter . _locate_col ) <EOL> ac . adapt_clause = ac . _wrap ( ac . adapt_clause , adapter . adapt_clause ) <EOL> ac . adapt_list = ac . _wrap ( ac . adapt_list , adapter . adapt_list ) <EOL> ac . columns = util . populate_column_dict ( ac . _locate_col ) <EOL> return ac <EOL> adapt_clause = ClauseAdapter . traverse <EOL> adapt_list = ClauseAdapter . copy_and_process <EOL> def _wrap ( self , local , wrapped ) : <EOL> def locate ( col ) : <EOL> col = local ( col ) <EOL> return wrapped ( col ) <EOL> return locate <EOL> def _locate_col ( self , col ) : <EOL> c = self . _corresponding_column ( col , True ) <EOL> if c is None : <EOL> c = self . adapt_clause ( col ) <EOL> if isinstance ( c , Label ) : <EOL> c = c . label ( None ) <EOL> if self . adapt_required and c is col : <EOL> return None <EOL> return c <EOL> def adapted_row ( self , row ) : <EOL> return AliasedRow ( row , self . columns ) <EOL> def __getstate__ ( self ) : <EOL> d = self . __dict__ . copy ( ) <EOL> del d [ '<STR_LIT>' ] <EOL> return d <EOL> def __setstate__ ( self , state ) : <EOL> self . __dict__ . update ( state ) <EOL> self . columns = util . PopulateDict ( self . _locate_col ) </s>
