o
    y¬´\6L  ã                   @   s0  d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZ ddlmZ e e¡ZG d
d„ deƒZG dd„ deƒZG dd„ deƒZe dd¡dd„ ƒZe dd¡dd„ ƒZe	 ¡ dd„ ƒZeje dd¡dd„ ƒƒZeje dd¡dd„ ƒƒZejZej Zej!Z!dS )zÎBaked query extension.

Provides a creational pattern for the :class:`.query.Query` object which
allows the fully constructed object, Core select statement, and string
compiled result to be fully cached.


é    Né   )Úexc)Úutil)Ústrategy_options)ÚQuery)Úfunc)Úliteral_columnc                   @   s$   e Zd ZdZdZdd„ Zdd„ ZdS )ÚBakeryzêCallable which returns a :class:`.BakedQuery`.

    This object is returned by the class method
    :meth:`.BakedQuery.bakery`.  It exists as an object
    so that the "cache" can be easily inspected.

    .. versionadded:: 1.2


    ©ÚclsÚcachec                 C   s   || _ || _d S ©Nr
   )ÚselfÚcls_r   © r   úF/var/www/html/env/lib/python3.10/site-packages/sqlalchemy/ext/baked.pyÚ__init__-   s   
zBakery.__init__c                 G   s   |   | j||¡S r   r
   )r   Ú
initial_fnÚargsr   r   r   Ú__call__1   s   zBakery.__call__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   r   r   r   r   r   r	      s
    r	   c                   @   s¼   e Zd ZdZdZd-dd„Zed.dd	„ƒZd
d„ Zd-dd„Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd/dd„Zdd„ Zd0dd „Zd0d!d"„Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ ZdS )1Ú
BakedQueryz3A builder object for :class:`.query.Query` objects.)ÚstepsÚ_bakeryÚ
_cache_keyÚ_spoiledr   c                 C   s*   d| _ |  ||¡ |g| _d| _|| _d S )Nr   F)r   Ú_update_cache_keyr   r   r   )r   Úbakeryr   r   r   r   r   r   :   s
   
zBakedQuery.__init__éÈ   Nc                 C   s   t | tj||d�ƒS )zSConstruct a new bakery.

        :return: an instance of :class:`.Bakery`

        )Ú
size_alert)r	   r   ÚLRUCache)r   ÚsizeÚ_size_alertr   r   r   r!   A   s   zBakedQuery.bakeryc                 C   s2   t  t ¡}| j|_t| jƒ|_| j|_| j|_|S r   )r   Ú__new__r   Úlistr   r   r   )r   Úb1r   r   r   Ú_cloneK   s   
zBakedQuery._clonec                 C   s   |  j |jf| 7  _ d S r   )r   Ú__code__©r   Úfnr   r   r   r   r    S   s   zBakedQuery._update_cache_keyc                 C   s&   t |tƒr| j|Ž  | S |  |¡ | S r   )Ú
isinstanceÚtupleÚadd_criteria©r   Úotherr   r   r   Ú__iadd__V   s
   


ÿzBakedQuery.__iadd__c                 C   s   t |tƒr
| j|Ž S |  |¡S r   )r.   r/   Úwith_criteriar1   r   r   r   Ú__add__]   s   


zBakedQuery.__add__c                 G   s   |   ||¡ | j |¡ | S )z«Add a criteria function to this :class:`.BakedQuery`.

        This is equivalent to using the ``+=`` operator to
        modify a :class:`.BakedQuery` in-place.

        )r    r   Úappendr,   r   r   r   r0   c   s   zBakedQuery.add_criteriac                 G   s   |   ¡ j|g|¢R Ž S )zËAdd a criteria function to a :class:`.BakedQuery` cloned from this one.

        This is equivalent to using the ``+`` operator to
        produce a new :class:`.BakedQuery` with modifications.

        )r*   r0   r,   r   r   r   r4   n   s   zBakedQuery.with_criteriac                 C   s
   t | |ƒS )zÒReturn a :class:`.Result` object for this :class:`.BakedQuery`.

        This is equivalent to calling the :class:`.BakedQuery` as a
        Python callable, e.g. ``result = my_baked_query(session)``.

        )ÚResult©r   Úsessionr   r   r   Úfor_sessionw   s   
