o znhr@svdZddlmZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd l m Z dd l mZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%dd lm&Z&e&j'(e!j)d!Z*d"d#Z+d$d%Z,d&d'Z-d(d)Z. * * * * *dUd+d,Z/d-d.Z0d/d0Z1d1d2Z2d3d4Z3d5d6Z4d7d8Z5dVd:d;Z6dd?Z8d@dAZ9GdBdCdCe:Z;GdDdEdEe;ZdWdKdLZ?dMdNZ@ J J *dXdOdPZAGdQdRdRejBZCGdSdTdTeCZDdJS)Yz;High level utilities which build upon other modules here. )deque)chain) operators)visitors)_deep_annotate)_deep_deannotate)_shallow_annotate) _from_objects) ColumnSet) sort_tables)_expand_cloned) _find_columns)_label_reference)_textual_label_reference) BindParameter) ColumnClause) ColumnElement)Null)UnaryExpression)Column)Alias) FromClause) FromGrouping)Join) ScalarSelect) SelectBase) TableClause)exc)utilz.sql.util.join_conditioncCsDtt|}g}t|D]\}}|D] }||r||qq |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 selectablesidxifsr,>/usr/local/lib/python3.10/dist-packages/sqlalchemy/sql/util.pyfind_join_source1s   r.c Cstt|}g}t|D]\}}|D]}||r ||nqq t|dkrNg}|D]}||}|D]}tt|t|rH||nq4q,|rN|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% join_fromr' liberal_idxr)r*r+conservative_idxr(r,r,r-#find_left_clause_that_matches_givenKs0      r6c sg}tt|}tdkr|durd}t|}nd}d}tD]4\}}||gD])} |rCt|j| j|rB| |nq+t || sM|durT| |nq+q!t|dkrqtt ddDfdd|D}|s}|dur}t tS|S)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|jqSr,)r _hide_froms).0r*r,r,r- sz1find_left_clause_to_join_from..csg|] }|vr|qSr,r,)r8r)r%toremover,r-r9s)r0r r/rr" differencecunion issupersetr$r _can_joinrrange) r%r&onclauser(r'resolve_ambiguitycols_in_onclauser)r*r+r,r:r-find_left_clause_to_join_fromus6       rEcs(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 |VdS|jdkrFt|jrFd||jD]}|jD] }d||q(q! d| D]}|q=dSt|t rN|V| D] }|D]}|VqXqRdS)Nbinaryr) isinstancer__visit_name__r is_comparisonoperatorinsertleftrightpop get_childrenr)elementlrelemefnstackvisitr,r-rXs,         z#visit_binary_product..visitN)r!)rVexprr,rUr-visit_binary_products# rZFcsgi}|rj|d<|d<|rj|d<|rj|d<|r/fdd|d<|d<|d <|r;fd d }||d <j|d <t|ddi|S)z1locate Table objects within the given expression.selectcompound_selectjoinaliascs |jSNr$table)enttablesr,r-s zfind_tables..rKupdatedeletec|jdSr_r`)columnrcr,r- visit_columnz!find_tables..visit_columnriracolumn_collectionsF)r$rtraverse)clause check_columnsinclude_aliases include_joinsinclude_selects include_crud _visitorsrjr,rcr- find_tabless&      rucCst}g}t|g}|rL|}t|tr>t|tr!t|j s>t|t r)|j }t|t r/q ||vr=| |||n |D]}||qB|s |S)zqBreak up an 'order by' expression into individual column-expressions, without DESC/ASC/NULLS FIRST/NULLS LAST)r column_setrpopleftrGrrris_ordering_modifiermodifierrrPraddr$rO)rncolsresultrWtr=r,r,r-unwrap_order_bys,         r~cCsdd}t|i|S)NcSst|ttfr |jSdSr_)rGrrrP)rSr,r,r-replace+sz'unwrap_label_reference..replace)rreplacement_traverse)rPrr,r,r-unwrap_label_reference*srcs2tdd|Dfddtdd|DDS)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|] }|jdur |jn|qSr_)_order_by_label_elementrPr8colr,r,r-r9:sz4expand_column_list_from_order_by..csg|]}|vr|qSr,r,rcols_already_presentr,r-r9@s cSsg|]}t|qSr,)r~)r8or,r,r-r9Bs)r0r)collistorder_byr,rr- expand_column_list_from_order_by2s rcCs t|D] }||kr dSqdS)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. TF)r1)rnsearchrSr,r,r-clause_is_presentGs rccsZ|g}|r+|}|Vt|tr||j|jfn t|tr'||j|sdSdSr_) rNrGrextendrLrMrr$rPrnrWrSr,r,r-r1Ws    r1ccs|g}|r>|}t|ttfr|Vt|tr#||j|jfnt|tr/| |j n t|t r:| |j |sdSdSr_) rNrGrrrrrLrMrr$rPrrarr,r,r-surface_selectables_onlybs     rTccsbtf}|s |tf7}t|g}|r/|}|V|D] }t||r%q||q|sdSdS)ztraverse and yield only outer-exposed column elements, such as would be addressable in the WHERE clause of a SELECT if this element were in the columns clause.N)rrrrwrOrGr$)rninclude_scalar_selectsfilter_rWrSsubr,r,r-surface_column_elementsps      rcCsttt|t|S)z:Return True if left/right have some overlapping selectable)boolr0r1r2)rLrMr,r,r-selectables_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] crhr_)r$effective_value)bindvr,r-visit_bindparamrkz$bind_values..visit_bindparam bindparam)rrm)rnrr,rr- bind_valuess rcCs(t|tjr|dd}d|St|S)N'z''z'%s')rGr string_typesrrepr)rPr,r,r-_quote_ddl_exprs  rc@s$eZdZdZdZdZdZddZdS) _repr_baserrr) max_charscCsPt|}t|}||jkr&|jd}|d|d||j|| d}|S)Nrrz# ... (%d characters truncated) ... )rr/r)selfvaluereplenrepsegment_lengthr,r,r-truncs    z_repr_base.truncN)__name__ __module__ __qualname___LIST_TUPLE_DICT __slots__rr,r,r,r-rs  rc@s&eZdZdZdZd ddZddZdS) _repr_rowzProvide a string view of a row.)row,cC||_||_dSr_)rr)rrrr,r,r-__init__ z_repr_row.__init__cs>|jddfdd|jDt|jdkrdfSdfS)N(%s%s), c3|]}|VqdSr_r,r8rrr,r- z%_repr_row.__repr__..r,)rr]rr/)rr,rr-__repr__sz_repr_row.__repr__N)r)rrr__doc__rrrr,r,r,r-rs   rc@s6eZdZdZdZd ddZddZd d Zd d ZdS) _repr_paramszProvide a string view of bound parameters. Truncates display to a given numnber of 'multi' parameter sets, as well as long values to a given number of characters. )paramsbatchesismultirNcCs||_||_||_||_dSr_)rrrr)rrrrrr,r,r-rs z_repr_params.__init__c Cs|jdur ||jSt|jtr|j}nt|jtr|j}nt|jtr)|j }n||jS|jrit |j|j krid}d | |jd|j d|dd||j t |jf| |jdd|ddfS|jrs| |j|S||j|S)Nz8 ... displaying %i of %i total bound parameter sets ...  rrr)rrrrGr!rtuplerdictrr/rr] _repr_multir)rtypmsgr,r,r-rs,       z_repr_params.__repr__cs|r;t|dtr jn t|dtrjnt|dtr#jn Jdt|ddfdd|D}nd}|jkrFd|Sd |S) NrFzUnknown parameter type %src3s|] }|VqdSr_)r)r8r elem_typerr,r-r s  z+_repr_params._repr_multi..r[%s]z(%s)) rGr!rrrrrtyper])r multi_paramsrelementsr,rr-rs"   z_repr_params._repr_multics|j||jurddfdd|DS||jur7ddfdd|Dt|dkr3dfSd fSd dfd d|DS) Nz{%s}rc3s$|] \}}d||fVqdS)z%r: %sNr,)r8keyrrr,r-rs  z,_repr_params._repr_params..rc3rr_r,rrr,r-r!rrrrrc3rr_r,rrr,r-r%r)rrr]itemsrr/)rrrr,rr-rs  z_repr_params._repr_params)rN) rrrrrrrrrr,r,r,r-rs  rcsfdd}t|id|iS)zWgiven criterion containing bind params, convert selected elements to IS NULL. csxt|jtr|jjvr|j|_t|_tj|_tj |_ dSt|jtr8|jjvr:t|_tj|_tj |_ dSdSdSr_) rGrLr_identifying_keyrMrris_rJisnotnegaterFnullsr,r- visit_binary.s      z-adapt_criterion_to_null..visit_binaryrF)rcloned_traverse)critrrr,rr-adapt_criterion_to_null(s rNcCs|dur|S|dfg}t|}d}|rN|\}}t|tr:||ur:|}|||j|_||j |fn||}|durF||_ |durL|}|s|Sr_) ClauseAdapterrNrGr_clone_reset_exportedrmrBr$rL)rLrMstop_onrWadapterret prevrightr,r,r- splice_joinsCs&   rc s|dd}|ddttD]L}tdd|jDD]?}D]:}||ur.q'z|j}Wntjy@|r?Yq'tj yL|rKYq'w| |rarZ|j |j kra |nq'q#q|rfdd}|D]} | durt | id |iqptS) 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_synonymscSg|]}|jqSr,) foreign_keysr8r=r,r,r-r9wz"reduce_columns..cs|jtjkr=ttddD}|j|vr?|j|vrAt D]}| |jr<r4|j |jj kr< |dSq#dSdSdSdS)NcSrr,) proxy_setrr,r,r-r9rz8reduce_columns..visit_binary..) rJreqr rvrr<rLrMreversedshares_lineagenamerz)rFr{r=columnsomitrr,r-rs    z$reduce_columns..visit_binaryNrF)rNr ordered_column_setrvrrrirNoReferencedColumnErrorNoReferencedTableErrorrrrzrrmr r<) rr%kwrrfkr=fk_colrrnr,rr-reduce_columns\sF        rcsHr r tdddfdd}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 ||Sr_)compare)abr,r,r-col_iss z"criterion_as_pairs..col_iscss |jtjur dSt|jtrt|jtsdSrV|jvr6|j|js+|jvr6|j|jfdS|jvrR|j|jsG|jvrT|j|jfdSdSdSr|jvrt|j|jsi|jvrt|j|jfdS|jvr|j|js|jvr|j|jfdSdSdSt|jtrt|jtr|j |jr|j|jfdS|j |jrȈ|j|jfdSdSdSdSr_) rJrrrGrLrrMr$r referencesr any_operatorrconsider_as_foreign_keysconsider_as_referenced_keyspairsr,r-rsR            z(criterion_as_pairs..visit_binaryrF)r ArgumentErrorrrm) expressionrrrrr,rr-criterion_as_pairss'r c@s:eZdZdZ     d ddZejfddZdd ZdS) 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|pi|_||_dS)N)ranonymize_labels)__traverse_options__ selectable include_fn exclude_fnr column_dict equivalentsadapt_on_names)rr rr rrr r,r,r-rs  zClauseAdapter.__init__cCs|jj||d}|dur1||jvr1||vr1|j|D]}|j||||gd}|dur0|Sq|jr@|dur@|jj|j}|S)N)require_embedded)r_seen) r corresponding_columnr_corresponding_columnr>rr=getr)rrrrnewcolequivr,r,r-rs  z#ClauseAdapter._corresponding_columncCs^t|tr|j|r|jSt|tsdS|jr||sdS|jr)||r)dS||dS)NT)rGrr r#rr rr)rrr,r,r-r's  zClauseAdapter.replace)NNNFF) rrrrrr EMPTY_SETrrr,r,r,r-rs  rc@sjeZdZdZ       dddZGdddeZd d Zd d ZeZ e j Z d dZ ddZddZdS) 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 CsXtj|||||||dt|j|_|js|jr!|||j|_||_ ||_ d|_ dS)N)r rrr ) rrr WeakPopulateDict _locate_colrr r_IncludeExcludeMappingadapt_requiredallow_label_resolve_wrap) rr rrr rrrr r,r,r-rUs   zColumnAdapter.__init__c@seZdZddZddZdS)z$ColumnAdapter._IncludeExcludeMappingcCrr_)parentr)rr!rr,r,r-rrrz-ColumnAdapter._IncludeExcludeMapping.__init__cCsL|jjr |j|r|jjr!|j|r!|jjr|jjj|S|S|j|Sr_)r!r rr r)rrr,r,r- __getitem__vs   z0ColumnAdapter._IncludeExcludeMapping.__getitem__N)rrrrr"r,r,r,r-rqs rcCsP|j|j}|j|j||_t|j|_|j s|j r&| ||j|_|Sr_) __class____new____dict__rfr r rrrr rr)rracr,r,r-wraps zColumnAdapter.wrapcCs |j|Sr_)r)robjr,r,r-rms zColumnAdapter.traversecCsHt||}|jr|j|}|dur|}|jr||urdS|j|_|Sr_)rrmr rrr_allow_label_resolve)rrr=c2r,r,r-rs  zColumnAdapter._locate_colcCs|j}|d=|S)Nr)r%copy)rdr,r,r- __getstate__s zColumnAdapter.__getstate__cCs|j|t|j|_dSr_)r%rfr rrr)rstater,r,r- __setstate__s zColumnAdapter.__setstate__)NFNNFTF)rrrrrobjectrr'rm adapt_clausercopy_and_process adapt_listrr-r/r,r,r,r-r6s$!   r)FFFFF)Tr_)NNF)Er collectionsr itertoolsrrrr annotationrrr baser r ddlr rr rrrrrrrrschemarr rrrrrrrrr langhelperspublic_factory_join_conditionjoin_conditionr.r6rErZrur~rrrr1rrrrrr0rrrrrrr ReplacingCloningVisitorrrr,r,r,r-s                             *4A ( X M ?P