B ÃÌ4][rã@sfdZddlmZddlmZddlmZddlmZddlm 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/d0„Z1d1d2„Z2d3d4„Z3d5d6„Z4d7d8„Z5dVd:d;„Z6dd?„Z8d@dA„Z9GdBdC„dCe:ƒZ;GdDdE„dEe;ƒZdWdKdL„Z?dMdN„Z@dXdOdP„ZAGdQdR„dRejBƒZCGdSdT„dTeCƒZDdJS)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_conditioncCsLtt|ƒƒ}g}x6t|ƒD]*\}}x |D]}| |¡r(| |¡q(WqW|S)a”Given 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 Ú enumerateÚis_derived_fromÚappend)ÚclausesÚjoin_toÚ selectablesÚidxÚiÚfÚs©r,úF/opt/alt/python37/lib64/python3.7/site-packages/sqlalchemy/sql/util.pyÚfind_join_source1s   r.c Cs¬tt|ƒƒ}g}x8t|ƒD],\}}x"|D]}| |¡r(| |¡Pq(WqWt|ƒdkr¨g}xD|D]<}||}x.|D]&}tt|ƒƒ t|ƒ¡rr| |¡PqrWq`W|r¨|S|S)zŽGiven 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$ÚlenÚsetÚsurface_selectablesÚ intersection) r%Z join_fromr'Z liberal_idxr)r*r+Zconservative_idxr(r,r,r-Ú#find_left_clause_that_matches_givenKs&            r3c s g}tt|ƒƒ}tˆƒdkr2|dk r2d}t|ƒ}nd}d}xttˆƒD]h\}}x^| |g¡D]N} |rˆt|jƒ | j¡ |¡r¨|  |¡PqZt   || ¡sœ|dk rZ|  |¡PqZWqDWt|ƒdkrætt dd„ˆDƒŽƒ‰‡‡fdd„|Dƒ}|s|dk rt tˆƒƒS|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|jƒ‘qSr,)r Z _hide_froms)Ú.0r*r,r,r-ú sz1find_left_clause_to_join_from..csg|]}ˆ|ˆkr|‘qSr,r,)r4r))r%Útoremover,r-r5Ÿs)r0r r/rr"Ú differenceÚcÚunionÚ issupersetr$rZ _can_joinrÚrange) r%r&Úonclauser(r'Zresolve_ambiguityZcols_in_onclauser)r*r+r,)r%r6r-Úfind_left_clause_to_join_fromus.        r=cs$g‰‡‡‡fdd„‰tˆ|ƒƒdS)aýProduce 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. c3sÐt|tƒr|Vnº|jdkr’t |j¡r’ˆ d|¡x6ˆ|jƒD](}x"ˆ|jƒD]}ˆˆd||ƒqPWq@Wˆ  d¡xT|  ¡D] }ˆ|ƒq€Wn:t|t ƒr¢|Vx(|  ¡D]}xˆ|ƒD] }|VqºWq¬WdS)NÚbinaryr) Ú isinstancerZ__visit_name__rZ is_comparisonÚoperatorÚinsertÚleftÚrightÚpopÚ get_childrenr)ÚelementÚlÚrÚelemÚe)ÚfnÚstackÚvisitr,r-rMÎs     z#visit_binary_product..visitN)r!)rKÚexprr,)rKrLrMr-Úvisit_binary_product©s#rOFcs–g‰i}|rˆj|d<|d<|r,ˆj|d<|r:ˆj|d<|r^‡fdd„|d<|d<|d <|rv‡fd d „}||d <ˆj|d <t |ddi|¡ˆS)z1locate Table objects within the given expression.ZselectZcompound_selectÚjoinÚaliascs ˆ |j¡S)N)r$Útable)Zent)Útablesr,r-Úózfind_tables..rAÚupdateÚdeletecsˆ |j¡dS)N)r$rR)Úcolumn)rSr,r-Ú visit_columnsz!find_tables..visit_columnrXrRZcolumn_collectionsF)r$rÚtraverse)ÚclauseZ check_columnsZinclude_aliasesZ include_joinsZinclude_selectsZ include_crudZ _visitorsrYr,)rSr-Ú find_tablesçs      r\cCs¢t ¡}g}t|gƒ}x†|rœ| ¡}t|tƒr~t|tƒrDt |j ¡s~t|t ƒrT|j }t|t ƒr`q||krš|  |¡| |¡qx| ¡D]}| |¡qˆWqW|S)zqBreak up an 'order by' expression into individual column-expressions, without DESC/ASC/NULLS FIRST/NULLS LAST)r Ú column_setrÚpopleftr?rrrZis_ordering_modifierZmodifierrrFrÚaddr$rE)r[ÚcolsÚresultrLÚtr8r,r,r-Úunwrap_order_bys$        rccCsdd„}t |i|¡S)NcSst|ttfƒr|jSdS)N)r?rrrF)rIr,r,r-Úreplace*sz'unwrap_label_reference..replace)rZreplacement_traverse)rFrdr,r,r-Úunwrap_label_reference)srecs2tdd„|Dƒƒ‰‡fdd„tdd„|DƒŽDƒS)zÜGiven 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|‘qS)N)Z_order_by_label_elementrF)r4Úcolr,r,r-r5:sz4expand_column_list_from_order_by..csg|]}|ˆkr|‘qSr,r,)r4rf)Úcols_already_presentr,r-r5@scSsg|] }t|ƒ‘qSr,)rc)r4Úor,r,r-r5As)r0r)ZcollistZorder_byr,)rgr-Ú expand_column_list_from_order_by1s   ricCs&x t|ƒD]}||kr dSq WdSdS)zÐGiven 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)r1)r[ÚsearchrIr,r,r-Úclause_is_presentFs rkccsV|g}xJ|rP| ¡}|Vt|tƒr8| |j|jf¡qt|tƒr| |j¡qWdS)N) rDr?rÚextendrBrCrr$rF)r[rLrIr,r,r-r1Vs  r1ccs||g}xp|rv| ¡}t|ttfƒr(|Vt|tƒrF| |j|jf¡qt|tƒr^|  |j ¡qt|t ƒr|  |j ¡qWdS)N) rDr?rrrrlrBrCrr$rFrrR)r[rLrIr,r,r-Úsurface_selectables_onlyas   rmTccsbtf}|s|tf7}t|gƒ}x>|r\| ¡}|Vx&| ¡D]}t||ƒrLq<| |¡q>> expr = and_( ... table.c.foo==5, table.c.foo==7 ... ) >>> bind_values(expr) [5, 7] csˆ |j¡dS)N)r$Zeffective_value)Zbind)Úvr,r-Úvisit_bindparam˜sz$bind_values..visit_bindparamZ bindparam)rrZ)r[rsr,)rrr-Ú bind_valuesŠs  rtcCs,t|tjƒr | dd¡}d|St|ƒSdS)Nú'z''z'%s')r?r Z string_typesrdÚrepr)rFr,r,r-Ú_quote_ddl_exprŸs  rwc@s$eZdZdZdZdZdZdd„ZdS)Ú _repr_baserrr)Ú max_charscCsPt|ƒ}t|ƒ}||jkrL|jd}|d|…d||j|| d…}|S)Nrrz# ... (%d characters truncated) ... )rvr/ry)ÚselfÚvalueZrepZlenrepZsegment_lengthr,r,r-Útrunc®s   (z_repr_base.truncN)Ú__name__Ú __module__Ú __qualname__Ú_LISTÚ_TUPLEÚ_DICTÚ __slots__r|r,r,r,r-rx§s rxc@s&eZdZdZdZd dd„Zdd„ZdS) Ú _repr_rowzProvide a string view of a row.)Úrowé,cCs||_||_dS)N)r…ry)rzr…ryr,r,r-Ú__init__Ãsz_repr_row.__init__cs:|j‰dd ‡fdd„|jDƒ¡t|jƒdkr2dndfS)Nz(%s%s)z, c3s|]}ˆ|ƒVqdS)Nr,)r4r{)r|r,r-ú Êsz%_repr_row.__repr__..rú,Ú)r|rPr…r/)rzr,)r|r-Ú__repr__Çsz_repr_row.__repr__N)r†)r}r~rÚ__doc__rƒr‡r‹r,r,r,r-r„¾s r„c@s6eZdZdZdZd dd„Zdd„Zdd „Zd d „Zd S)Ú _repr_paramsz±Provide 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. )ÚparamsÚbatchesé,cCs||_||_||_dS)N)rŽrry)rzrŽrryr,r,r-r‡Ùsz_repr_params.__init__c Cst|jtƒr0|j}|jo,t|jdtttfƒ}nTt|jtƒr`|j}|jo\t|jdtttfƒ}n$t|jtƒrx|j}d}n | |j¡S|röt |jƒ|j kröd}d  |  |jd|j d…|¡dd…||j t |jƒf|  |jdd…|¡dd…f¡S|r |  |j|¡S|  |j|¡SdS) NrFz8 ... displaying %i of %i total bound parameter sets ... ú réÿÿÿÿéþÿÿÿr)r?rŽr!r€ÚdictÚtuplerr‚r|r/rrPÚ _repr_multir)rzÚtypZismultiÚmsgr,r,r-r‹Þs,    "z_repr_params.__repr__csœ|rzt|dtƒrˆj‰nDt|dtƒr0ˆj‰n.t|dtƒrFˆj‰nds^tdt|dƒƒ‚d  ‡‡fdd„|Dƒ¡}nd}|ˆjkrd|Sd |SdS) NrFzUnknown parameter type %sz, c3s|]}ˆ |ˆ¡VqdS)N)r)r4rŽ)Ú elem_typerzr,r-rˆ sz+_repr_params._repr_multi..rŠz[%s]z(%s)) r?r!r€r•rr”r‚ÚAssertionErrorÚtyperP)rzZ multi_paramsr—Úelementsr,)r™rzr-r–ÿs z_repr_params._repr_multicsŠ|j‰||jkr0dd ‡fdd„| ¡Dƒ¡S||jkrjdd ‡fdd„|Dƒ¡t|ƒdkrbdnd fSd d ‡fd d„|Dƒ¡SdS) Nz{%s}z, c3s"|]\}}d|ˆ|ƒfVqdS)z%r: %sNr,)r4Úkeyr{)r|r,r-rˆsz,_repr_params._repr_params..z(%s%s)c3s|]}ˆ|ƒVqdS)Nr,)r4r{)r|r,r-rˆ"srr‰rŠz[%s]c3s|]}ˆ|ƒVqdS)Nr,)r4r{)r|r,r-rˆ&s)r|r‚rPÚitemsrr/)rzrŽr—r,)r|r-rs   z_repr_params._repr_paramsN)r) r}r~rrŒrƒr‡r‹r–rr,r,r,r-rÏs  !rcs‡fdd„}t |id|i¡S)zWgiven criterion containing bind params, convert selected elements to IS NULL. csnt|jtƒr:|jjˆkr:|j|_tƒ|_tj|_tj |_ n0t|jtƒrj|jjˆkrjtƒ|_tj|_tj |_ dS)N) r?rBrZ_identifying_keyrCrrÚis_r@ZisnotÚnegate)r>)Únullsr,r-Ú visit_binary/s     z-adapt_criterion_to_null..visit_binaryr>)rZcloned_traverse)Zcritr¡r¢r,)r¡r-Úadapt_criterion_to_null)s r£NcCs¢|dkr |S|dfg}t|ƒ}d}xz|rœ| ¡\}}t|tƒrv||k rv| ¡}| ¡| |j¡|_| |j |f¡n | |¡}|dk rŽ||_ |dkr$|}q$W|S)N) Ú ClauseAdapterrDr?rZ_cloneZ_reset_exportedrZr<r$rB)rBrCÚstop_onrLÚadapterZretZ prevrightr,r,r-Ú splice_joinsDs$   r§c s*| dd¡}| dd¡‰t ˆ¡‰t ¡‰x²ˆD]ª}x¤tdd„|jDƒŽD]Œ}x†ˆD]~}||krbqTy |j}Wn>tjk rŒ|r†wTn‚Yn tj k rª|r¤wTn‚YnX|  |¡rTˆrÆ|j |j krTˆ  |¡PqTWqJWq0W|r‡‡‡fdd„}x&|D]} | dk rút  | id |i¡qúWtˆ ˆ¡ƒS) 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|] }|j‘qSr,)Z foreign_keys)r4r8r,r,r-r5xsz"reduce_columns..cs~|jtjkrzt tdd„ˆ ˆ¡DƒŽ¡}|j|krz|j|krzx:t ˆƒD].}|  |j¡rHˆrj|j |jj krHˆ  |¡PqHWdS)NcSsg|] }|j‘qSr,)Ú proxy_set)r4r8r,r,r-r5—sz8reduce_columns..visit_binary..) r@rÚeqr r]rr7rBrCÚreversedÚshares_lineageÚnamer_)r>r`r8)ÚcolumnsÚomitr©r,r-r¢”s   z$reduce_columns..visit_binaryNr>)rDr Zordered_column_setr]rrªrXrZNoReferencedColumnErrorZNoReferencedTableErrorr­r®r_rrZr r7) r¯r%Úkwr¨rfZfkr8Zfk_colr¢r[r,)r¯r°r©r-Úreduce_columns]s:         r²csHˆrˆrt d¡‚dd„‰‡‡‡‡‡fdd„}g‰t |id|i¡ˆS)z9traverse an expression and locate binary criterion pairs.zSCan only specify one of 'consider_as_foreign_keys' or 'consider_as_referenced_keys'cSs | |¡S)N)Zcompare)ÚaÚbr,r,r-Úcol_is·sz"criterion_as_pairs..col_iscs~ˆs|jtjk rdSt|jtƒr,t|jtƒs0dSˆr |jˆkrjˆ|j|jƒsV|jˆkrjˆ |j|jf¡n4|jˆkržˆ|j|jƒsŒ|jˆkržˆ |j|jf¡nÚˆr|jˆkr܈|j|jƒsÈ|jˆkr܈ |j|jf¡n:|jˆkrzˆ|j|jƒs|jˆkrzˆ |j|jf¡nbt|jtƒrzt|jtƒrz|j  |j¡rXˆ |j|jf¡n"|j  |j¡rzˆ |j|jf¡dS)N) r@rr«r?rBrrCr$rZ references)r>)Ú any_operatorrµÚconsider_as_foreign_keysÚconsider_as_referenced_keysÚpairsr,r-r¢»s:          z(criterion_as_pairs..visit_binaryr>)rZ ArgumentErrorrrZ)Z expressionr·r¸r¶r¢r,)r¶rµr·r¸r¹r-Úcriterion_as_pairs¨s'rºc@s0eZdZdZd dd„Zejfdd„Zdd „ZdS) r¤a5Clones 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)r¥Úanonymize_labels)Z__traverse_options__Ú selectableÚ include_fnÚ exclude_fnr Z column_dictÚ equivalentsÚadapt_on_names)rzr¼r¿r½r¾rÀr»r,r,r-r‡s  zClauseAdapter.__init__cCs„|jj||d}|dkrb||jkrb||krbx6|j|D](}|j||| |g¡d}|dk r6|Sq6W|jr€|dkr€|jj |j¡}|S)N)Úrequire_embedded)rÁÚ_seen) r¼Zcorresponding_columnr¿Ú_corresponding_columnr9rÀr8Úgetr®)rzrfrÁrÂZnewcolZequivr,r,r-rÃs z#ClauseAdapter._corresponding_columncCsbt|tƒr|j |¡r|jSt|tƒs*dS|jr>| |¡s>dS|jrR| |¡rRdS| |d¡SdS)NT)r?rr¼r#rr½r¾rÃ)rzrfr,r,r-rd(s zClauseAdapter.replace)NNNFF) r}r~rrŒr‡r Z EMPTY_SETrÃrdr,r,r,r-r¤çs r¤c@s\eZdZdZddd„ZGdd„deƒZd d „Zd d „ZeZ e j Z d d„Z dd„Zdd„ZdS)Ú ColumnAdapteraƒExtends 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¡|_|js2|jrB| ||j¡|_||_ ||_ d|_ dS)N)r½r¾rÀr») r¤r‡r Úpopulate_column_dictÚ _locate_colr¯r½r¾Ú_IncludeExcludeMappingÚadapt_requiredÚallow_label_resolveÚ_wrap) rzr¼r¿rÉr½r¾rÀrÊr»r,r,r-r‡Vs  zColumnAdapter.__init__c@seZdZdd„Zdd„ZdS)z$ColumnAdapter._IncludeExcludeMappingcCs||_||_dS)N)Úparentr¯)rzrÌr¯r,r,r-r‡ssz-ColumnAdapter._IncludeExcludeMapping.__init__cCsL|jjr|j |¡r(|jjrB|j |¡rB|jjr>|jjj|S|S|j|S)N)rÌr½r¾rËr¯)rzrr,r,r-Ú __getitem__ws z0ColumnAdapter._IncludeExcludeMapping.__getitem__N)r}r~rr‡rÍr,r,r,r-rÈrsrÈcCsP|j |j¡}|j |j¡||_t |j¡|_|j s<|j rL|  ||j¡|_|S)N) Ú __class__Ú__new__Ú__dict__rVrËr rÆrÇr¯r½r¾rÈ)rzr¦Zacr,r,r-Úwraps zColumnAdapter.wrapcCs |j|S)N)r¯)rzÚobjr,r,r-rZ‹szColumnAdapter.traversecCsHt ||¡}|jr*|j |¡}|dk r*|}|jr<||kr sz                              *4@ "  Z M ;P