zBakedQuery.for_sessionc                 C   s
   |   |¡S r   )r:   r8   r   r   r   r   €   s   
zBakedQuery.__call__Fc                 C   s4   |s| j s|  ¡ }| jd7  _|jg| _d| _ | S )az  Cancel any query caching that will occur on this BakedQuery object.

        The BakedQuery can continue to be used normally, however additional
        creational functions will not be cached; they will be called
        on every invocation.

        This is to support the case where a particular step in constructing
        a baked query disqualifies the query from being cacheable, such
        as a variant that relies upon some uncacheable value.

        :param full: if False, only functions added to this
         :class:`.BakedQuery` object subsequent to the spoil step will be
         non-cached; the state of the :class:`.BakedQuery` up until
         this point will be pulled from the cache.   If True, then the
         entire :class:`.Query` object is built from scratch each
         time, with all creational functions being called on each
         invocation.

        )Ú_query_onlyT)r   r*   r   Ú_retrieve_baked_queryr   )r   ÚfullÚ_spoil_pointr   r   r   Úspoilƒ   s   

zBakedQuery.spoilc                 C   s   | j |jf S )aŸ  Return the key that actually goes into the cache dictionary for
        this :class:`.BakedQuery`, taking into account the given
        :class:`.Session`.

        This basically means we also will include the session's query_class,
        as the actual :class:`.Query` object is part of what's cached
        and needs to match the type of :class:`.Query` that a later
        session will want to use.

        )r   Ú
_query_clsr8   r   r   r   Ú_effective_keyž   s   zBakedQuery._effective_keyc                 C   s   |   ¡ }|j|||d� |S )z2Cloning version of _add_lazyload_options.
        )Ú
cache_path)r*   Ú_add_lazyload_options)r   ÚoptionsÚeffective_pathrB   Úqr   r   r   Ú_with_lazyload_options«   s   z!BakedQuery._with_lazyload_optionsc                    sv   d}|sˆ }|j d jr|  ¡  nˆD ]}| |¡}|du r#|  ¡  q|dur+||7 }q|  ‡ ‡fdd„|j |¡ dS )a*  Used by per-state lazy loaders to add options to the
        "lazy load" query from a parent query.

        Creates a cache key based on given load path and query options;
        if a repeatable cache key cannot be generated, the query is
        "spoiled" so that it won't use caching.

        r   r   FNc                    s   |   ˆ ¡jˆŽ S r   )Ú_with_current_pathÚ_conditional_options©rF   ©rE   rD   r   r   Ú<lambda>Î   s
    ÿþz2BakedQuery._add_lazyload_options.<locals>.<lambda>)ÚpathÚis_aliased_classr?   Ú_generate_cache_keyr0   )r   rD   rE   rB   ÚkeyÚoptÚ	cache_keyr   rK   r   rC   ²   s"   



€ûz BakedQuery._add_lazyload_optionsc                 C   sF   | j  |  |¡d ¡}|d u r|  |¡}| d ¡| j |  |¡< | |¡S r   )r   ÚgetrA   Ú	_as_queryÚwith_session)r   r9   Úqueryr   r   r   r<   Õ   s   
ÿ
z BakedQuery._retrieve_baked_queryc                 C   sv   |   |¡}| ¡ }|  ||¡ d |_|j d ¡ |_}|j d| ji¡|_dD ]	}|j	 
|d ¡ q'|| j|  |¡< |S )NÚcompiled_cache)Ú
_correlateÚ	_from_objÚ_mapper_adapter_mapÚ	_joinpathÚ
_joinpoint)rT   Ú_compile_contextÚ_bake_subquery_loadersr9   rV   rU   Ú_execution_optionsÚunionr   Ú__dict__ÚpoprA   )r   r9   rV   ÚcontextÚattrr   r   r   Ú_bakeÞ   s   
ÿzBakedQuery._bakec                 C   s.   | j d |ƒ}| j dd … D ]}||ƒ}q|S ©Nr   é   )r   )r   r9   rV   Ústepr   r   r   rT   ö   s   
zBakedQuery._as_queryc                    s€   g  |j d< }t|j  ¡ ƒD ]/\}‰ tˆ tƒr=d|v r9t| j‡ fdd„ƒ}| j| |_| |¡ | 	||jˆ f¡ |j |= qdS )ab  convert subquery eager loaders in the cache into baked queries.

        For subquery eager loading to work, all we need here is that the
        Query point to the correct session when it is run.  However, since
        we are "baking" anyway, we may as well also turn the query into
        a "baked" query so that we save on performance too.

        Úbaked_queriesÚsubqueryc                     s   ˆ S r   r   )r   ©Úvr   r   rL   
  s    z3BakedQuery._bake_subquery_loaders.<locals>.<lambda>N)
