L i܍HdZddlmZddlmZddlmZddlZddlZddl Z ddl m Z ddl m Z ddl m Z dd l m Z dd l mZdd l mZdd l mZdd l mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddlmZddlmZddlmZddlmZddl m!Z!ddl m"Z"ddl m#Z#er ddl$m%Z%dd l&m'Z'e j.ses dd!l(m)Z)dd"l(m*Z+n dd!l,m)Z)dd"l,m*Z+gd#Z-Gd$d%e"Z.Gd&d'Z/Gd(d)eZ0eee1e0fZ2 Gd*d+Z3Gd,d-e"Z4Gd.d/Z5e0Z6dPd0Z7e5jpZ8e7Gd1d2e3e/Z9ed3e94Z:ed5d64Z;e e:gdfZ<Gd7d8e"Z=Gd9d:e"e:Z>ed;d<4Z?Gd=dd?eAZBGd@dAeBZCe/ZDeAZEeBZFeCZGejf dQdBZIe dRdCZJe dSdDZJ dTdEZJe dUdFZKe dVdGZK dWdHZKe dUdIZLe dXdJZL dWdKZLe dYdLZMe dZdMZMe d[dNZM d\dOZMy)]z2Visitor/traversal interface and library functions.) annotations)deque)EnumN)Any)Callable)cast)ClassVar)Dict)Iterable)Iterator)List)Mapping)Optional)overload)Tuple)Type) TYPE_CHECKING)TypeVar)Union)exc)util) langhelpers)HAS_CYEXTENSION)Literal)Protocol)Self)_AnnotationDict) ColumnElement)prefix_anon_map)cache_anon_map) iteratetraverse_usingtraversecloned_traversereplacement_traverse VisitableExternalTraversalInternalTraversalanon_mapceZdZddZy)_CompilerDispatchTypec yN)_selfselfvisitorkws ]/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sqlalchemy/sql/visitors.py__call__z_CompilerDispatchType.__call__HN)r2r(r3rr4rreturnr__name__ __module__ __qualname__r6r0r8r5r-r-GsMr8r-cxeZdZUdZdZded<ded<ejrd dZd fd Z e d d Z d d Z xZ S)r(a,Base class for visitable objects. :class:`.Visitable` is used to implement the SQL compiler dispatch functions. Other forms of traversal such as for cache key generation are implemented separately using the :class:`.HasTraverseInternals` interface. .. versionchanged:: 2.0 The :class:`.Visitable` class was named :class:`.Traversible` in the 1.4 series; the name is changed back to :class:`.Visitable` in 2.0 which is what it was prior to 1.4. Both names remain importable in both 1.4 and 2.0 versions. r0str__visit_name__r-_original_compiler_dispatchc yr/r0)r2r3r4s r5_compiler_dispatchzVisitable._compiler_dispatchcr7r8c^d|jvr|jt| y)Nr@)__dict___generate_compiler_dispatchsuper__init_subclass__)cls __class__s r5rHzVisitable.__init_subclass__es& s|| +  + + - !#r8c0|j}d|jvr|j|_yt |t s#t jd|jdd|z}tj| dfd }|x|_|_y)NrCz__visit_name__ on class z$ must be a string at the class levelvisit_%sc| |}||fi|S#t$r}|j||fi|cYd}~Sd}~wwxYw)zLook for an attribute named "visit_" on the visitor, and call it with the same kw params. N)AttributeErrorvisit_unsupported_compilation)r2r3r4metherrgetters r5rCzAVisitable._generate_compiler_dispatch.._compiler_dispatch~sQ (gD'B''" Nz4HasTraverseInternals.get_children..s5- ##tz)co OO- s !_generated_get_children_traversal) r preloadedsql_traversalsrrN _get_childrenrun_generated_dispatch itertoolschain from_iterable)r2rr4 traversalstraverse_internalsdispatchs `` r5 get_childrenz!HasTraverseInternals.get_childrens"^^22  !%!9!9  ++BB,,- '/(*M(-    I s A11 A?>A?N)rTuple[str, ...]r4rr9zIterable[HasTraverseInternals]) r;r<r=r^r_r`rrpreload_modulerr0r8r5rrsYI//M4T45/1 , 9< ' 6 r8rceZdZddZy)_InternalTraversalDispatchTypecyr/r0)sr2r3s r5r6z'_InternalTraversalDispatchType.__call__r7r8N)r2objectr3HasTraversalDispatchr9rr:r0r8r5rrsLr8rcpeZdZUdZdZiZded<d dZ d dZ d dZ d dZ y )rzcDefine infrastructure for classes that perform internal traversals .. versionadded:: 2.0 r0z2ClassVar[Dict[Union[InternalTraversal, str], str]]_dispatch_lookupc.t|}t||dS)ztGiven a method from :class:`.HasTraversalDispatch`, return the corresponding method on a subclass. N)rgetattr)r2 visit_symbolrXs r5rzHasTraversalDispatch.dispatchs  -tT4((r8c |jj|}|||S#t$r |j|j||}Y1wxYwr/)rJrEKeyErrorgenerate_dispatch)r2targetinternal_dispatchgenerate_dispatcher_name dispatchers r5rz+HasTraversalDispatch.run_generated_dispatchs_ ))223KLJ&$''  //  "35MJ  s$&A  A cD|j||}t||||Sr/)_generate_dispatchersetattr)r2 target_clsrrrs r5rz&HasTraversalDispatch.generate_dispatchs/ .. 7   4jAr8cg}|D]5\}}|j|}|t|}|j||f7ddjd|Dzdz}d|z|zdz} t t t j| i|S)Nz return [ z, c3:K|]\}}d|d|d|dyw)z (z, self.z , visitor.)Nr0)rrrWs r5rz..s'-* :7sz ] zdef %s(self, visitor):  )rrappendjoinrrr_exec_code_in_env) r2r method_namenamesr visit_symrPrWcode meth_texts r5rz)HasTraversalDispatch._generate_dispatcher!s#4 5 Hi==+D-i8  h 34  5 16   0+=EL  *  ) ))R E  r8N)rr*r9zCallable[..., Any])rrrrrr?r9r)rz Type[object]rrrr?r9r)rrrr?r9r) r;r<r=r^r_rr`rrrrr0r8r5rrs IKMHM)((2(#& ( (&   2 #&  (  !7 FI ' r8rct}tD]S}|j}|jds!|j dd}|j }||vsJ||x||<||<Uy)Ndp_visit_)rr*rX startswithreplacevalue)lookupsymrZ visit_keysym_names r5_generate_traversal_dispatchr@sj F 7hh >>%  E84IyyH6) 38 3)-6 6F3K&* 7r8ceZdZUdZej Zded<ejrd dZ dd d dZ d dZ dd d dZ y )ExternallyTraversibler0zMapping[Any, Any] _annotationscyr/r0)r2valuess r5 _annotatezExternallyTraversible._annotateWr7r8rc yr/r0r2rr4s r5rz"ExternallyTraversible.get_childrenYs.1r8c t)zclone this elementNotImplementedError)r2r4s r5_clonezExternallyTraversible._clone]s !##r8c t)atReassign internal elements to be clones of themselves. Called during a copy-and-traverse operation on newly shallow-copied elements to create a deep copy. The given clone function should be used, which may be applying additional transformations to the element (i.e. replacement traversal, cloned traversal, annotations). rrs r5_copy_internalsz%ExternallyTraversible._copy_internalsas "##r8N)rrr9r)rrr4rr9Iterable[ExternallyTraversible])r4rr9r)rrr4rr9r])r;r<r=r_r EMPTY_DICTrr`rarrrrr r0r8r5rrPskI&*ooL#5 A46 2!0 2=@ 2 , 2$ 02 $, $9< $  $r8r_ET)bound_CEzColumnElement[Any]ceZdZddZy)_CloneCallableTypec yr/r0r2elementr4s r5r6z_CloneCallableType.__call__yr7r8N)rr r4rr9r r:r0r8r5rrxs;r8rceZdZddZy)_TraverseTransformCallableTypec yr/r0rs r5r6z'_TraverseTransformCallableType.__call__}r7r8N)rr r4rr9z Optional[_ET]r:r0r8r5rr|sEr8r_ExtTr)ceZdZUdZdZiZded<ded<ddZ ddZe dd Z e dd Z dd Z dd Z e dd Z ddZy)r)zBase class for visitor objects which can traverse externally using the :func:`.visitors.traverse` function. Direct usage of the :func:`.visitors.traverse` function is usually preferred. ) _visitor_dict_nextzDict[str, Any]__traverse_options__zOptional[ExternalTraversal]rc t|jD])}t|d|jzd}|s ||fi|cSy)NrL)visitor_iteratorrr@)r2rr4vrPs r5traverse_singlez!ExternalTraversal.traverse_singlesA&& 'A1j3+=+==tDDC2& 'r8c.t||jS)zaTraverse the given expression structure, returning an iterator of all elements. )r#rr2rs r5r#zExternalTraversal.iteratessD5566r8cyr/r0r!s r5r%zExternalTraversal.traverse47r8cyr/r0r!s r5r%zExternalTraversal.traverse!$r8cDt||j|jS2Traverse and visit the given expression structure.)r%rrr!s r5r%zExternalTraversal.traverses T668J8JKKr8cri}t|D]&}|jdst||||dd<(|S)Nr)dirrr)r2visitorsrXs r5_memoized_attr__visitor_dictz.ExternalTraversal._memoized_attr__visitor_dictsFI 9Dx(%,T4%8ab" 9r8c#>K|}|r|t|dd}|ryyw)z8Iterate through this visitor and each 'chained' visitor.rN)r)r2rs r5rz"ExternalTraversal.visitor_iterators(*.G7D)AscDt|jd}||_|S)z'Chain' an additional ExternalTraversal onto this ExternalTraversal The chained visitor will receive all visit events after this one. )listrr)r2r3tails r5rzExternalTraversal.chains% D))*2.  r8N)rr(r4rr9r)rOptional[ExternallyTraversible]r9Iterator[ExternallyTraversible]r Literal[None]r9r]rrr9rrr3r9r3)r9z%Dict[str, _TraverseCallableType[Any]])r9zIterator[ExternalTraversal])r2rr3r)r9r)r;r<r=r^r_rr`rr#rr%r-propertyrrr0r8r5r)r)s+I+-.- &&' 727 (777 $($ $$L2L (L .**r8c`eZdZdZdZ ddZed dZe d dZ d dZy) CloningExternalTraversalzBase class for visitor objects which can traverse using the :func:`.visitors.cloned_traverse` function. Direct usage of the :func:`.visitors.cloned_traverse` function is usually preferred. r0cJ|Dcgc]}|j|c}Scc}w)z`Apply cloned traversal to the given list of elements, and return the new list. )r%)r2list_xs r5copy_and_processz)CloningExternalTraversal.copy_and_processs!+00Q a 000s cyr/r0r!s r5r%z!CloningExternalTraversal.traverser#r8cyr/r0r!s r5r%z!CloningExternalTraversal.traverser%r8cDt||j|jSr')r&rrr!s r5r%z!CloningExternalTraversal.traverses$  **D,>,>  r8N)r=List[ExternallyTraversible]r9rCr5r7r8)r;r<r=r^r_r?rr%r0r8r5r;r;sjI101 $177 $($ $$ 2 ( r8r;c`eZdZdZdZ ddZed dZe d dZ d dZy) ReplacingExternalTraversalzBase class for visitor objects which can traverse using the :func:`.visitors.replacement_traverse` function. Direct usage of the :func:`.visitors.replacement_traverse` function is usually preferred. r0cy)aReceive pre-copied elements during a cloning traversal. If the method returns a new element, the element is used instead of creating a simple copy of the element. Traversal will halt on the newly returned element if it is re-encountered. Nr0)r2elems r5rz"ReplacingExternalTraversal.replacesr8cyr/r0r!s r5r%z#ReplacingExternalTraversal.traverse r#r8cyr/r0r!s r5r%z#ReplacingExternalTraversal.traverse r%r8cJ dfd }t|j|S)r(crjD]'}tt|j|}|%|cSyr/)rrrEr)rr4rer2s r5rz4ReplacingExternalTraversal.traverse..replacesA** 3Q7??H=H  r8)rrr4rr9r3)r'r)r2rrs` r5r%z#ReplacingExternalTraversal.traverses9 *  - $C)B)BGLLr8N)rGrr9r3r5r7r8)r;r<r=r^r_rrr%r0r8r5rErEsmI )  ( 77 $($ $$M2M (Mr8rEc#K|y||jdi|}|syt|g}|r@|j}|D]'}||j|jdi|)|r?yyw)aHTraverse the given expression structure, returning an iterator. Traversal is configured to be breadth-first. The central API feature used by the :func:`.visitors.iterate` function is the :meth:`_expression.ClauseElement.get_children` method of :class:`_expression.ClauseElement` objects. This method should return all the :class:`_expression.ClauseElement` objects which are associated with a particular :class:`_expression.ClauseElement` object. For example, a :class:`.Case` structure will refer to a series of :class:`_expression.ColumnElement` objects within its "whens" and "else\_" member variables. :param obj: :class:`_expression.ClauseElement` structure to be traversed :param opts: dictionary of iteration options. This dictionary is usually empty in modern usage. Nr0)rrpopleftr)roptschildrenstack t_iteratorts r5r#r#-s}0 { Is'$'H  8* E ]]_  1AG LL/$/ 0 1 s A)A.,A.cyr/r0iteratorrr,s r5r$r$V r8cyr/r0rUs r5r$r$^  r8cb|D])}|j|jd}|s"||+|S)aVisit the given expression structure using the given iterator of objects. :func:`.visitors.traverse_using` is usually called internally as the result of the :func:`.visitors.traverse` function. :param iterator: an iterable or sequence which will yield :class:`_expression.ClauseElement` structures; the iterator is assumed to be the product of the :func:`.visitors.iterate` function. :param obj: the :class:`_expression.ClauseElement` that was used as the target of the :func:`.iterate` function. :param visitors: dictionary of visit functions. See :func:`.traverse` for details on this dictionary. .. seealso:: :func:`.traverse` N)getr@)rVrr,rrPs r5r$r$fs::||F1148  L Jr8cyr/r0rrOr,s r5r%r%rWr8cyr/r0r]s r5r%r%rYr8c0tt||||S)aTraverse and visit the given expression structure using the default iterator. e.g.:: from sqlalchemy.sql import visitors stmt = select(some_table).where(some_table.c.foo == "bar") def visit_bindparam(bind_param): print("found bound value: %s" % bind_param.value) visitors.traverse(stmt, {}, {"bindparam": visit_bindparam}) The iteration of objects uses the :func:`.visitors.iterate` function, which does a breadth-first traversal using a stack. :param obj: :class:`_expression.ClauseElement` structure to be traversed :param opts: dictionary of iteration options. This dictionary is usually empty in modern usage. :param visitors: dictionary of visit functions. The dictionary should have strings as keys, each of which would correspond to the ``__visit_name__`` of a particular kind of SQL expression object, and callable functions as values, each of which represents a visitor function for that kind of object. )r$r#r]s r5r%r%sH '#t,c8 <.deferred_copy_internalsssD(33r8c|vr|St|vrd|vr2ttt|d|}||t|<|S|jddi|xt|<}|j ddi||j s&j|jd}|r||t|S)Nrcloner0) idrrrrr rr[r@)rGr4newelemrPrhclonedrdr,s r5rhzcloned_traverse..clone s 7?K$xv%?" !67It9LG*+2r$x(&.9T[[-Ku-K-KKr$x 7''':e:r: ))#<<(>(>EDW "T(# #r8Nrfr7rGrr4rr9r)setr[)rrOr,rfrhrkrds `` @@@r5r&r&soZ02F$((9b)*G4 "4 4 $$<   )@ DH  E Jr8cyr/r0rrOrs r5r'r'0rWr8cyr/r0ros r5r'r'8rbr8cyr/r0ros r5r'r'@rYr8cijdgDchc] }t|c} dfd }dfd | |fd|i}d|Scc}w)a]Clone the given expression structure, allowing element replacement by a given replacement function. This function is very similar to the :func:`.visitors.cloned_traverse` function, except instead of being passed a dictionary of visitors, all elements are unconditionally passed into the given replace function. The replace function then has the option to return an entirely new object which will replace the one given. If it returns ``None``, then the object is kept in place. The difference in usage between :func:`.visitors.cloned_traverse` and :func:`.visitors.replacement_traverse` is that in the former case, an already-cloned object is passed to the visitor function, and the visitor function can then manipulate the internal state of the object. In the case of the latter, the visitor function should only return an entirely different object, or do nothing. The use case for :func:`.visitors.replacement_traverse` is that of replacing a FROM clause inside of a SQL structure with a different one, as is a common use case within the ORM. rdct|Sr/)r'ros r5rfz5replacement_traverse..deferred_copy_internalsgs$Cw77r8c8t|vsd|jvr|S|}|jt||St|}|vrCd|vr|d|}|||<|S|jdi|x|<}|jddi||S)Nno_replacement_traverserrhr0)riraddrr )rGr4rjid_elemrhrkrrds r5rhz#replacement_traverse..clonels tH (D,=,==KdmG" BwK( T(&( B"/"Y-"5"..5F7O#*N0; 0Ab0AAF7Og+G++>%>2>g&r8Nrfr7rl)r[ri)rrOrr>rfrhrkrds `` @@@r5r'r'Hs~8F"hhy"56r!u6G8 "8 8 ''6   )@ DH  E JO7sAr\)rr3rOMapping[str, Any]r9r4)rVr rr6r,(Mapping[str, _TraverseCallableType[Any]]r9r])rVr rrr,ryr9r)rVr rr3r,ryr9r3)rr6rOrxr,ryr9r])rrrOrxr,ryr9r)rr3rOrxr,ryr9r3)rr rOrxr,ryr9r )rr6rOrxr#_TraverseTransformCallableType[Any]r9r])rrrOrxrrzr9r)rrrOrxrrzr9r)rr3rOrxrrzr9r3)Nr^ __future__r collectionsrenumrrrUrarrrr r r r r rrrrrrrrrrr util._has_cyr util.typingrrr annotationrelementsr _py_utilr!r"r+sqlalchemy.cyextension.util__all__r-r(r*r?rrrrExtendedInternalTraversalrrrr r_TraverseCallableTyperrr MemoizedSlotsr)r;rE Traversible ClauseVisitorCloningVisitorReplacingCloningVisitorr r#r$r%r&r'r0r8r5rs9"  *!"+' <4 NHNGGTPPfeC):$:;< 1 1 hMXML L ^. 7(88$0)$Be01 e/0 #-<<FXc]F 23G**GT$ 0$ N.M!9.Md ! )4 #oo&1 (&1 &1%&1R - 7    -  7     !-! (!7!% !H   7      7     $= ($= $=7$=% $=N   7     7  X (X X7X% Xv   1     1      1     D (D D1D% Dr8