U aي@sdZddlmZddlmZddlmZddlmZddlmZddlm Z dd l m Z dd l m Z dd l m Z dd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddl!m"Z"ddl!m#Z#ddl!m$Z$ddl!m%Z%ddl!m&Z&dd l!m'Z'dd!l!m(Z(dd"l)m*Z*d#d$lm+Z+d#d%lm,Z,e,j-.e%j/d&Z0d'd(Z1d)d*Z2d+d,Z3d-d.Z4dad0d1Z5d2d3Z6d4d5Z7d6d7Z8d8d9Z9d:d;Z:dd?ZdDdEZ?dFdGZ@GdHdIdIeAZBGdJdKdKeBZCGdLdMdMeBZDdNdOZEdbdQdRZFdSdTZGdcdUdVZHGdWdXdXe jIZJGdYdZdZeJZKddd[d\ZLd]d^ZMd_d`ZNdPS)ez;High level utilities which build upon other modules here. )deque)chain) coercions) operators)roles)visitors)_deep_annotate)_deep_deannotate)_shallow_annotate)_expand_cloned) _from_objects) ColumnSet) sort_tables) _find_columns)_label_reference)_textual_label_reference) BindParameter) ColumnClause) ColumnElement)Grouping)Label)Null)UnaryExpression)Column)Alias) FromClause) FromGrouping)Join) ScalarSelect) SelectBase) TableClause) HasCacheKey)exc)utilz.sql.util.join_conditioncCsDtt|}g}t|D]&\}}|D]}||r$||q$q|S)aGiven a list of FROM clauses and a selectable, return the first index and element from the list of clauses which can be joined against the selectable. returns None, None if no match is found. e.g.:: clause1 = table1.join(table2) clause2 = table4.join(table5) join_to = table2.join(table3) find_join_source([clause1, clause2], join_to) == clause1 )listr enumerateis_derived_fromappend)clausesjoin_to selectablesidxifsr1TC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-nyjtotrf\sqlalchemy\sql\util.pyfind_join_source6s  r3c Cstt|}g}t|D]*\}}|D]}||r$||qq$qt|dkrg}|D]:}||}|D](}tt|t|rh||qXqhqX|r|S|S)zGiven a list of FROM clauses and a selectable, return the indexes from the list of clauses which is derived from the selectable. r) r&r r'r(r)lensetsurface_selectables intersection) r*Z join_fromr,Z liberal_idxr.r/r0Zconservative_idxr-r1r1r2#find_left_clause_that_matches_givenPs(      r8c sg}tt|}tdkr2|dk r2d}t|}nd}d}tD]h\}}||gD]R} |rt|j| j|r| |qBqVt || s|dk rV| |qBqVqBt|dkrtt ddDfdd|D}|s|dk rt tS|SdS)a Given a list of FROM clauses, a selectable, and optional ON clause, return a list of integer indexes from the clauses list indicating the clauses that can be joined from. The presence of an "onclause" indicates that at least one clause can definitely be joined from; if the list of clauses is of length one and the onclause is given, returns that index. If the list of clauses is more than length one, and the onclause is given, attempts to locate which clauses contain the same columns. rNTFcSsg|]}t|jqSr1)r Z _hide_froms).0r/r1r1r2 sz1find_left_clause_to_join_from..csg|]}|kr|qSr1r1)r9r.r*Ztoremover1r2r:s )r5r r4rr' differencecunion issupersetr)rZ _can_joinrrange) r*r+onclauser-r,Zresolve_ambiguityZcols_in_onclauser.r/r0r1r;r2find_left_clause_to_join_fromzs0        rBcs(gfddt|ddS)aProduce a traversal of the given expression, delivering column comparisons to the given function. The function is of the form:: def my_fn(binary, left, right) For each binary expression located which has a comparison operator, the product of "left" and "right" will be delivered to that function, in terms of that binary. Hence an expression like:: and_( (a + b) == q + func.sum(e + f), j == r ) would have the traversal:: a q a e a f b q b e b f j r That is, every combination of "left" and "right" that doesn't further contain a binary comparison is passed as pairs. c3st|tr|Vn|jdkrt|jrd||jD]$}|jD]}d||qLq> d| D] }|qvn2t|t r|V| D]}|D] }|VqqdS)Nbinaryr) isinstancerZ__visit_name__rZ is_comparisonoperatorinsertleftrightpop get_childrenr)elementlrelemefnstackvisitr1r2rSs"        z#visit_binary_product..visitN)r&)rQexprr1rPr2visit_binary_products# rUFcsgi}|rj|d<|d<|r,j|d<|rRj|d<|d<|d<|d<|rvfdd |d <|d <|d <|rfd d}||d<j|d<t|i|S)z1locate Table objects within the given expression.selectZcompound_selectjoinaliasZsubqueryZ tablesampleZlateralcs |jSNr)table)entZtablesr1r2zfind_tables..rFupdatedeletecs|jdSrYrZ)columnr]r1r2 visit_column sz!find_tables..visit_columnrbr[)r)rtraverse)clauseZ check_columnsZinclude_aliasesZ include_joinsZinclude_selectsZ include_crudZ _visitorsrcr1r]r2 find_tabless.     rfcCst}g}t|g}|r|}t|trt|trBt|j st|t r|t|j t s||j }t|t rn|j }||qnt|tr|j }||qt|trq||kr||||q|D]}||qq|S)zqBreak up an 'order by' expression into individual column-expressions, without DESC/ASC/NULLS FIRST/NULLS LAST)r% column_setrpopleftrDrrrZis_ordering_modifiermodifierrrKrrr)rraddrJ)recolsresultrRtr=r1r1r2unwrap_order_bys>            rncCsdd}t|i|S)NcSst|ttfr|jSdSrY)rDrrrK)rNr1r1r2replaceGsz'unwrap_label_reference..replace)rZreplacement_traverse)rKror1r1r2unwrap_label_referenceFsrpcs:tdd|Dttdd|D}fdd|DS)zGiven the columns clause and ORDER BY of a selectable, return a list of column expressions that can be added to the collist corresponding to the ORDER BY, without repeating those already in the collist. cSs g|]}|jdk r|jn|qSrY)Z_order_by_label_elementrKr9colr1r1r2r:Vsz4expand_column_list_from_order_by..cSsg|] }t|qSr1)rn)r9or1r1r2r:\scsg|]}|kr|qSr1r1rqZcols_already_presentr1r2r:^s)r5r&r)ZcollistZorder_byZ to_look_forr1rtr2 expand_column_list_from_order_byNsrucCs t|D]}||krdSqdS)zGiven a target clause and a second to search within, return True if the target is plainly present in the search without any subqueries or aliases involved. Basically descends through Joins. TFN)r6)researchrNr1r1r2clause_is_presentas rwccsdt|tr8t|jD] }|Vqt|jD] }|Vq*n(t|trZt|jD] }|VqLn|VdSrY)rDrtables_from_leftmostrGrHrrK)rermr1r1r2rxqs    rxccsR|g}|rN|}|Vt|tr6||j|jfqt|tr||jqdSrY) rIrDrextendrGrHrr)rKrerRrNr1r1r2r6~s  r6ccs|g}|r|}t|ttfr&|Vt|trD||j|jfqt|tr\| |j qt|t r|j dk r~| |j q|Vq|dk r|VqdSrY) rIrDr!rrryrGrHrr)rKrr[rzr1r1r2surface_selectables_onlys    r{cCs\ttf}t|g}|rX|}||jkr2|j|S|D]}t||rJq:||q:qdSrY)rr rrh _annotationsrJrDr))rbZannotation_nameZfilter_rRrNsubr1r1r2extract_first_column_annotations     r~cCsttt|t|S)z:Return True if left/right have some overlapping selectable)boolr5r6r7)rGrHr1r1r2selectables_overlapsrcs&gfdd}t|id|iS)zReturn an ordered list of "bound" values in the given clause. E.g.:: >>> expr = and_( ... table.c.foo==5, table.c.foo==7 ... ) >>> bind_values(expr) [5, 7] cs|jdSrY)r)Zeffective_value)bindvr1r2visit_bindparamsz$bind_values..visit_bindparamZ bindparam)rrd)rerr1rr2 bind_valuess  rcCs,t|tjr |dd}d|St|SdS)N'z''z'%s')rDr% string_typesrorepr)rKr1r1r2_quote_ddl_exprs  rc@s$eZdZdZdZdZdZddZdS) _repr_baserrr#) max_charscCsPt|}t|}||jkrL|jd}|d|d||j|| d}|S)Nr#rz# ... (%d characters truncated) ... )rr4r)selfvaluerepZlenrepZsegment_lengthr1r1r2truncs    z_repr_base.truncN)__name__ __module__ __qualname___LIST_TUPLE_DICT __slots__rr1r1r1r2rs rc@s&eZdZdZdZd ddZddZdS) _repr_rowzProvide a string view of a row.)row,cCs||_||_dSrY)rr)rrrr1r1r2__init__sz_repr_row.__init__cs:|jddfdd|jDt|jdkr2dndfS)N(%s%s), c3s|]}|VqdSrYr1r9rrr1r2 sz%_repr_row.__repr__..r,)rrWrr4)rr1rr2__repr__s z_repr_row.__repr__N)r)rrr__doc__rrrr1r1r1r2rs rc@s6eZdZdZdZd ddZddZd d Zd d ZdS) _repr_paramszProvide a string view of bound parameters. Truncates display to a given number of 'multi' parameter sets, as well as long values to a given number of characters. )paramsbatchesismultirNcCs||_||_||_||_dSrY)rrrr)rrrrrr1r1r2rsz_repr_params.__init__c Cs|jdkr||jSt|jtr*|j}n4t|jtr>|j}n t|jtrR|j }n ||jS|jrt |j|j krd}d | |jd|j d|dd||j t |jf| |jdd|ddfS|jr| |j|S||j|SdS)Nz8 ... displaying %i of %i total bound parameter sets ...  r#rr)rrrrDr&rtuplerdictrr4rrW _repr_multir)rtypmsgr1r1r2rs.       z_repr_params.__repr__cs|rzt|dtrjnDt|dtr0jn.t|dtrFjnds^tdt|dd fdd|D}nd}|jkrd|Sd |SdS) NrFzUnknown parameter type %src3s|]}|VqdSrY)r)r9rZ elem_typerr1r2r3sz+_repr_params._repr_multi..r[%s]z(%s)) rDr&rrrrrAssertionErrortyperW)rZ multi_paramsrelementsr1rr2r&s"   z_repr_params._repr_multics|j||jkr0ddfdd|DS||jkrjddfdd|Dt|dkrbdnd fSd dfd d|DSdS) Nz{%s}rc3s"|]\}}d||fVqdS)z%r: %sNr1)r9keyrrr1r2rBsz,_repr_params._repr_params..rc3s|]}|VqdSrYr1rrr1r2rIsrrrrc3s|]}|VqdSrYr1rrr1r2rMs)rrrWitemsrr4)rrrr1rr2r>s  z_repr_params._repr_params)rN) rrrrrrrrrr1r1r1r2rs  rcsfdd}t|id|iS)zWgiven criterion containing bind params, convert selected elements to IS NULL. csnt|jtr:|jjkr:|j|_t|_tj|_tj |_ n0t|jtrj|jjkrjt|_tj|_tj |_ dSrY) rDrGrZ_identifying_keyrHrris_rEis_notnegaterCnullsr1r2 visit_binaryVs     z-adapt_criterion_to_null..visit_binaryrC)rZcloned_traverse)critrrr1rr2adapt_criterion_to_nullPs rNcCs|dkr |S|dfg}t|}d}|r|\}}t|trl||k rl|}||j|_||j|fn ||}|dk r||_|dkr"|}q"|SrY) ClauseAdapterrIrDrZ_clonerdrAr)rG)rGrHstop_onrRadapterretZ prevrightr1r1r2 splice_joinsks"   rc s |dd}|ddttD]}tdd|jDD]}D]}||kr\qNz |j}WnBtjk r|rYqNnYn"tj k r|rYqNnYnX| |rNr|j |j krN |qFqNqFq.|rfdd}|D]} | dk rt | id |iqtS) aCgiven a list of columns, return a 'reduced' set based on natural equivalents. the set is reduced to the smallest list of columns which have no natural equivalent present in the list. A "natural equivalent" means that two columns will ultimately represent the same value because they are related by a foreign key. \*clauses is an optional list of join clauses which will be traversed to further identify columns that are "equivalent". \**kw may specify 'ignore_nonexistent_tables' to ignore foreign keys whose tables are not yet configured, or columns that aren't yet present. This function is primarily used to determine the most minimal "primary key" from a selectable, by reducing the set of primary key columns present in the selectable to just those that are not repeated. ignore_nonexistent_tablesF only_synonymscSsg|] }|jqSr1)Z foreign_keysr9r=r1r1r2r:sz"reduce_columns..cs||jtjkrxttddD}|j|krx|j|krxt D]0}| |jrFrh|j |jj krF |qxqFdS)NcSsg|] }|jqSr1) proxy_setrr1r1r2r:sz8reduce_columns..visit_binary..) rEreqr%rgrr<rGrHreversedshares_lineagenamerj)rCrkr=columnsZomitrr1r2rs     z$reduce_columns..visit_binaryNrC)rIr%Zordered_column_setrgrrrbr$ZNoReferencedColumnErrorZNoReferencedTableErrorrrrjrrdrr<) rr*kwrrrZfkr=Zfk_colrrer1rr2reduce_columnss@         rcsHrrtdddfdd}gt|id|iS)z9traverse an expression and locate binary criterion pairs.zSCan only specify one of 'consider_as_foreign_keys' or 'consider_as_referenced_keys'cSs ||SrY)compare)abr1r1r2col_issz"criterion_as_pairs..col_iscs~s|jtjk rdSt|jtr,t|jts0dSr|jkrj|j|jsV|jkrj|j|jfn4|jkr|j|js|jkr|j|jfnڈr|jkr܈|j|js|jkr܈|j|jfn:|jkrz|j|js|jkrz|j|jfnbt|jtrzt|jtrz|j |jrX|j|jfn"|j |jrz|j|jfdSrY) rErrrDrGrrHr)rZ referencesr any_operatorrconsider_as_foreign_keysconsider_as_referenced_keyspairsr1r2rsR        z(criterion_as_pairs..visit_binaryrC)r$ ArgumentErrorrrd)Z expressionrrrrr1rr2criterion_as_pairss'rc@s:eZdZdZd ddZejfddZedd d Z dS) ra5Clones and modifies clauses based on column correspondence. E.g.:: table1 = Table('sometable', metadata, Column('col1', Integer), Column('col2', Integer) ) table2 = Table('someothertable', metadata, Column('col1', Integer), Column('col2', Integer) ) condition = table1.c.col1 == table2.c.col1 make an alias of table1:: s = table1.alias('foo') calling ``ClauseAdapter(s).traverse(condition)`` converts condition to read:: s.c.col1 == table2.c.col1 NFcCs@|g|d|_||_||_||_t|p*i|_||_||_dS)N)ranonymize_labels) Z__traverse_options__ selectable include_fn exclude_fnr%Z column_dict equivalentsadapt_on_namesadapt_from_selectables)rrrrrrrrr1r1r2r(s zClauseAdapter.__init__cCs|jj||d}|dkrb||jkrb||krb|j|D],}|j||||gd}|dk r4|Sq4|jr|dkr|jj|j}|S)N)require_embedded)r_seen) rcorresponding_columnr_corresponding_columnr>rZexported_columnsgetr)rrrrrnewcolequivr1r1r2r=s   z#ClauseAdapter._corresponding_columnzsqlalchemy.sql.functionscCstjj}t|trtt||jst|j|rT|jrN|jD]}||r6qNq6dS|jSt|t rnt|j t rn|SdSnt|t sdSd|j kr|j d}|jr||jkr|jD]}|j|ddk rqqdS|jr||sdS|jr||rdS||dSdS)NZ adapt_columnFT)r%Z preloadedZ sql_functionsrDrZFunctionElementrr(rrrKr!rr|rr=rrrr)rrrZ functionsZadpr1r1r2roQs@          zClauseAdapter.replace)NNNFFN) rrrrrr% EMPTY_SETrZpreload_moduleror1r1r1r2r s  rc@sdeZdZdZdddZGdddeZd d Zd d ZeZ e j Z d dZ ddZddZddZdS) ColumnAdapteraExtends ClauseAdapter with extra utility functions. Key aspects of ColumnAdapter include: * Expressions that are adapted are stored in a persistent .columns collection; so that an expression E adapted into an expression E1, will return the same object E1 when adapted a second time. This is important in particular for things like Label objects that are anonymized, so that the ColumnAdapter can be used to present a consistent "adapted" view of things. * Exclusion of items from the persistent collection based on include/exclude rules, but also independent of hash identity. This because "annotated" items all have the same hash identity as their parent. * "wrapping" capability is added, so that the replacement of an expression E can proceed through a series of adapters. This differs from the visitor's "chaining" feature in that the resulting object is passed through all replacing functions unconditionally, rather than stopping at the first one that returns non-None. * An adapt_required option, used by eager loading to indicate that We don't trust a result row column that is not translated. This is to prevent a column from being interpreted as that of the child row in a self-referential scenario, see inheritance/test_basic.py->EagerTargetingTest.test_adapt_stringency NFTc CsZtj|||||||| dt|j|_|js4|jrD|||j|_||_ ||_ d|_ dS)N)rrrrr) rrr%WeakPopulateDict _locate_colrrr_IncludeExcludeMappingadapt_requiredallow_label_resolve_wrap) rrrrrrrrrrr1r1r2rs   zColumnAdapter.__init__c@seZdZddZddZdS)z$ColumnAdapter._IncludeExcludeMappingcCs||_||_dSrY)parentr)rrrr1r1r2rsz-ColumnAdapter._IncludeExcludeMapping.__init__cCsL|jjr|j|r(|jjrB|j|rB|jjr>|jjj|S|S|j|SrY)rrrrr)rrr1r1r2 __getitem__s  z0ColumnAdapter._IncludeExcludeMapping.__getitem__N)rrrrrr1r1r1r2rsrcCsP|j|j}|j|j||_t|j|_|j s<|j rL| ||j|_|SrY) __class____new____dict__r`rr%rrrrrr)rracr1r1r2wraps zColumnAdapter.wrapcCs |j|SrY)r)robjr1r1r2rdszColumnAdapter.traversecCs*|j|}||kr&||ddkr&dS|S)NT)rr)rrrrr1r1r2adapt_check_presents z!ColumnAdapter.adapt_check_presentcCsHt||}|jr*|j|}|dk r*|}|jr<||krs                                  *4A */  X M ?z~