Ú
attributesr(   Úitemsr.   r   r   r   r   re   r6   )r   r9   rc   ri   ÚkÚbkr   rk   r   r^   ý   s   	

€ùz!BakedQuery._bake_subquery_loadersc                 C   sh   |j d D ],\}}}t| j|fdd„ƒ}||_| |¡}	|D ]}
|	 |
¡}	q|	jdi |¤Ž|j |< qdS )z­Retrieve subquery eager loaders stored by _bake_subquery_loaders
        and turn them back into Result objects that will iterate just
        like a Query object.

        ri   c                 S   s
   |  | ¡S r   )rU   )ÚsessrF   r   r   r   rL     ó   
 z5BakedQuery._unbake_subquery_loaders.<locals>.<lambda>Nr   )rm   r   r   r   r:   Úwith_post_criteriaÚparams)r   r9   rc   rt   Úpost_criteriaro   rR   rV   rp   rF   r-   r   r   r   Ú_unbake_subquery_loaders  s   ÿ
øz#BakedQuery._unbake_subquery_loaders)r   )r"   N)Fr   )r   r   r   r   r   r   Úclassmethodr!   r*   r    r3   r5   r0   r4   r:   r   r?   rA   rG   rC   r<   re   rT   r^   rv   r   r   r   r   r   5   s.    
	
		


#	r   c                   @   sŒ   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!S )"r7   a  Invokes a :class:`.BakedQuery` against a :class:`.Session`.

    The :class:`.Result` object is where the actual :class:`.query.Query`
    object gets created, or retrieved from the cache,
    against a target :class:`.Session`, and is then invoked for results.

    ©Úbqr9   Ú_paramsÚ_post_criteriac                 C   s   || _ || _i | _g | _d S r   rx   )r   ry   r9   r   r   r   r   .  s   
zResult.__init__c                 O   sB   t |ƒdkr| |d ¡ nt |ƒdkrt d¡‚| j |¡ | S )z@Specify parameters to be replaced into the string SQL statement.rg   r   zFparams() takes zero or one positional argument, which is a dictionary.)ÚlenÚupdateÚsa_excÚArgumentErrorrz   )r   r   Úkwr   r   r   rt   4  s   ÿzResult.paramsc                 C   s   |r| j  |¡ | S r   )r{   Úextend)r   Úfnsr   r   r   Ú_using_post_criteriaA  s   zResult._using_post_criteriac                 C   s   |   |g¡S )a  Add a criteria function that will be applied post-cache.

        This adds a function that will be run against the
        :class:`.Query` object after it is retrieved from the
        cache.    Functions here can be used to alter the query in ways
        that **do not affect the SQL output**, such as execution options
        and shard identifiers (when using a shard-enabled query object)

        .. warning::  :meth:`.Result.with_post_criteria` functions are applied
           to the :class:`.Query` object **after** the query's SQL statement
           object has been retrieved from the cache.   Any operations here
           which intend to modify the SQL should ensure that
           :meth:`.BakedQuery.spoil` was called first.

        .. versionadded:: 1.2


        )rƒ   )r   r-   r   r   r   rs   F  s   zResult.with_post_criteriac                 C   s.   | j  | j¡ | j¡}| jD ]}||ƒ}q|S r   )ry   rT   r9   rt   rz   r{   )r   rF   r-   r   r   r   rT   [  s   

zResult._as_queryc                 C   s   t |  ¡ ƒS r   )ÚstrrT   ©r   r   r   r   Ú__str__a  s   zResult.__str__c                 C   sÐ   | j }| jjr
|jrt|  ¡ ƒS |j | | j¡d ¡}|d u r%| 	| j¡}t
 
