|oiD>dZddlmZddlZddlmZddlmZgdZGdd eZ d Z Gd d ej e e Z Gd de ZGddeZGddeZdZdZdZdZdZdZdZdS)a@Visitor/traversal interface and library functions. SQLAlchemy schema and expression constructs rely on a Python-centric version of the classic "visitor" pattern as the primary way in which they apply functionality. The most common use of this pattern is statement compilation, where individual expression classes match up to rendering methods that produce a string result. Beyond this, the visitor system is also used to inspect expressions for various information and patterns, as well as for the purposes of applying transformations to expressions. Examples of how the visit system is used can be seen in the source code of for example the ``sqlalchemy.sql.util`` and the ``sqlalchemy.sql.compiler`` modules. Some background on clause adaption is also at http://techspot.zzzeek.org/2008/01/23/expression-transformations/ . )dequeN)exc)util) VisitableType Visitable ClauseVisitorCloningVisitorReplacingCloningVisitoriterateiterate_depthfirsttraverse_usingtraversetraverse_depthfirstcloned_traversereplacement_traversec"eZdZdZfdZxZS)rarMetaclass which assigns a ``_compiler_dispatch`` method to classes having a ``__visit_name__`` attribute. The ``_compiler_dispatch`` attribute becomes an instance method which looks approximately like the following:: def _compiler_dispatch (self, visitor, **kw): '''Look for an attribute named "visit_" + self.__visit_name__ on the visitor, and call it with the same kw params.''' visit_attr = 'visit_%s' % self.__visit_name__ return getattr(visitor, visit_attr)(self, **kw) Classes having no ``__visit_name__`` attribute will remain unaffected. c|dkrt|drt|tt||||dS)Nr__visit_name__)hasattr_generate_dispatchsuperr__init__)clsclsnamebasesclsdict __class__s N/opt/cloudlinux/venv/lib64/python3.11/site-packages/sqlalchemy/sql/visitors.pyrzVisitableType.__init__BsT k ! !gc3C&D&D ! s # # # mS!!**7E7CCCCC)__name__ __module__ __qualname____doc__r __classcell__)rs@rrr1sK DDDDDDDDDr rcdjvrYj}t|tjjrt jd|zfd}nfd}d|_|_ dSdS)zZReturn an optimized visit dispatch function for the cls for use by the compiler. rvisit_%sc |}||fi|S#t$r4}tjtj||Yd}~dSd}~wwxYw)Nreplace_context)AttributeErrorrraise_rUnsupportedCompilationError)selfvisitorkwmetherrrgetters r_compiler_dispatchz._generate_dispatch.._compiler_dispatchWs,!6'??D 4+++++ &K7EE(+s  A)AAcd|jz} t||}||fi|S#t$r4}tjt j||Yd}~dSd}~wwxYw)Nr'r))rgetattrr+rr,rr-)r.r/r0 visit_attrr1r2rs rr4z._generate_dispatch.._compiler_dispatchfs'$*== ,"7J77D 4+++++ &K7EE(+s& A$)AA$zLook for an attribute named "visit_" + self.__visit_name__ on the visitor, and call it with the same kw params. N) __dict__r isinstancercompat string_typesoperator attrgetterr$r4)r visit_namer4r3s` @rrrIs 3<''' j$+": ; ; ,(j)@AAF , , , , , , , , , , , ,&""4O('r ceZdZdZdS)rzBase class for visitable objects, applies the :class:`.visitors.VisitableType` metaclass. The :class:`.Visitable` class is essentially at the base of the :class:`_expression.ClauseElement` hierarchy. N)r!r"r#r$r rrrxsr rcdeZdZdZiZdZdZdZej dZ e dZ dZ dS) r zBase class for visitor objects which can traverse using the :func:`.visitors.traverse` function. Direct usage of the :func:`.visitors.traverse` function is usually preferred. c f|jD](}t|d|jzd}|r ||fi|cS)dS)Nr')visitor_iteratorr6r)r.objr0vr1s rtraverse_singlezClauseVisitor.traverse_singles^& ' 'A1j3+==tDDD 'tC2&&& ' ' 'r c,t||jS)zaTraverse the given expression structure, returning an iterator of all elements. )r __traverse_options__r.rDs rr zClauseVisitor.iterates sD5666r c8t||j|jS2Traverse and visit the given expression structure.)rrH _visitor_dictrIs rrzClauseVisitor.traversesT68JKKKr ci}t|D]2}|drt||||dd<3|S)Nvisit_)dir startswithr6)r.visitorsnames rrMzClauseVisitor._visitor_dictsRII 9 9Dx(( 9%,T4%8%8abb"r c#DK|}|r|Vt|dd}|dSdS)z8Iterate through this visitor and each 'chained' visitor._nextN)r6)r.rEs rrCzClauseVisitor.visitor_iteratorsM  *GGG7D))A * * * * *r cHt|jd}||_|S)z'Chain' an additional ClauseVisitor onto this ClauseVisitor. The chained visitor will receive all visit events after this one. )listrCrV)r.r/tails rchainzClauseVisitor.chains% D)**2.  r N)r!r"r#r$rHrFr rrmemoized_propertyrMpropertyrCr[r@r rr r s''' 777LLL  **X*r r ceZdZdZdZdZdS)r zBase 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. c fd|DS)z`Apply cloned traversal to the given list of elements, and return the new list. c:g|]}|Sr@)r).0xr.s r z3CloningVisitor.copy_and_process..s%000Q a  000r r@)r.list_s` rcopy_and_processzCloningVisitor.copy_and_processs 1000%0000r c8t||j|jSrK)rrHrMrIs rrzCloningVisitor.traverses# *D,>   r N)r!r"r#r$rerr@r rr r s<111     r r ceZdZdZdZdZdS)r zBase 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. cdS)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. Nr@)r.elems rreplacezReplacingCloningVisitor.replaces tr c:fd}t|j|S)rLcRjD]}||}||cSdS)N)rCrj)rirEer.s rrjz1ReplacingCloningVisitor.traverse..replacesA*  IIdOO=HHH!  r )rrH)r.rDrjs` rrz ReplacingCloningVisitor.traverses3      $C)BGLLLr N)r!r"r#r$rjrr@r rr r sA M M M M Mr r c$|jdi|}|s|gSt}t|g}|rP|}|||jdi|D]}|||Pt |S)arTraverse the given expression structure, returning an iterator. Traversal is configured to be breadth-first. The central API feature used by the :func:`.visitors.iterate` and :func:`.visitors.iterate_depthfirst` functions 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. r@) get_childrenrpopleftappenditer)rDoptschildren traversalstacktcs rr r s, s''$''H u I 3%LLE  MMOO''$''  A LLOOOO   ??r c$|jdi|}|s|gSt|g}t}|rP|}|||jdi|D]}|||Pt |S)aTraverse the given expression structure, returning an iterator. Traversal is configured to be depth-first. :param obj: :class:`_expression.ClauseElement` structure to be traversed :param opts: dictionary of iteration options. This dictionary is usually empty in modern usage. .. seealso:: :func:`.visitors.iterate` - includes a general overview of iteration. r@)rorpop appendleftrqrr)rDrsrtrvrurwrxs rr r s  s''$''H u 3%LLEI  IIKKQ''$''  A LLOOOO   ??r c`|D]*}||jd}|r ||+|S)aeVisit 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` or :func:`.visitors.traverse_depthfirst` functions. :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` or :func:`.visitors.iterate_depthfirst` functions. :param obj: the :class:`_expression.ClauseElement` that was used as the target of the :func:`.iterate` or :func:`.iterate_depthfirst` function. :param visitors: dictionary of visit functions. See :func:`.traverse` for details on this dictionary. .. seealso:: :func:`.traverse` :func:`.traverse_depthfirst` N)getr)iteratorrDrStargetr1s rrr5sD8||F1488   DLLL Jr c@tt||||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. )rr rDrsrSs rrrXs < '#t,,c8 < <.clones 7??K$xxv%%-1[[]]:r$xx 7''::e:r:::||G$:DAA"DMMM"T((# #r N)setr})rDrsrSrrrs `@@@rrrsn6F$((9b))**G $ $ $ $ $ $ $ $ eCjj E Jr c|id|dgDfd| |fi|}d|S)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. c,h|]}t|Sr@)r)rarbs r z'replacement_traverse..s666r!uu666r rct|vs d|jvr|S|}|$t||S|vr(|x|<}|jddi||S)Nno_replacement_traverserr@)r _annotationsaddrr)rir0rrrrjrs rrz#replacement_traverse..clones tHH  (D,===KgdmmG" BwKK(((v%%-1[[]]:F4L7+G+>>%>2>>>d|#r N)r})rDrsrjrrrs `@@@rrrs~0F66dhhy"55666G$$$$$$$$" eC  4   E Jr )r$ collectionsrr<rr__all__typerrwith_metaclassobjectrr r r r r rrrrrr@r rrs$    DDDDDDDDD0,4,4,4^##M6::77777F777t     ]   2MMMMMnMMM