� k\�g,���*�UddlmZddlmZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddl m Z ddl mZddl mZddl mZdd l mZdd l mZdd l mZdd l mZdd l mZddl mZddlZddlmZddlmZddlmZddlmZddlmZddlmZddl m!Z!ddl m"Z"ddl m#Z#ddlm$Z$ddl%m&Z&ddlm'Z'ddlm(Z(ddlm)Z)ddl*m+Z+erdd lm,Z,dd!lm-Z-dd"lm.Z.dd#lm/Z/eee d$fed%fZ0e d$e fZ1e)j2d&��Z3d'e4d(<e ge fZ5e d$e fZ6e ge fZ7ed)e"�*��Z8e e8ge fZ9Gd+�d,e#��Z: dNdOd;�Z;Gd<�d=ej-��Z<Gd>�d?e<��Z=Gd@�d:ej>e<e"��Z?GdA�dBej>ej-��Z@GdC�dDe?��ZAGdE�dF��ZBGdG�dH��ZCGdI�dJ��ZDGdK�dLe&��ZEe(jFe<��dM���ZGdS)P�)� annotationsN)�CodeType)�Any)�Callable)�cast)�List)�MutableMapping)�Optional)�Tuple)�Type)� TYPE_CHECKING)�TypeVar)�Union�)� cache_key)� coercions)�elements)�roles)�schema)�visitors)�_clone)� Executable)�Options)� CacheConst)�ColumnOperators�)�exc)� inspection)�util)�Literal)� BindParameter)� ClauseElement)�SQLRole)�_CloneCallableType.)�NonAnalyzedFunction�AnalyzedFunctioni��_LambdaCacheType�_closure_per_cache_key�_E)�boundc�<�eZdZUdZdZdZded<dZdZdZ ded<dS)� LambdaOptionsTN�Optional[object]�track_on�Optional[_LambdaCacheType]� lambda_cache) �__name__� __module__� __qualname__�enable_tracking�track_closure_variablesr.�__annotations__�global_track_bound_values�track_bound_valuesr0���f/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sqlalchemy/sql/lambdas.pyr,r,HsK��������O�"��!%�H�%�%�%�%� $����/3�L�3�3�3�3�3�3r:r,T�lmb�_StmtLambdaTyper4�boolr5r.r-r7r8r0r/�return�StatementLambdaElementc �^�t|tjt||||||�����S)ahProduce a SQL statement that is cached as a lambda. The Python code object within the lambda is scanned for both Python literals that will become bound parameters as well as closure variables that refer to Core or ORM constructs that may vary. The lambda itself will be invoked only once per particular set of constructs detected. E.g.:: from sqlalchemy import lambda_stmt stmt = lambda_stmt(lambda: table.select()) stmt += lambda s: s.where(table.c.id == 5) result = connection.execute(stmt) The object returned is an instance of :class:`_sql.StatementLambdaElement`. .. versionadded:: 1.4 :param lmb: a Python function, typically a lambda, which takes no arguments and returns a SQL expression construct :param enable_tracking: when False, all scanning of the given lambda for changes in closure variables or bound parameters is disabled. Use for a lambda that produces the identical results in all cases with no parameterization. :param track_closure_variables: when False, changes in closure variables within the lambda will not be scanned. Use for a lambda where the state of its closure variables will never change the SQL structure returned by the lambda. :param track_bound_values: when False, bound parameter tracking will be disabled for the given lambda. Use for a lambda that either does not produce any bound values, or where the initial bound values never change. :param global_track_bound_values: when False, bound parameter tracking will be disabled for the entire statement including additional links added via the :meth:`_sql.StatementLambdaElement.add_criteria` method. :param lambda_cache: a dictionary or other mapping-like object where information about the lambda's Python code as well as the tracked closure variables in the lambda itself will be stored. Defaults to a global LRU cache. This cache is independent of the "compiled_cache" used by the :class:`_engine.Connection` object. .. seealso:: :ref:`engine_lambda_caching` )r4r.r5r7r8r0)r@r� StatementRoler,)r<r4r5r.r7r8r0s r;� lambda_stmtrCQsD��v "� � ���+��$;�&?�1�%�  � � � � � r:c�<�eZdZUdZdZdZdejjfgZ dZ de d<de d <d Z d e d <d e d<de d<de d<de d<de d<d�Z ed fd3d�Zd�Zd�Zed ���Zed!���Zed"���Zd#�Zd$�Zed fd4d-�Zejd.���Zd/�Zd5d2�Zd S)6� LambdaElementa�A SQL construct where the state is stored as an un-invoked lambda. The :class:`_sql.LambdaElement` is produced transparently whenever passing lambda expressions into SQL constructs, such as:: stmt = select(table).where(lambda: table.c.col == parameter) The :class:`_sql.LambdaElement` is the base of the :class:`_sql.StatementLambdaElement` which represents a full statement within a lambda. .. versionadded:: 1.4 .. seealso:: :ref:`engine_lambda_caching` �lambda_elementT� _resolvedr9zTuple[_CloneCallableType, ...]� _transformszList[BindParameter[Any]]�_resolved_bindparamsNz Optional[StatementLambdaElement]� parent_lambdaz4Union[Tuple[Any, ...], Literal[CacheConst.NO_CACHE]]�closure_cache_key� Type[SQLRole]�rolez,Union[AnalyzedFunction, NonAnalyzedFunction]�_rec�_AnyLambdaType�fnzTuple[CodeType, ...]� tracker_keyc�:�|jj�d|jj�d�S)N�(�))� __class__r1rP�__code__��selfs r;�__repr__zLambdaElement.__repr__�s(�� �N� #� #� #� �G� � � � � r:� _LambdaType�opts�)Union[Type[LambdaOptions], LambdaOptions]�apply_propagate_attrs�Optional[ClauseElement]c���||_||_|jf|_||_|�|t jur|}|�|||��}|�|j}|r ||_ dSdSdS�N) rPrMrVrQr[rrB�_retrieve_tracker_rec�propagate_attrs�_propagate_attrs)rXrPrMr[r]�recrbs r;�__init__zLambdaElement.__init__�s�������� ��K�>����� � � (�d�e�6I�.I�.I�$(� !��(�(��-B�D�I�I�� � ,�!�1�O�� I�9H�%�6�6�6� -� ,� I� Ir:c�X�� ����j}|�t}|j}|j}|j�t �||���}gx|_�|j� |jj }nd}|tj ur~tj ��� t� ���fd�|jD����}tj � vr+||z}||_ |||z} n/#t $rd} Yn!wxYwtj }d} ntj }d} ||_ | ��|tj urbt j5||z} | |vr,t%||||��} t'���| _| || <n|| } ddd��n #1swxYwYnGt+|�|����} n$d�t/| j���D���dd�<| |_|tj urV|j�|jj�dd�<|} | �:| j} | jr#| j} | jD]}|| j| ����| j} | �:| S)Nr9c�,��g|]}|��������Sr9r9)�.0�getter�anon_map� bindparams�closurer[s ����r;� <listcomp>z7LambdaElement._retrieve_tracker_rec.<locals>.<listcomp>�s9��������F�7�D�(�J�?�?���r:c�L�g|]!\}}|�|jd�����"S)T�� maintain_key)� _with_value�value)rh� orig_bind�new_binds r;rmz7LambdaElement._retrieve_tracker_rec.<locals>.<listcomp>!sA�����'�I�x��%�%�h�n�4�%�H�H���r:r)r0r(rQrP� __closure__� AnalyzedCode�getrIrJrK� _cache_key�NO_CACHErrj�tuple�closure_trackers�KeyError�_generation_mutexr&�list�closure_bindparamsr%�_invoke_user_fn�ziprN�bindparam_trackers�tracker_instrumented_fn)rXrPr]r[r0rQ�tracker�parent_closure_cache_keyrrd�keyrFr�rjrkrls ` @@@r;raz#LambdaElement._retrieve_tracker_rec�s-�������(� � � �1�L��&� � �W���.���"�"� � � � � ��24�3��!�J� � � )�'+�'9�'K� $� $�')� $� $�:�+>� >� >��(�*�*�H���������")�":������I��"�(�2�2�4�y�@� �)2��&��&�{�Y�'>�?�C�C�������C�C�C�����'�/� ����#�+�I��C�!*��� �;�� � 3�3�3�!�3� 0� 0�%� �1�C��,�.�.�.�#�T�+@�"����26�j�1A�1A��.�,/� �S�)�)�*�3�/�� 0� 0� 0� 0� 0� 0� 0� 0� 0� 0� 0���� 0� 0� 0� 0��*�$�*>�*>�r�*B�*B�C�C�����+.��*�J�,�,����J�q�q�q�M��� � �J�/� /� /��!�-�!%�!3�!H� �2�A�2��6:�N� �,�$�)���)��.1�.I�+�#&�#9������*�-�3�&����� "0�!=��!�,�� s$� C� C�C�">E,�,E0�3E0c�6�t|jj|��Sr`)�getattrrN� expected_expr�rXr�s r;� __getattr__zLambdaElement.__getattr__=s���t�y�.��4�4�4r:c��|jjSr`)rN� is_sequencerWs r;� _is_sequencezLambdaElement._is_sequence@s ���y�$�$r:c��|jr.tj�d�|jD����S|jjS)Nc��g|] }|j�� Sr9)�_select_iterable�rh�elements r;rmz2LambdaElement._select_iterable.<locals>.<listcomp>Hs��H�H�H�g��)�H�H�Hr:)r�� itertools�chain� from_iterablerGr�rWs r;r�zLambdaElement._select_iterableDsG�� � � 3��?�0�0�H�H���H�H�H��� � �>�2� 2r:c��|jr.tj�d�|jD����S|jjS)Nc��g|] }|j�� Sr9)� _from_objectsr�s r;rmz/LambdaElement._from_objects.<locals>.<listcomp>Rs��E�E�E�7��&�E�E�Er:)r�r�r�r�rGr�rWs r;r�zLambdaElement._from_objectsNsG�� � � 0��?�0�0�E�E�d�n�E�E�E��� � �>�/� /r:c�$�d�|jD��S)Nc�(�i|]}|j|j��Sr9)r�rr�rh�bs r;� <dictcomp>z-LambdaElement._param_dict.<locals>.<dictcomp>Ys��B�B�B�1���q�w�B�B�Br:)rIrWs r;� _param_dictzLambdaElement._param_dictXs��B�B��(A�B�B�B�Br:c����d�|jD���d �fd� �|jjr�fd�|D��}n't|d d ��rt j|i���}|S) Nc��i|] }|j|�� Sr9)r�r�s r;r�z?LambdaElement._setup_binds_for_tracked_expr.<locals>.<dictcomp>\s��H�H�H��A�E�1�H�H�Hr:r��(Optional[visitors.ExternallyTraversible]�kwrr?c���t|tj��r>|j�vr5�|j}|jrd|_|j|_|j|_|SdS�NT)� isinstancerr!r�� expanding� expand_op�type)r�r��bind�bindparam_lookups �r;�replacez<LambdaElement._setup_binds_for_tracked_expr.<locals>.replace^sc����'�8�#9�:�:� ��;�"2�2�2�+�G�K�8�D��(�1�)-���)0�):���$+�L�� ��K��4r:c�<��g|]}tj|i�����Sr9)r�replacement_traverse)rh�sub_exprr�s �r;rmz?LambdaElement._setup_binds_for_tracked_expr.<locals>.<listcomp>ms8��������-�h��G�D�D���r:�is_clause_elementF)r�r�r�rr?r�)rIrNr�r�rr�)rX�exprr�r�s @@r;�_setup_binds_for_tracked_exprz+LambdaElement._setup_binds_for_tracked_expr[s�����H�H�d�.G�H�H�H�� � � � � � � �9� � D����� $����D�D��T�.�� 6� 6� D��0��r�7�C�C�D�� r:�cloner$�deferred_copy_internals�Optional[_CloneCallableType]r�rr?�Nonec �0�||jfd|i|��|_dS)Nr�)rG)rXr�r�r�s r;�_copy_internalszLambdaElement._copy_internalsvs7���� �N� � �$;� �� � ����r:c�V�|jj}|jr|�|��}|Sr`)rNr�rIr��rXr�s r;rGzLambdaElement._resolved�s/���y�&�� � $� <��5�5�d�;�;�D�� r:c�H�|jtjurd|tj<dS|jj|jf|jz}|j}|�8|jtjusJ�|j}|jjf|z|z}|j}|�8|jr|� |j��|Sr�) rKrxryrPrVrUrJrrI�extend)rXrjrkr�parentr�s r;�_gen_cache_keyzLambdaElement._gen_cache_key�s��� � !�Z�%8� 8� 8�,0�H�Z�(� )��4� �G� � �N� � � "�#� � �#��� ��+�:�3F�F�F�F�F��(� %� ��#�%�(@�@�9�L� ��)�F�� � � $� 9� � � �d�7� 8� 8� 8��r:�argr"c��|��Sr`r9�rXrPr�s r;r�zLambdaElement._invoke_user_fn�s ���r�t�t� r:)rPrZrMrLr[r\r]r^)r�r$r�r�r�rr?r�)rPrOr�rr?r")r1r2r3�__doc__�__visit_name__�_is_lambda_elementr�InternalTraversal�dp_clauseelement�_traverse_internalsrHr6rJrYr,rerar��propertyr�r�r�r�r�rr�r�memoized_propertyrGr�r�r9r:r;rErE�s����������&&�N��� �h�0�A�B���35�K�4�4�4�4�2�2�2�2�6:�M�:�:�:�:�K�K�K�K�����6�6�6�6�����%�%�%�%� � � �;H�9=� I�I�I�I�I�,_�_�_�B5�5�5��%�%��X�%��3�3��X�3��0�0��X�0�C�C�C����:%+�@D�  �  �  �  �  � ����������8�����r:rEc�H��eZdZdZedfd�fd � Zd �Zdd�Zedf�fd� Z �xZ S)�DeferredLambdaElementa>A LambdaElement where the lambda accepts arguments and is invoked within the compile phase with special context. This lambda doesn't normally produce its real SQL expression outside of the compile phase. It is passed a fixed set of initial arguments so that it can generate a sample expression. r9rPrOrM�Type[roles.SQLRole]r[r\� lambda_args�Tuple[Any, ...]c�\��||_t���|||��dSr`)r��superre)rXrPrMr[r�rUs �r;rezDeferredLambdaElement.__init__�s/���'��� ������T�4�(�(�(�(�(r:c��||j�Sr`)r�r�s r;r�z%DeferredLambdaElement._invoke_user_fn�s���r�4�#�$�$r:rr?r"c���t|jt��sJ�|jj}||�}t j|j|��}|�|��}|jD] }||��}�|Sr`) r�rNr&r�r�expectrMr�rH)rXr�� tracker_fnr�r�s r;�_resolve_with_argsz(DeferredLambdaElement._resolve_with_args�s����$�)�%5�6�6�6�6�6��Y�6� ��z�;�'����� �4�0�0���1�1�$�7�7��0(,�'7� 1� 1� #�*�*�4�0�0�D�D�� r:Nc �z��t���|||���|r|xj|fz c_dSdS)N)r�r�r[)r�r�rH)rXr�r�r�rUs �r;r�z%DeferredLambdaElement._copy_internals�sa��� ������$;�� � � � � #� ;� � � �!8� :� :� � � � � ;� ;r:)rPrOrMr�r[r\r�r�)r�rr?r") r1r2r3r�r,rer�r�rr�� __classcell__)rUs@r;r�r��s����������;H�')� )�)�)�)�)�)�)�%�%�%�"�"�"�"�J�D� ;� ;� ;� ;� ;� ;� ;� ;� ;� ;r:r�c�@�eZdZdZeredfd'd �Zd(d�Z d)d*d�Zd�Z e d+d���Z e d���Z e d���Z e d���Ze d���Ze d���Ze d ���Ze d!���Ze d"���Ze d#���Ze d$���Zd,d&�ZdS)-r@a_Represent a composable SQL statement as a :class:`_sql.LambdaElement`. The :class:`_sql.StatementLambdaElement` is constructed using the :func:`_sql.lambda_stmt` function:: from sqlalchemy import lambda_stmt stmt = lambda_stmt(lambda: select(table)) Once constructed, additional criteria can be built onto the statement by adding subsequent lambdas, which accept the existing statement object as a single parameter:: stmt += lambda s: s.where(table.c.col == parameter) .. versionadded:: 1.4 .. seealso:: :ref:`engine_lambda_caching` NrPr=rMrLr[r\r]r^c��dSr`r9)rXrPrMr[r]s r;rezStatementLambdaElement.__init__s �� �3r:�other�_StmtLambdaElementType[Any]r?c�,�|�|��Sr`)� add_criteria)rXr�s r;�__add__zStatementLambdaElement.__add__s��� � ��'�'�'r:Tr4r>r.� Optional[Any]r5r8c�r�|jt|||jj||���z}t|||���S)a(Add new criteria to this :class:`_sql.StatementLambdaElement`. E.g.:: >>> def my_stmt(parameter): ... stmt = lambda_stmt( ... lambda: select(table.c.x, table.c.y), ... ) ... stmt = stmt.add_criteria(lambda: table.c.x > parameter) ... return stmt The :meth:`_sql.StatementLambdaElement.add_criteria` method is equivalent to using the Python addition operator to add a new lambda, except that additional arguments may be added including ``track_closure_values`` and ``track_on``:: >>> def my_stmt(self, foo): ... stmt = lambda_stmt( ... lambda: select(func.max(foo.x, foo.y)), ... track_closure_variables=False, ... ) ... stmt = stmt.add_criteria(lambda: self.where_criteria, track_on=[self]) ... return stmt See :func:`_sql.lambda_stmt` for a description of the parameters accepted. )r4r5r7r.r8)rJr[)r[�dictr7�LinkedLambdaElement)rXr�r4r.r5r8r[s r;r�z#StatementLambdaElement.add_criteria sM��J�y�4�+�$;�&*�i�&I��1�  � � � ��#�5��4�H�H�H�Hr:c���tr!t|jjt��sJ�|jjjr|�|||��Stj|���r`) r r�rNr�r"�supports_execution�_execute_clauseelementr�ObjectNotExecutableError�rX� connection�distilled_params�execution_optionss r;�_execute_on_connectionz-StatementLambdaElement._execute_on_connectionOsl�� � F��d�i�5�}�E�E� E� E�E� �9� "� 5� 5��4�4��&�(9��� ��.�t�4�4� 4r:rc��|jSr`)�_rec_expected_exprrWs r;�_proxiedzStatementLambdaElement._proxied[s ���&�&r:c��|jjSr`)r�� _with_optionsrWs r;r�z$StatementLambdaElement._with_options_s ���}�*�*r:c��|jjSr`)r��_effective_plugin_targetrWs r;r�z/StatementLambdaElement._effective_plugin_targetcs ���}�5�5r:c��|jjSr`)r��_execution_optionsrWs r;r�z)StatementLambdaElement._execution_optionsgs ���}�/�/r:c��|jjSr`)r��_all_selected_columnsrWs r;r�z,StatementLambdaElement._all_selected_columnsks ���}�2�2r:c��|jjSr`)r�� is_selectrWs r;r�z StatementLambdaElement.is_selecto� ���}�&�&r:c��|jjSr`)r�� is_updaterWs r;r�z StatementLambdaElement.is_updatesr�r:c��|jjSr`)r�� is_insertrWs r;r�z StatementLambdaElement.is_insertwr�r:c��|jjSr`)r��is_textrWs r;r�zStatementLambdaElement.is_text{s ���}�$�$r:c��|jjSr`)r�� is_deleterWs r;rz StatementLambdaElement.is_deleter�r:c��|jjSr`)r��is_dmlrWs r;rzStatementLambdaElement.is_dml�s ���}�#�#r:�NullLambdaStatementc�D�t|�����S)ztReturn a new :class:`.StatementLambdaElement` that will run all lambdas unconditionally each time. )rrPrWs r;�spoilzStatementLambdaElement.spoil�s�� #�4�7�7�9�9�-�-�-r:)rPr=rMrLr[r\r]r^)r�r�r?r@)TNTT) r�r�r4r>r.r�r5r>r8r>r?r@)r?r)r?r)r1r2r3r�r r,rer�r�r�r�r�r�r�r�r�r�r�r�r�rrrr9r:r;r@r@�s���������0�� ?L�=A�  � � � � �(�(�(�(�!%�"&�(,�#'� -I�-I�-I�-I�-I�^ 5� 5� 5��'�'�'��X�'��+�+��X�+��6�6��X�6��0�0��X�0��3�3��X�3��'�'��X�'��'�'��X�'��'�'��X�'��%�%��X�%��'�'��X�'��$�$��X�$�.�.�.�.�.�.r:c�V�eZdZdZdZdZdejjfgZ d�Z d�Z d�Z d�Z d �Zd S) rz�Provides the :class:`.StatementLambdaElement` API but does not cache or analyze lambdas. the lambdas are instead invoked immediately. The intended use is to isolate issues that may arise when using lambda statements. rFTrGc�,�||_|j|_dSr`)rGrc)rX� statements r;rezNullLambdaStatement.__init__�s��"��� )� :����r:c�,�t|j|��Sr`)r�rGr�s r;r�zNullLambdaStatement.__getattr__�s���t�~�s�+�+�+r:c�@�||j��}t|��Sr`�rGr)rXr�r s r;r�zNullLambdaStatement.__add__��!���E�$�.�)�)� �"�9�-�-�-r:c �@�||j��}t|��Sr`r )rXr�r�r s r;r�z NullLambdaStatement.add_criteria�r r:c�p�|jjr|�|||��Stj|���r`)rGr�r�rr�r�s r;r�z*NullLambdaStatement._execute_on_connection�sE�� �>� ,� 5��4�4��&�(9��� ��.�t�4�4� 4r:N)r1r2r3r�r�r�rr�r�r�rer�r�r�r�r9r:r;rr�s���������&�N��� �h�0�A�B���;�;�;�,�,�,�.�.�.� .�.�.� 5�5�5�5�5r:rc�,�eZdZUdZded<d d�Zd �Zd S) r�zARepresent subsequent links of a :class:`.StatementLambdaElement`.r@rJrPr�r[r\c��||_||_||_|j|jfz|_|�|||��|j|_dSr`)r[rPrJrQrVrarc)rXrPrJr[s r;rezLinkedLambdaElement.__init__�sW�� �� ����*���(�4�� �~�E��� �"�"�2�t�T�2�2�2� -� >����r:c�,�||jj��Sr`)rJrGr�s r;r�z#LinkedLambdaElement._invoke_user_fn�s���r�$�$�.�/�/�/r:N)rPr�rJr@r[r\)r1r2r3r�r6rer�r9r:r;r�r��sK�������K�K�)�)�)�)� ?� ?� ?� ?�0�0�0�0�0r:r�c���eZdZUdZej��Zded<ej ��Z e d���Z d�Z d�Zd�Zd�Zd �Ze d ���Zd �Zd �Zd �Z dd�Z dd�Zd�ZdS)rv)r5r8r�r{�build_py_wrappersz1weakref.WeakKeyDictionary[CodeType, AnalyzedCode]�_fnsc �"� |j|jS#t$rYnwxYw|j5|j|jvr|j|jcddd��St |||fi|��x|j|j<}|cddd��S#1swxYwYdSr`)rrVr|r}rv)�clsrPrF� lambda_kwr��analyzeds r;rwzAnalyzedCode.get�s"�� ��8�B�K�(� (��� � � � �D� ����� "� � ��{�c�h�&�&��x�� �,� � � � � � � � � 0<��N�I�0�0�13�0�0� �C�H�R�[� !�H�� � � � � � � � � � � � ���� � � � � � s#�� !�!� B�B�B� Bc��tj|��rtjd|z���|j}|jo|j|_|j}|j}|j }|o| |_ g|_ g|_ g|_ |rC|r|� |��|�|��|r|�|��|�|||��dS)Nz/Method %s may not be passed as a SQL expression)�inspect�ismethodr� ArgumentErrorrur8r7r4r.r5r�r{r�_init_track_on� _init_globals� _init_closure�"_setup_additional_closure_trackers)rXrPrFr[rlr4r.r5s r;rezAnalyzedCode.__init__�s �� � �B� � � ��#�A�B�F��� ��.�� � #� F��(F� ���.���=��"&�">��'>�'O�x�<��$� #%��� !#���!#��� � '�� .��#�#�H�-�-�-� � � �r� "� "� "�� '��"�"�2�&�&�&� �/�/��N�D�I�I�I�I�Ir:c�n���j��fd�t|��D����dS)Nc3�J�K�|]\}}��||��V��dSr`)�_cache_key_getter_track_on)rh�idx�elemrXs �r;� <genexpr>z.AnalyzedCode._init_track_on.<locals>.<genexpr>sK�����% �% ���T� � +� +�C�� 6� 6�% �% �% �% �% �% r:)r{r�� enumerate)rXr.s` r;rzAnalyzedCode._init_track_onsV��� ��$�$�% �% �% �% �&�x�0�0�% �% �% � � � � � r:c�L�|j}|j}|j}|jjD]�}||jvr� |�|j|��}tj|��rA|� |df��|r(|� |� |������dSr`) rr�r8rV�co_names� __globals__�_roll_down_to_literalr�_deep_is_literal�append�$_bound_parameter_getter_func_globals)rXrPrr�r8�name� _bound_values r;rzAnalyzedCode._init_globals#s��� �2��!�4��!�4���K�(� � �D��2�>�)�)���5�5�b�n�T�6J�K�K�L��)�,�7�7� �!�(�(�$���6�6�6�%��&�-�-��A�A�$�G�G����� � r:c ��|j}|j}|j}|j}|j}|j}t t|jj |����D]�\}\} } |� | j ��} tj | ��rC|�| |f��|r)|�|�| |�����y|r0|�|�|| || j ������dSr`)rrur8r5r�r{r(r�rV� co_freevarsr,� cell_contentsrr-r.�$_bound_parameter_getter_func_closure�"_cache_key_getter_closure_variable) rXrPrrlr8r5r�r{� closure_index�fv�cellr1s r;r zAnalyzedCode._init_closure5s3�� �2���.��!�4��"&�">��!�4���0��)2� �� �'�� 1� 1�* �* � � � %�M�:�B�� �5�5�d�6H�I�I�L��)�,�7�7� �!�(�(�"�m�)<�=�=�=�%��&�-�-��A�A�� �������+��$�+�+��?�?���M�4�3E�������) � r:c��t||d|��}|j}|jD]2}|js)|�|�||�����3dSr`)r&r{�closure_pywrappers�_sa__has_paramr.�!_cache_key_getter_tracked_literal)rXrPrFr[�analyzed_functionr{� pywrappers r;r!z/AnalyzedCode._setup_additional_closure_trackersXs~��-� � � � �  � �� �0��*�=� � �I��+� � �'�'��:�:�2�y�I�I����� � r:c��t|d��}|r~t|tjtjt f��sR |���}n#t$rYn0wxYwt|tjtjt f���R|sAtj |d���}|�' |���S#t$r|cYSwxYw|S|S)N�__clause_element__F��raiseerr) �hasattrr�rr"r� SchemaItemr�rA�AttributeErrorrr)rr�r��insps r;r,z"AnalyzedCode._roll_down_to_literalns ��#�G�-A�B�B�� � � ��(�0�&�2C�T�J��� ��%�8�8�:�:�G�G��%�����E����� !��(�0�&�2C�T�J��� �!� ��%�g��>�>�>�D��� ��2�2�4�4�4��%� � � ��K�K�K� �����N��Ns$�A� A"�!A"�,C� C�Cc����fd�}|S)z�Return a getter that will extend a list of bound parameters with new entries from the ``__globals__`` collection of a particular lambda. c���|j�}t�|d��|j�|��dS�N�_extract_bound_parameters)r+�object�__getattribute__)� current_fnr��result�wrapperr0s �r;�extract_parameter_valuezRAnalyzedCode._bound_parameter_getter_func_globals.<locals>.extract_parameter_value�sL���.�9�$�?�G� � #� #�G�-H� I� I��&�t�,�f� � � � � r:r9)rXr0rQs ` r;r/z1AnalyzedCode._bound_parameter_getter_func_globals�s$��� � � � � �'�&r:c����fd�}|S)z�Return a getter that will extend a list of bound parameters with new entries from the ``__closure__`` collection of a particular lambda. c���|j�j}t�|d��|j�j|��dSrJ)rur4rLrM)rNr�rOrPr7s �r;rQzRAnalyzedCode._bound_parameter_getter_func_closure.<locals>.extract_parameter_value�sZ���.�9���� � � #� #�G�-H� I� I��&�}�5�C�V� � � � � r:r9)rXr0r7rQs ` r;r5z1AnalyzedCode._bound_parameter_getter_func_closure�s$��� � � � � �'�&r:c���t|t��r�fd�}n%t|tj��r�fd�}n�fd�}|S)z�Return a getter that will extend a cache key with new entries from the "track_on" parameter passed to a :class:`.LambdaElement`. c�V����t��fd�|j�D����S)Nc3�D�K�|]}|�����V��dSr`�r�)rh�tup_elemrjrks ��r;r'zGAnalyzedCode._cache_key_getter_track_on.<locals>.get.<locals>.<genexpr>�sG������� ��+�+�H�j�A�A������r:)rzr.�rlr[rjrkr%s ``�r;rwz4AnalyzedCode._cache_key_getter_track_on.<locals>.get�sI�����������$(�M�#�$6������r:c�F��|j��||��Sr`)r.r�rYs �r;rwz4AnalyzedCode._cache_key_getter_track_on.<locals>.get�s ����}�S�)�8�8��:�N�N�Nr:c���|j�Sr`)r.rYs �r;rwz4AnalyzedCode._cache_key_getter_track_on.<locals>.get�s����}�S�)�)r:)r�rzrx� HasCacheKey)rXr%r&rws ` r;r$z'AnalyzedCode._cache_key_getter_track_on�s���� �d�E� "� "� *� � � � � � � ��j�4� 5� 5� *� O� O� O� O� O� O�  *� *� *� *� *�� r:Fc�@�������t|tj��r���fd�}n�t|tj��r�fd�}n�t|t j��r ����fd�}n�|}d} t|d��r9d} t|dd��s|� ��}nnt|d���9| s3tj |d���} | ��� ���| d� ��Sn�� ���|d� ��S�� ����|S) z�Return a getter that will extend a cache key with new entries from the ``__closure__`` collection of a particular lambda. c���|�j}�rtj|��}nG�rEt|d��r5t |dd��s|���}t|d���5|�||��S)NrAr�F)r4rrrDr�rAr�)rlr[rjrk�objr%�use_clause_element� use_inspects ���r;rwz<AnalyzedCode._cache_key_getter_closure_variable.<locals>.get�s�����c�l�0���;�$�,�S�1�1�C�C�'�;�!�#�';�<�<�;�&�s�,?��G�G�;�"%�"8�"8�":�":�C�"�#�';�<�<�;��)�)�(�J�?�?�?r:c�(��|�jjSr`)r4rVrYs �r;rwz<AnalyzedCode._cache_key_getter_closure_variable.<locals>.get�s����s�|�1�:�:r:c�����|�j} t��fd�|D����S#t$r#}��� �|���Yd}~dSd}~wwxYw)Nc3�D�K�|]}|�����V��dSr`rW)rhr&rjrks ��r;r'zOAnalyzedCode._cache_key_getter_closure_variable.<locals>.get.<locals>.<genexpr>�sG�����!�!� ��+�+�H�j�A�A�!�!�!�!�!�!r:)�from_)r4rzrF�'_raise_for_uncacheable_closure_variable) rlr[rjrk�contents�aerPr%rX� variable_names `` ����r;rwz<AnalyzedCode._cache_key_getter_closure_variable.<locals>.get�s������"�3�<�5��� �!�!�!�!�!�$,�!�!�!�����&�����@�@�%�r��A�����������������s�.� A�A�AFrATr�rBN)ra)r`)r�rxr\�types� FunctionType�collections_abc�SequencerDr�rArrr6rf) rXrPrir%r4r`rarwr�r�rGs ```` `` r;r6z/AnalyzedCode._cache_key_getter_closure_variable�s��������� �m�Z�%;� <� <�: L� @� @� @� @� @� @� @� @�� �u�'9� :� :�- L� ;� ;� ;� ;� ;� ;�� ��'?� @� @�( L� � � � � � � � � �$$�G� %� ��'�#7�8�8� �$(�!��w�(;�U�C�C��%�8�8�:�:�G�G�� �'�#7�8�8� �%� �!�)�'�E�B�B�B���#��B�B��M�3��$�C����$� �>�>�� �s�G��?���� � 8� 8��� K� K� K�� r:Nc�D�tjd|�d|j�d���|�)NzClosure variable named 'z' inside of lambda callable a does not refer to a cacheable SQL element, and also does not appear to be serving as a SQL literal bound value based on the default SQL expression returned by the function. This variable needs to remain outside the scope of a SQL-generating lambda so that a proper cache key may be generated from the lambda's state. Evaluate this variable outside of the lambda, set track_on=[<elements>] to explicitly select closure elements to track, or set track_closure_variables=False to exclude closure variables from being part of the cache key.)r�InvalidRequestErrorrV)rXrirPres r;rfz4AnalyzedCode._raise_for_uncacheable_closure_variables7���%�%��}�}�b�k�k�k� +� � �� r:c�\�|j}|j}|j}|�||||��S)a�Return a getter that will extend a cache key with new entries from the ``__closure__`` collection of a particular lambda. this getter differs from _cache_key_getter_closure_variable in that these are detected after the function is run, and PyWrapper objects have recorded that a particular literal value is in fact not being interpreted as a bound parameter. )�_sa__to_evaluate�_sa__closure_index� _sa__namer6)rXrP� pytrackerr&r7ris r;r=z.AnalyzedCode._cache_key_getter_tracked_literal%s=���)��!�4� �!�+� ��6�6� � �}�d� � � r:)FFr`)r1r2r3� __slots__�weakref�WeakKeyDictionaryrr6� threading�RLockr}� classmethodrwrerrr r!r,r/r5r$r6rfr=r9r:r;rvrv�sS��������I� "��!�#�#� �����(� ��)�)������[��$%J�%J�%J�N � � � ���$!�!�!�F���,����[��2'�'�'�"'�'�'�&���@!��J�J�J�J�Z(,�����& � � � � r:rvc�^�eZdZUdZdZded<dZded<dZded <d d �Ze dd ���Z dS)r%�r�N�"Optional[List[BindParameter[Any]]]r�%Optional[List[_BoundParameterGetter]]r�Fr"r�c��||_dSr`r|r�s r;rezNonAnalyzedFunction.__init__Cs ���� � � r:r?c��|jSr`r|rWs r;r�z!NonAnalyzedFunction.expected_exprFs ���y�r:)r�r")r?r") r1r2r3rurr6r�r�rer�r�r9r:r;r%r%9s���������I�=A��A�A�A�A�@D��D�D�D�D��K��������������X���r:r%c�J�eZdZUdZded<ded<ded<d�Zd �Zd �Zd �Zd S) r&) � analyzed_coderPr;r�r�r�r�r�rbrr}rz)Union[ClauseElement, List[ClauseElement]]r�r~r�c��||_||_|j|_|�|��|�||��dSr`)r�rPr��_instrument_and_run_function�_coerce_expression)rXr�rFr]rPs r;rezAnalyzedFunction.__init__]sO��+������"/�"B��� �)�)�.�9�9�9� ����0E�F�F�F�F�Fr:c���|j}|j}gx|_}|j}|s%|x|_}|�|��|_dS|j}|j}|r%d�t|j j |��D���ni�|j � ��} |D]r\} } | �H|| j} t|| | | |jj���x�| <} |r|�| ���O|j | } t|| | ��x| | <} �s|�|�fd�|j j D��| ��x|_}|�|��|_dS)Nc�$�i|] \}}||j��Sr9)r4)rhr8r9s r;r�zAAnalyzedFunction._instrument_and_run_function.<locals>.<dictcomp>~s1����� ��D���*���r:)r7r8c� ��g|] }�|�� Sr9r9)rhr0� new_closures �r;rmzAAnalyzedFunction._instrument_and_run_function.<locals>.<listcomp>�s���K�K�K�4�[��&�K�K�Kr:)r�rPr;rr�r�r�r5rur�rVr3r+�copyr4� PyWrapperr8r.�_rewrite_code_obj)rXrFr�rPr;rr�r5rl� new_globalsr0r7rrr�r�s @r;r�z-AnalyzedFunction._instrument_and_run_functionms�����*� � �W��79�9���"4�)�;�� �: P�EG� G�D� (�+B�&�6�6�7N�O�O�D�I�I�I�&3�&K� #��n�G�� !���$'�� �(?��$I�$I���� � � !� ��.�-�-�/�/�K�'8� J� J�#��m� �,�#�M�2�@�E�/8����&3� �.�A� 0�0�0��K��%��/�8�*�1�1�$�7�7�7���N�4�0�E�/8��T�5�/I�/I�I�K��%��� �&�&��K�K�K�K�2�;�3J�K�K�K���� �D� (�+B�&'�6�6�7N�O�O�D�I�I�Ir:c�b����j}|j}|�mt|tj��r��fd�|D��|_d|_nEtdtj �j |������|_d|_n||_d|_���j |_ dStj|_ dS)a,Run the tracker-generated expression through coercion rules. After the user-defined lambda has been invoked to produce a statement for re-use, run it through coercion rules to both check that it's the correct type of object and also to coerce it to its useful form. Nc �d��g|],}tdtj�j|��������-S)r"�r])rrr�rM)rhr�r]rFs ��r;rmz7AnalyzedFunction._coerce_expression.<locals>.<listcomp>�sY��� &� &� &�!��'�!�(�*�/�$�2G������ &� &� &r:Tr"r�F)rJr�r�rlrmr�r�rrr�rMrcrbr� EMPTY_DICT)rXrFr]rJr�s `` r;r�z#AnalyzedFunction._coerce_expression�s�����'�4� ��y�� � ��$�� 8�9�9� )� &� &� &� &� &�%)� &� &� &��"�$(�� � �%)�#��$�&�+��.C����&�&��"�$)�� � �!%�D� �$�D� � � ,�#8�#I�D� � � �#'�?�D� � � r:c�F��tt�����}d}�rF|dd�d�|D�����dd�d�|D�����d�z }|dz }|d d�d �|D����zz }|d z }�fd �|D��}t|||��|d ��}t |��|j||j|j|��}|j|_|j |_ |j |_ |j |_ |S)zaReturn a copy of f, with a new closure and new globals yes it works in pypy :P zdef make_cells(): z (z, c3� K�|] }d|zV�� dS�zi%dNr9�rh�is r;r'z5AnalyzedFunction._rewrite_code_obj.<locals>.<genexpr>��&����6�6��%�!�)�6�6�6�6�6�6r:z) = (c3� K�|] }d|zV�� dS)�o%dNr9r�s r;r'z5AnalyzedFunction._rewrite_code_obj.<locals>.<genexpr>�r�r:z) z def closure(): z return %s c3� K�|] }d|zV�� dSr�r9r�s r;r'z5AnalyzedFunction._rewrite_code_obj.<locals>.<genexpr>�s&����1N�1N��%�!�)�1N�1N�1N�1N�1N�1Nr:z return closure.__closure__c�(��i|]}d|z�|��S)r�r9)rhr�� cell_valuess �r;r�z6AnalyzedFunction._rewrite_code_obj.<locals>.<dictcomp>�s#���=�=�=�q����K��N�=�=�=r:� make_cells) �range�len�join�execr�rVr1� __defaults__r6�__kwdefaults__r�r2) rX�fr��globals_�argrange�code�vars_rl�funcs ` r;r�z"AnalyzedFunction._rewrite_code_obj�sW�����[�)�)�*�*��$�� � � �D�� � �6�6�X�6�6�6�6�6�6�6�� � �6�6�X�6�6�6�6�6�6�6�� �D� �&�&�� �%�� � �1N�1N�X�1N�1N�1N�(N�(N�N�N�� �0�0��=�=�=�=�H�=�=�=�� �T�5�%� � � �%�%� �%�'�'���t�A�w�w� �J��!�*�a�n�g� � �� !�0����.����y�� ��,���� r:N) r1r2r3rur6rer�r�r�r9r:r;r&r&Ks�������� �I�;�:�:�:�<�<�<�<�=�=�=�=�G�G�G� BP�BP�BP�H+3�+3�+3�Z����r:r&c�^�eZdZdZ dd�Zd�Zd�Zd�Zd�Zdd �Z d �Z d �Z d �Z d �Z d�ZdS)r�a�A wrapper object that is injected into the ``__globals__`` and ``__closure__`` of a Python function. When the function is instrumented with :class:`.PyWrapper` objects, it is then invoked just once in order to set up the wrappers. We look through all the :class:`.PyWrapper` objects we made to find the ones that generated a :class:`.BindParameter` object, e.g. the expression system interpreted something as a literal. Those positions in the globals/closure are then ones that we will look at, each time a new lambda comes in that refers to the same ``__code__`` object. In this way, we keep a single version of the SQL expression that this lambda produced, without calling upon the Python function that created it more than once, unless its other closure variables have changed. The expression is then transformed to have the new bound values embedded into it. NTc��||_||_||_d|_d|_i|_||_||_||_dS)NF) rP�_name� _to_evaluate�_param� _has_param� _bind_paths�_getter�_closure_indexr8)rXrPr0� to_evaluater7rir8s r;rezPyWrapper.__init__sM������� �'����� ��������� �+���"4����r:c�6�t�|d��}||i|��}|jrntj|��rZt |t j��s@t�|d��}tj d|�d|j j �d����|S)Nr�r�zCan't invoke Python callable z+() inside of lambda expression argument at a�; lambda SQL constructs should not invoke functions from closure variables to produce literal values since the lambda SQL system normally extracts bound values without actually invoking the lambda or any functions within it. Call the function outside of the lambda and assign to a local variable that is used in the lambda as a closure variable, or set track_bound_values=False if the return value of this function is used in some other way other than a SQL bound value.) rLrM�_sa_track_bound_valuesrr-r�rxr\rro�_sa_fnrV)rXr�r�r&rrr0s r;�__call__zPyWrapper.__call__$s����&�&�t�^�<�<����c� �R� � �� � '� ��*�5�1�1� ����&��� ��*�*�4��9�9�D��)�)�!�D�D�$�+�"6�"6�"6� 8��� � �Lr:c�^�t�|d����}||g|�Ri|��S�N�_py_wrapper_literal�rLrM�rX�opr��kwargsr&s r;�operatezPyWrapper.operateCs>���&�&�t�-B�C�C�E�E���r�$�)��)�)�)�&�)�)�)r:c �Z�t�|d����}|||fi|��Sr�r�r�s r;�reverse_operatezPyWrapper.reverse_operateGs8���&�&�t�-B�C�C�E�E���r�%��(�(��(�(�(r:c�p�t�|d��}|�,|�|d���}|�|��t�|d�����D]>}t�|d��}||��}|�||���?dS)Nr�Tror�r�)rLrMrqr.�values�_sa__extract_bound_parameters)rX�starting_point� result_list�paramr?rir�s r;rKz#PyWrapper._extract_bound_parametersKs����'�'��h�7�7�� � ��%�%�n�4�%�H�H�E� � � �u� %� %� %��0�0��}�E�E�L�L�N�N� J� J�I��,�,�Y� �B�B�F��f�^�,�,�G� � 3� 3�G�[� I� I� I� I� J� Jr:c �6�t�|d��}t�|d��}|�Kt�|d��}tj|dd||�|jnd���x|_}d|_|�|d���S)Nr�r�r�FT)�required�unique�_compared_to_operator�_compared_to_typero)rLrMrr!r�r�r�rq)rXr��operatorr�r�r�r0s r;r�zPyWrapper._py_wrapper_literalUs����'�'��h�7�7���-�-�d�N�C�C� � �=��*�*�4��9�9�D�"*�"8����&.�/3�/?�$�)�)�T� #�#�#� �D�K�%�#�D�O�� � ��4� �@�@�@r:c�V�t�|d��}t|��S�Nr�)rLrMr>)rXr�s r;�__bool__zPyWrapper.__bool__ds%���-�-�d�N�C�C� ��K� � � r:c�p�|�d��r#t�||dd���S|dvrt�||��S|�d��r+t�|d��}t||��S|�|t j��S)N�_sa_�)rAr�r�r�rU�__dict__�__r�)� startswithrLrMr��_sa__add_getterr�� attrgetter�rXr�r&s r;rMzPyWrapper.__getattribute__hs��� �>�>�&� !� !� 6��*�*�4��Q�R�R��9�9� 9� � � � ��*�*�4��5�5� 5� �>�>�$� � � B��*�*�4��@�@�D��4��%�%� %��'�'��X�-@�A�A� Ar:c�V�t�|d��}t|��Sr�)rLrM�iter)rXr&s r;�__iter__zPyWrapper.__iter__{s#���&�&�t�^�<�<���D�z�z�r:c��t�|d��}t|d��std���t |t ��rt jd���|�|tj ��S)Nr�� __getitem__zUDictionary keys / list indexes inside of a cached lambda must be Python literals only) rLrMrDrFr�r�rror�r�� itemgetterr�s r;r�zPyWrapper.__getitem__s~���&�&�t�^�<�<���t�]�+�+� 0� ��/�/� /� �c�9� %� %� ��)�6��� ��#�#�C��)<�=�=�=r:c�X�t�|d��}||f}||vr||S||��}t�|d��}||��}t�|��}t j|��rt |j|||���} | ||<| S|S)Nr�r�)ri)rLrMrvr,rr-r�r�) rXr�� getter_fn� bind_paths� bind_path_keyrir&rr�rolled_down_valuerPs r;� _add_getterzPyWrapper._add_getter�s����,�,�T�=�A�A� ��i�(� � �J� &� &��m�,� ,���3�����&�&�t�^�<�<����t� � ��(�>�>�u�E�E�� � %�&7� 8� 8� ��� �S�%��G�G�G�G�(/�J�}� %��N��Lr:)NNT)NN)r1r2r3r�rer�r�r�rKr�r�rMr�r�r�r9r:r;r�r��s���������,���5�5�5�5�&���>*�*�*�)�)�)�J�J�J� A� A� A� A�!�!�!�B�B�B�&��� >� >� >�����r:r�c�4�tj|j��Sr`)rrrG)r<s r;rGrG�s�� � �c�m� ,� ,�,r:)TTNTTN)r<r=r4r>r5r>r.r-r7r>r8r>r0r/r?r@)H� __future__r�collections.abc�abcrlrr�r�rxrjr�typingrrrrr r r r r rrrv�rrxrrrrr�baserrrr� operatorsrrrr� util.typingr r!r"r#r$r'�_BoundParameterGetter�LRUCacher(r6rZrOr=r)�_StmtLambdaElementTyper,rCrEr��AllowsLambdaRoler@rr�rvr%r&r�� _inspectsrGr9r:r;�<module>r�s���#�"�"�"�"�"�"�)�)�)�)�)�)����������������� � � � �������������������������������!�!�!�!�!�!������������������� � � � � � �����������������%�%�%�%�%�%�������������������������������������������������!�!�!�!�!�!�&�&�&�&�&�&�������������������!�!�!�!�!�!��-�'�'�'�'�'�'�'�'�'�'�'�'�������,�,�,�,�,�,�!� �#�s�(�O�U�D�E�E���!��c��*��+8�4�=��+>�+>��>�>�>�>��r�3�w�� ��#�s�(�#���2�s�7�#�� �W�T��$�$�$��!�2�$��)�,��4�4�4�4�4�G�4�4�4�!�$(�!%�&*�#�/3�F�F�F�F�F�RO�O�O�O�O�H�*�O�O�O�dG;�G;�G;�G;�G;�M�G;�G;�G;�TV.�V.�V.�V.�V.� ��M�:�V.�V.�V.�r,5�,5�,5�,5�,5�%�0�(�2H�,5�,5�,5�^0�0�0�0�0�0�0�0�0�.a �a �a �a �a �a �a �a �H ��������$q�q�q�q�q�q�q�q�h_�_�_�_�_��_�_�_�D���m�$�$�-�-�%�$�-�-�-r:
Memory