|¡}| j|_|j 
¡ |_| | j|| j| j¡ d|j_|jrN|jsN| j ¡  |j | j¡ | j¡}| jD ]}||ƒ}q\| |¡S ©NT)ry   r9   Úenable_baked_queriesr   ÚiterrT   r   rS   rA   re   Úcopyrm   rv   rz   r{   Ú	statementÚ
use_labelsÚ	autoflushÚpopulate_existingÚ
_autoflushrV   rt   rU   Ú_execute_and_instances)r   ry   Úbaked_contextrc   rF   r-   r   r   r   Ú__iter__d  s&   
ÿ



zResult.__iter__c                    s:   t  tdƒ¡‰ | j ‡ fdd„¡}| | j¡ | j¡ 	¡ S )zéreturn the 'count'.

        Equivalent to :meth:`.Query.count`.

        Note this uses a subquery to ensure an accurate count regardless
        of the structure of the original statement.

        .. versionadded:: 1.1.6

        Ú*c                    s
   |   ˆ ¡S r   )Ú	from_selfrJ   ©Úcolr   r   rL   ‹  rr   zResult.count.<locals>.<lambda>)
r   Úcountr   ry   r4   r:   r9   rt   rz   Úscalar)r   ry   r   r•   r   r—   ~  s   zResult.countc                 C   s:   z|   ¡ }t|tƒs|W S |d W S  tjy   Y dS w )zîReturn the first element of the first result or None
        if no rows present.  If multiple rows are returned,
        raises MultipleResultsFound.

        Equivalent to :meth:`.Query.scalar`.

        .. versionadded:: 1.1.6

        r   N)Úoner.   r/   Úorm_excÚNoResultFound©r   Úretr   r   r   r˜   Ž  s   


ÿzResult.scalarc                 C   sH   | j  dd„ ¡}t| | j¡ | j¡ | j¡ƒ}t	|ƒdkr"|d S dS )zLReturn the first row.

        Equivalent to :meth:`.Query.first`.

        c                 S   s   |   dd¡S rf   )ÚslicerJ   r   r   r   rL   ¦  s    zResult.first.<locals>.<lambda>r   N)
ry   r4   r(   r:   r9   rt   rz   rƒ   r{   r|   )r   ry   r�   r   r   r   Úfirst   s   
ýzResult.firstc                 C   s>   z|   ¡ }W n tjy   t d¡‚w |du rt d¡‚|S )zeReturn exactly one result or raise an exception.

        Equivalent to :meth:`.Query.one`.

        z"Multiple rows were found for one()NzNo row was found for one())Úone_or_nonerš   ÚMultipleResultsFoundr›   rœ   r   r   r   r™   ±  s   ÿÿ
z
Result.onec                 C   s6   t | ƒ}t|ƒ}|dkr|d S |dkrdS t d¡‚)zªReturn one or zero results, or raise an exception for multiple
        rows.

        Equivalent to :meth:`.Query.one_or_none`.

        .. versionadded:: 1.0.9

        rg   r   Nz*Multiple rows were found for one_or_none())r(   r|   rš   r¡   )r   r�   Úlr   r   r   r    Â  s   	ÿzResult.one_or_nonec                 C   s   t | ƒS )zEReturn all rows.

        Equivalent to :meth:`.Query.all`.

        )r(   r…   r   r   r   Úall×  s   z
Result.allc                 C   s    | j jd | jƒ}| || j¡S )zZRetrieve an object based on identity.

        Equivalent to :meth:`.Query.get`.

        r   )ry   r   r9   Ú	_get_implÚ_load_on_pk_identity)r   ÚidentrV   r   r   r   rS   ß  s   z
