U aά@sddlZddlZddlZddlZddlZddlmZddlmZddlmZddlm Z ddlm Z ddlm Z dd lm Z dd l mZdd l mZdd lmZd dlmZd dlmZd dlmZd dlmZd dlmZedZGdddeZd+ddZGdddejZGdddeZGdddejeZGdd d ejejZ Gd!d"d"eZ!Gd#d$d$e"Z#Gd%d&d&e"Z$Gd'd(d(eZ%e&ed)d*Z'dS),N) coercions)elements)roles)schema) traversals)type_api)visitors)_clone)Options)ColumnOperators)exc) inspection)util)collections_abc)compatic@s$eZdZdZdZdZdZdZdZdS) LambdaOptionsTN) __name__ __module__ __qualname__enable_trackingtrack_closure_variablestrack_onglobal_track_bound_valuestrack_bound_values lambda_cacherrWC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-nyjtotrf\sqlalchemy\sql\lambdas.pyr!s rTc Cst|tjt||||||dS)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` )rrrrrr)StatementLambdaElementr StatementRoler)lmbrrrrrrrrr lambda_stmt*s;r"c@seZdZdZdZdZdejjfgZ dZ dZ ddZ e dfd d Zd d Zd dZeddZeddZeddZddZddZedfddZejddZddZdd ZdS)! LambdaElementaA 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 _resolvedrNcCsd|jj|jjfS)Nz%s(%r)) __class__rfn__code__selfrrr__repr__szLambdaElement.__repr__cCs\||_||_|jf|_||_|dkr2|tjkr2|}||||}|dk rX|j}|rX||_ dSN) r'roler( tracker_keyoptsrr _retrieve_tracker_recpropagate_attrs_propagate_attrs)r*r'r-r/apply_propagate_attrsrecr1rrr__init__s zLambdaElement.__init__c s^j}|dkrt}|j}|j}|jt||}g|_t t fdd|j D}|j dk rx|j j |}||_ z|||}Wntk rd}YnX|dkrt||||}|_||||<nddt|jDdd<|j dk r |j jdd<||_|} | dk rZ| j}|jrP|j} |jD]}|| j| q:| j } q|S)Ncsg|]}|qSrr).0getteranon_map bindparamsclosurer/rr sz7LambdaElement._retrieve_tracker_rec..cSs g|]\}}|j|jddqS)TZ maintain_key) _with_valuevalue)r6Z orig_bindZnew_bindrrrr<sr)r_closure_per_cache_keyr.r' __closure__ AnalyzedCodeget_resolved_bindparamsrr9tupleclosure_trackers parent_lambdaclosure_cache_keyKeyErrorAnalyzedFunctionclosure_bindparamszip_recbindparam_trackerstracker_instrumented_fn) r*r'r3r/rr.tracker cache_keyr4r$rOrr8rr0sf        z#LambdaElement._retrieve_tracker_reccCst|jj|Sr,)getattrrM expected_exprr*keyrrr __getattr__szLambdaElement.__getattr__cCs|jjSr,)rM is_sequencer)rrr _is_sequenceszLambdaElement._is_sequencecCs*|jrtjdd|jDS|jjSdS)NcSsg|] }|jqSr)_select_iterabler6elementrrrr<sz2LambdaElement._select_iterable..)rX itertoolschain from_iterabler%rYr)rrrrYs zLambdaElement._select_iterablecCs*|jrtjdd|jDS|jjSdS)NcSsg|] }|jqSr) _from_objectsrZrrrr<sz/LambdaElement._from_objects..)rXr\r]r^r%r_r)rrrr_s zLambdaElement._from_objectscCsdd|jDS)NcSsi|]}|j|jqSr)rUr?r6brrr sz-LambdaElement._param_dict..)rDr)rrr _param_dictszLambdaElement._param_dictcsVdd|jDfdd|jjr8fdd|D}nt|ddrRt|i}|S) NcSsi|] }|j|qSr)rUr`rrrrbsz?LambdaElement._setup_binds_for_tracked_expr..csDt|tjr@|jkr@|j}|jr.replacecsg|]}t|iqSr)r replacement_traverser6Zsub_expr)rjrrr<sz?LambdaElement._setup_binds_for_tracked_expr..is_clause_elementF)rDrMrWrRr rkr*exprr)rirjr_setup_binds_for_tracked_exprs   z+LambdaElement._setup_binds_for_tracked_exprcKs||jfd|i||_dS)Ndeferred_copy_internals)r%r*clonerqkwrrr_copy_internalsszLambdaElement._copy_internalscCs|jj}|jr||}|Sr,)rMrSrDrprnrrrr%)s zLambdaElement._resolvedcCsT|jj|jf|j}|j}|dk r>|jjf|j|}|j}q|jrP||j|Sr,)r'r(r&rHrGrDextend)r*r9r:rQparentrrr_gen_cache_key2s zLambdaElement._gen_cache_keycGs|Sr,rr*r'argrrr_invoke_user_fnEszLambdaElement._invoke_user_fn)rrr__doc____visit_name___is_lambda_elementr InternalTraversaldp_clauseelement_traverse_internals _transformsrGr+rr5r0rVpropertyrXrYr_rcrpr rurZmemoized_propertyr%rxr{rrrrr#ss8  A     r#csHeZdZdZedffdd ZddZddZed ffd d Z Z 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. rcs||_tt||||dSr,) lambda_argssuperrr5)r*r'r-r/rr&rrr5SszDeferredLambdaElement.__init__cGs ||jSr,)rryrrrr{Wsz%DeferredLambdaElement._invoke_user_fncGs@|jj}||}t|j|}||}|jD] }||}q.|Sr,)rMrOrexpectr-rpr)r*rZ tracker_fnrorqrrr_resolve_with_argsZs   z(DeferredLambdaElement._resolve_with_argsNc s.tt|j|||d|r*|j|f7_dS)N)rsrqr/)rrrurrrrrrru}s z%DeferredLambdaElement._copy_internals) rrrr|rr5r{rr ru __classcell__rrrrrIs $rc@sVeZdZdZddZdddZdd Zed d Zed d Z eddZ ddZ dS)ra`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` cCs ||Sr,) add_criteria)r*otherrrr__add__szStatementLambdaElement.__add__TNcCs*|jt|||jj||d}t|||dS)aAdd 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. )rrrrr)rGr/)r/dictrLinkedLambdaElement)r*rrrrrr/rrrrs*z#StatementLambdaElement.add_criteriacCs(|jjjr|||||St|dSr,)rMrSsupports_execution_execute_clauseelementrObjectNotExecutableErrorr* connectionZ multiparamsparamsZexecution_optionsrrr_execute_on_connections z-StatementLambdaElement._execute_on_connectioncCs |jjjSr,)rMrS _with_optionsr)rrrrsz$StatementLambdaElement._with_optionscCs |jjjSr,)rMrS_effective_plugin_targetr)rrrrsz/StatementLambdaElement._effective_plugin_targetcCs |jjjSr,)rMrS_execution_optionsr)rrrrsz)StatementLambdaElement._execution_optionscCs t|S)ztReturn a new :class:`.StatementLambdaElement` that will run all lambdas unconditionally each time. )NullLambdaStatementr'r)rrrspoilszStatementLambdaElement.spoil)TNTT) rrrr|rrrrrrrrrrrrrs 4    rc@sNeZdZdZdZdZdejjfgZ ddZ ddZ d d Z d d Z d dZdS)rzProvides 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. r$Tr%cCs||_|j|_dSr,)r%r2)r* statementrrrr5szNullLambdaStatement.__init__cCs t|j|Sr,)rRr%rTrrrrVszNullLambdaStatement.__getattr__cCs||j}t|Sr,r%r)r*rrrrrrs zNullLambdaStatement.__add__cKs||j}t|Sr,r)r*rrtrrrrrs z NullLambdaStatement.add_criteriacCs&|jjr|||||St|dSr,)r%rrrrrrrrrsz*NullLambdaStatement._execute_on_connectionN)rrrr|r}r~r rrrr5rVrrrrrrrrs  rc@s$eZdZdZdZddZddZdS)rzARepresent subsequent links of a :class:`.StatementLambdaElement`.NcCs<||_||_||_|j|jf|_|||||j|_dSr,)r/r'rGr.r(r0r2)r*r'rGr/rrrr5/s zLinkedLambdaElement.__init__cGs ||jjSr,)rGr%ryrrrr{8sz#LinkedLambdaElement._invoke_user_fn)rrrr|r-r5r{rrrrr*s rc@seZdZdZeZeddZddZ ddZ dd Z d d Z d d Z eddZddZddZddZdddZdddZddZdS) rB)rrrNrFbuild_py_wrapperscKsDz|j|jWStk r"YnXt|||f||j|j<}|Sr,)_fnsr(rIrB)clsr'r$Z lambda_kwrtZanalyzedrrrrCFszAnalyzedCode.getcCs|j}|jo|j|_|j}|j}|j}|o.| |_g|_g|_g|_|rn|rV| || ||rn| || |||dSr,) rArrrrrrNrFr_init_track_on _init_globals _init_closure"_setup_additional_closure_trackers)r*r'r$r/r;rrrrrrr5Rs"     zAnalyzedCode.__init__cs"jfddt|DdS)Nc3s|]\}}||VqdSr,)_cache_key_getter_track_on)r6idxelemr)rr vsz.AnalyzedCode._init_track_on..)rFrv enumerate)r*rrr)rruszAnalyzedCode._init_track_oncCsl|j}|j}|j}|jjD]L}||jkr*q||j|}t|r| |df|r| | |qdSr,) rrNrr(co_names __globals___roll_down_to_literalr_deep_is_literalappend$_bound_parameter_getter_func_globals)r*r'rrNrname _bound_valuerrrr{s   zAnalyzedCode._init_globalsc Cs|j}|j}|j}|j}|j}|j}tt|jj |D]d\}\} } | | j } t | r~|| |f|r||| |q6|r6|||| || j q6dSr,)rrArrrNrFrrLr( co_freevarsr cell_contentsrrr$_bound_parameter_getter_func_closure"_cache_key_getter_closure_variable) r*r'rr;rrrNrF closure_indexfvcellrrrrrs8    zAnalyzedCode._init_closurecCs<t||d|}|j}|jD]}|js||||qdSr,)rJrFclosure_pywrappersZ_sa__has_paramr!_cache_key_getter_tracked_literal)r*r'r$r/Zanalyzed_functionrF pywrapperrrrrs  z/AnalyzedCode._setup_additional_closure_trackerscCst|d}|rJt|tjtjtfsJz |}Wqtk rFYqJYqXq|st j |dd}|dk rz |WStk r|YSX|S|SdS)N__clause_element__FZraiseerr) hasattrrdr ClauseElementrZ SchemaItemrfrAttributeErrorrinspect)rr[rminsprrrrs&      z"AnalyzedCode._roll_down_to_literalcsfdd}|S)zReturn a getter that will extend a list of bound parameters with new entries from the ``__globals__`` collection of a particular lambda. cs&|j}t|d|j|dSN_extract_bound_parameters)robject__getattribute__Z current_fnrOresultwrapperrrrextract_parameter_values   zRAnalyzedCode._bound_parameter_getter_func_globals..extract_parameter_valuer)r*rrrrrrs z1AnalyzedCode._bound_parameter_getter_func_globalscsfdd}|S)zReturn a getter that will extend a list of bound parameters with new entries from the ``__closure__`` collection of a particular lambda. cs*|jj}t|d|jj|dSr)rArrrrrrrrs  zRAnalyzedCode._bound_parameter_getter_func_closure..extract_parameter_valuer)r*rrrrrrrs z1AnalyzedCode._bound_parameter_getter_func_closurecsBt|trfdd}n&t|tjr2fdd}n fdd}|S)zReturn a getter that will extend a cache key with new entries from the "track_on" parameter passed to a :class:`.LambdaElement`. cstfdd|jDS)Nc3s|]}|VqdSr,rx)r6Ztup_elemr9r:rrr szGAnalyzedCode._cache_key_getter_track_on..get..)rErr;r/r9r:rrrrC sz4AnalyzedCode._cache_key_getter_track_on..getcs|j||Sr,)rrxrrrrrCscs |jSr,)rrrrrrCs)rdrEr HasCacheKey)r*rrrCrrrrs    z'AnalyzedCode._cache_key_getter_track_onFc st|tjrfdd}nt|tjr8fdd}nt|tjrXfdd}n|}d} t|drd} t|dds| }q`qq`| st j |dd } | d k r̈j | dd Snj |dd S |S) zReturn a getter that will extend a cache key with new entries from the ``__closure__`` collection of a particular lambda. csJ|j}rt|}n$r>t|dr>t|dds|}q|||S)NrrmF)rrrrrRrrx)r;r/r9r:obj)ruse_clause_element use_inspectrrrC.s     z.getcs |jjSr,)rr(rrrrrC<sc s\|j}ztfdd|DWStk rV}zj|dW5d}~XYnXdS)Nc3s|]}|VqdSr,r)r6rrrrrEszOAnalyzedCode._cache_key_getter_closure_variable..get..from_)rrEr'_raise_for_uncacheable_closure_variable)r;r/r9r:contentsZae)r'rr* variable_namerrrCAs  FrTrmrN)r)r)rdrrtypes FunctionTyperSequencerrRrrrrr) r*r'rrrrrrCr[rmrr)r'rr*rrrrrs@       z/AnalyzedCode._cache_key_getter_closure_variableNcCs"tjtd||jf|ddS)NaSClosure variable named '%s' inside of lambda callable %s does not refer to a cachable 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=[] 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)rZraise_rInvalidRequestErrorr()r*rr'rrrrrks z4AnalyzedCode._raise_for_uncacheable_closure_variablecCs"|j}|j}|j}|||||S)aReturn 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. )Z_sa__to_evaluateZ_sa__closure_indexZ _sa__namer)r*r'Z pytrackerrrrrrrrs z.AnalyzedCode._cache_key_getter_tracked_literal)FF)N)rrr __slots__weakrefWeakKeyDictionaryr classmethodrCr5rrrrrrrrrrrrrrrrB<s( ##   N rBc@s0eZdZdZddZddZddZdd Zd S) rJ) analyzed_coder'rrOrorNrSrWr1rKcCs.||_||_|j|_|||||dSr,)rr'rN_instrument_and_run_function_coerce_expression)r*rr$r3r'rrrr5s  zAnalyzedFunction.__init__cs|j}|j}g|_}|j}|s8||_}|||_n|j}|j}|rbddt |j j |Dni|j } |D]f\} } | dk r|| j} t|| | | |jjd| <} |r|| qt|j | } t|| | | | <} qt||fdd|j j D| |_}|||_dS)NcSsi|]\}}||jqSr)r)r6rrrrrrbszAAnalyzedFunction._instrument_and_run_function..)rrcsg|] }|qSrr)r6rZ new_closurerrr<szAAnalyzedFunction._instrument_and_run_function..)rr'rrrOr{rorrArLr(rrcopyr PyWrapperrr_rewrite_code_obj)r*r$rr'rrrOrr;Z new_globalsrrr?rhrrrrsJ         z-AnalyzedFunction._instrument_and_run_functioncsj}|j}|dkrZt|tjr>fdd|D|_d|_qftjj |d|_d|_n ||_d|_dk rxj |_ nt j |_ 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. Ncsg|]}tjj|dqS)r3)rrr-rlr3r$rrr<s z7AnalyzedFunction._coerce_expression..TrF)rGrordrrrSrWrrr-r2r1r EMPTY_DICT)r*r$r3rGrorrrrs&    z#AnalyzedFunction._coerce_expressionc stt}d}rD|dddd|Dddd|Df7}|d7}|ddd d|D7}|d 7}fd d |D}t||||d }t||j||j|j|}t j dkr|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 (%s) = (%s) z, css|]}d|VqdSzi%dNrr6irrrr*sz5AnalyzedFunction._rewrite_code_obj..css|]}d|VqdS)o%dNrrrrrr+sz def closure(): z return %s css|]}d|VqdSrrrrrrr.sz return closure.__closure__csi|]}d||qS)rrr cell_valuesrrrb0sz6AnalyzedFunction._rewrite_code_obj..Z make_cells))rangelenjoinrexec_rfr(r __defaults__sys version_info__annotations____kwdefaults__r|r) r*frglobals_ZargrangecodeZvars_r;funcrrrrs4   z"AnalyzedFunction._rewrite_code_objN)rrrrr5rrrrrrrrJs  D'rJc@sreZdZdZdddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZdS)raA 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. NTcCs:||_||_||_d|_d|_i|_||_||_||_dS)NF) r'_name _to_evaluate_param _has_param _bind_paths_getterZ_closure_indexr)r*r'r to_evaluaterr7rrrrr5Rs zPyWrapper.__init__cOs^t|d}|||}|jrVt|rVt|tjsVt|d}t d||j j fn|SdS)Nrra#Can't invoke Python callable %s() inside of lambda expression argument at %s; 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.) rrZ_sa_track_bound_valuesrrrdrrrr_sa_fnr()r*rzrtrr?rrrr__call__es$    zPyWrapper.__call__cOst|d}||f||SNrrrr*oprkwargsrrrroperateszPyWrapper.operatecKst|d}|||f|Sr r r rrrreverse_operateszPyWrapper.reverse_operatecCsft|d}|dk r,|j|dd}||t|dD]$}t|d}||}|||qrvaluesZ_sa__extract_bound_parameters)r*starting_pointZ result_listparamrr7r[rrrrs   z#PyWrapper._extract_bound_parameterscCsbt|d}t|d}|dkrTt|d}tj|ddd|_}d|_t||_|j |ddS)NrrrFT)requireduniquer=) rrrrerrrZ_resolve_value_to_typerfr>)r*rrrrrrrs     zPyWrapper.__clause_element__cCst|d}t|SNrrrboolr*rrrr__bool__s zPyWrapper.__bool__cCst|d}t|Srrrrrr __nonzero__s zPyWrapper.__nonzero__cCsd|drt||ddS|dkr2t||S|drRt|d}t||S||tjSdS)NZ_sa_)rrrr&__dict____r) startswithrrrR_sa__add_getteroperator attrgetterr*rUrrrrrs     zPyWrapper.__getattribute__cCst|d}t|Sr)rriter)r*rrrr__iter__s zPyWrapper.__iter__cCs@t|d}t|dstdt|tr2td||t j S)Nr __getitem__zUDictionary keys / list indexes inside of a cached lambda must be Python literals only) rrrrrdrrrrr  itemgetterr"rrrr%s   zPyWrapper.__getitem__c Cszt|d}||f}||kr$||S||}t|d}||}t|}t|rrt|j|||d} | ||<| S|SdS)Nrr)r7)rrrBrrrrr) r*rUZ getter_fnZ bind_pathsZ bind_path_keyr7rr?Zrolled_down_valuerrrr _add_getters    zPyWrapper._add_getter)NNT)rrrr|r5rrrrrrrrr$r%r'rrrrr@s     rcCs t|jSr,)rrr%)r!rrrrsr)TTNTTN)(r\r rrrrrrrrrr baser r operatorsr rrrrrZLRUCacher@rr"rr#rZAllowsLambdaRolerrrrrBrJrZ _inspectsrrrrrsV                  IWCo/[,$