Result.getc                    sÀ   |  ¡ ‰ˆj\‰ ‰‡ ‡‡‡‡fdd„}ˆj}| ¡ }| jˆ f7  _| |tdd„ ˆD ƒƒ¡}t‡fdd„tˆˆj	ƒD ƒƒ}t
| ˆj¡jd
i |¤Žƒ}t|ƒ}|dkrXt ¡ ‚|r^|d S d	S )z6Load the given primary key identity from the database.c                    sx   ˆ }|   ¡ }| ¡  d |_d ˆv r&t‡fdd„tˆjˆƒD ƒƒ}t ||¡}| |dd¡}||_	ˆj
D ]}||ƒ}q3|S )Nc                    s"   g | ]\}}|d u rˆ | j ‘qS r   ©rP   )Ú.0r–   Úvalue©Ú_get_paramsr   r   Ú
<listcomp>ú  s
    üz>Result._load_on_pk_identity.<locals>.setup.<locals>.<listcomp>TF)r*   Ú_get_conditionÚ	_order_byÚsetÚzipÚprimary_keyÚsql_utilÚadapt_criterion_to_nullÚ_adapt_clauseÚ
_criterionr{   )rV   Ú_lcl_get_clauserF   Únonesr-   ©Ú_get_clauser«   ÚmapperÚprimary_key_identityr   r   r   Úsetupð  s(   
ÿþÿ	ÿ

z*Result._load_on_pk_identity.<locals>.setupc                 s   s   � | ]}|d u V  qd S r   r   )r¨   Úelemr   r   r   Ú	<genexpr>  s   € z.Result._load_on_pk_identity.<locals>.<genexpr>c                    s   g | ]\}}ˆ | j |f‘qS r   r§   )r¨   Úid_valr±   rª   r   r   r¬     s    ÿÿz/Result._load_on_pk_identity.<locals>.<listcomp>rg   r   Nr   )Ú_mapper_zeror¹   ry   r*   r   r4   r/   Údictr°   r±   r(   r:   r9   rt   r|   rš   r¡   )r   rV   r»   r¼   ry   rt   Úresultr¢   r   r¸   r   r¥   é  s.   
ÿ
ÿþÿ	zResult._load_on_pk_identityN)r   r   r   r   r   r   rt   rƒ   rs   rT   r†   r’   r—   r˜   rŸ   r™   r    r£   rS   r¥   r   r   r   r   r7   #  s$    
r7   z1.2z5Baked lazy loading is now the default implementation.c                   C   s   dS )zâEnable the use of baked queries for all lazyloaders systemwide.

    The "baked" implementation of lazy loading is now the sole implementation
    for the base lazy loader; this method has no effect except for a warning.

    Nr   r   r   r   r   Úbake_lazy_loaders.  s   
rÃ   c                   C   s   t dƒ‚)aC  Disable the use of baked queries for all lazyloaders systemwide.

    This method now raises NotImplmentedError() as the "baked" implementation
    is the only lazy load implementation.  The
    :paramref:`.relationship.bake_queries` flag may be used to disable
    the caching of queries on a per-relationship basis.

    z4Baked lazy loading is now the default implementation)ÚNotImplementedErrorr   r   r   r   Úunbake_lazy_loaders;  s   ÿrÅ   c                 C   s   |   |ddi¡S )zxIndicate that the given attribute should be loaded using "lazy"
    loading with a "baked" query used in the load.

    ÚlazyÚbaked_select)Úset_relationship_strategy)Úloadoptrd   r   r   r   Úbaked_lazyloadL  s   rÊ   zFBaked lazy loading is now the default implementation for lazy loading.c                  G   ó   t j t jj| di ¡S )NF©r   Ú_UnboundLoadÚ
_from_keysrÊ   ©Úkeysr   r   r   rÊ   U  ó   ÿc                  G   rË   r‡   rÌ   rÏ   r   r   r   Úbaked_lazyload_alla  rÑ   rÒ   )"r   rŠ   ÚloggingÚ r   r~   r   Úormrš   r   Ú	orm.queryr   Úsqlr   r   r²   Ú	getLoggerr   ÚlogÚobjectr	   r   r7   Ú
deprecatedrÃ   rÅ   Úloader_optionrÊ   Ú_add_unbound_fnÚ_add_unbound_all_fnrÒ   Ú_unbound_fnÚ_unbound_all_fnr!   r   r   r   r   Ú<module>   sV   	
 o  ÿ

ÿ

þþ
