HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/local/lib/python3.10/dist-packages/sqlalchemy/ext/__pycache__/baked.cpython-310.pyc
o

���gYE�@s�dZddlmZddlZddlmZddlmZddl	mZ
ddlmZddl
mZddlmZdd	lmZddlmZe�e�ZGd
d�d�ZGdd
�d
�ZGdd�d�ZejZdS)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)�Query)�Session)�func)�literal_columnc@s$eZdZdZdZdd�Zdd�ZdS)�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�cachecCs||_||_dS�Nr
)�self�cls_r�r�?/usr/local/lib/python3.10/dist-packages/sqlalchemy/ext/baked.py�__init__1s
zBakery.__init__cGs|�|j||�Sr
r
)r�
initial_fn�argsrrr�__call__5szBakery.__call__N)�__name__�
__module__�__qualname__�__doc__�	__slots__rrrrrrr	#s
r	c@s�eZdZdZdZd+dd�Zed,dd	��Zd
d�Zd+dd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zd-dd�Zdd�Zd.dd �Zd.d!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�ZdS)/�
BakedQueryz3A builder object for :class:`.query.Query` objects.)�steps�_bakery�
_cache_key�_spoiledrcCs*d|_|�||�|g|_d|_||_dS)NrF)r�_update_cache_keyrrr)r�bakeryrrrrrr>s

zBakedQuery.__init__��NcCst|tj||d��S)zSConstruct a new bakery.

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

        )�
size_alert)r	r�LRUCache)r�size�_size_alertrrrr!EszBakedQuery.bakerycCs2t�t�}|j|_t|j�|_|j|_|j|_|Sr
)r�__new__r�listrrr)r�b1rrr�_cloneOs
zBakedQuery._clonecCs|j|jf|7_dSr
)r�__code__�r�fnrrrrr WszBakedQuery._update_cache_keycCs&t|t�r|j|�|S|�|�|Sr
)�
isinstance�tuple�add_criteria�r�otherrrr�__iadd__Zs



�zBakedQuery.__iadd__cCst|t�r
|j|�S|�|�Sr
)r.r/�
with_criteriar1rrr�__add__as


zBakedQuery.__add__cGs|�||�|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,rrrr0gszBakedQuery.add_criteriacGs|��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*r0r,rrrr4rszBakedQuery.with_criteriacCs
t||�S)z�Return a :class:`_baked.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�sessionrrr�for_session|s
zBakedQuery.for_sessioncCs
|�|�Sr
)r:r8rrrr�s
zBakedQuery.__call__FcCs4|s|js|��}|jd7_|jg|_d|_|S)a�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.Query` object is built from scratch each
         time, with all creational functions being called on each
         invocation.

        )�_query_onlyT)rr*r�_retrieve_baked_queryr)r�full�_spoil_pointrrr�spoil�s

zBakedQuery.spoilcCs|j|jfS)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.Query` object is part of what's cached
        and needs to match the type of :class:`_query.Query` that a later
        session will want to use.

        )r�
_query_clsr8rrr�_effective_key�szBakedQuery._effective_keycCs|��}|j|||d�|S)z)Cloning version of _add_lazyload_options.)�
cache_path)r*�_add_lazyload_options)r�options�effective_pathrB�qrrr�_with_lazyload_options�sz!BakedQuery._with_lazyload_optionscszd}|s�}�D]%}|js|jr-|��}|dur|jdd�q|dr'Jd��||d7}q|���fdd	�|j|�dS)
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.

        rNT)r=�z�loader options with variable bound parameters not supported with baked queries.  Please use new-style select() statements for cached ORM queries.rcs|���j��Sr
)�_with_current_pathrD�rF�rErDrr�<lambda>�sz2BakedQuery._add_lazyload_options.<locals>.<lambda>)�_is_legacy_option�_is_compile_state�_generate_cache_keyr?r0�path)rrDrErB�key�opt�ckrrKrrC�s$

���z BakedQuery._add_lazyload_optionscCsF|j�|�|�d�}|dur|�|�}|�d�|j|�|�<|�|�Sr
)r�getrA�	_as_query�with_session)rr9�queryrrrr<�s
�
z BakedQuery._retrieve_baked_querycCs<|�|�}d|_|��}|jjr||f|j|�|�<||fSr
)rUr9�
_statement_20�_compile_options�_bake_okrrA)rr9rW�	statementrrr�_bake�s
�zBakedQuery._bakecCsNt|t�r|}nt|t�r|j}|durt�d��ntdt|���|�|�S)aReturn the :class:`_query.Query` object for use as a subquery.

        This method should be used within the lambda callable being used
        to generate a step of an enclosing :class:`.BakedQuery`.   The
        parameter should normally be the :class:`_query.Query` object that
        is passed to the lambda::

            sub_bq = self.bakery(lambda s: s.query(User.name))
            sub_bq += lambda q: q.filter(User.id == Address.user_id).correlate(Address)

            main_bq = self.bakery(lambda s: s.query(Address))
            main_bq += lambda q: q.filter(sub_bq.to_query(q).exists())

        In the case where the subquery is used in the first callable against
        a :class:`.Session`, the :class:`.Session` is also accepted::

            sub_bq = self.bakery(lambda s: s.query(User.name))
            sub_bq += lambda q: q.filter(User.id == Address.user_id).correlate(Address)

            main_bq = self.bakery(
                lambda s: s.query(Address.id, sub_bq.to_query(q).scalar_subquery())
            )

        :param query_or_session: a :class:`_query.Query` object or a class
         :class:`.Session` object, that is assumed to be within the context
         of an enclosing :class:`.BakedQuery` callable.


         .. versionadded:: 1.3


        Nz1Given Query needs to be associated with a Sessionz)Query or Session object expected, got %r.)	r.rrr9�sa_exc�
ArgumentError�	TypeError�typerU)r�query_or_sessionr9rrr�to_query�s
"
����
zBakedQuery.to_querycCs.|jd|�}|jdd�D]}||�}q|S�NrrH)r)rr9rW�steprrrrU-s
zBakedQuery._as_query)r)r"N)Fr
)rrrrrr�classmethodr!r*r r3r5r0r4r:rr?rArGrCr<r\rbrUrrrrr9s,
	





#	1rc@s�eZdZdZdZdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#S)$r7aInvokes a :class:`.BakedQuery` against a :class:`.Session`.

    The :class:`_baked.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_criteriacCs||_||_i|_g|_dSr
rf)rrgr9rrrrAs
zResult.__init__cOsBt|�dkr|�|d�nt|�dkrt�d��|j�|�|S)z@Specify parameters to be replaced into the string SQL statement.rHrzFparams() takes zero or one positional argument, which is a dictionary.)�len�updater]r^rh)rr�kwrrr�paramsGs�z
Result.paramscCs|r|j�|�|Sr
)ri�extend)r�fnsrrr�_using_post_criteriaTszResult._using_post_criteriacCs|�|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.Query` object after it is retrieved from the
        cache.    This currently includes **only** the
        :meth:`_query.Query.params` and :meth:`_query.Query.execution_options`
        methods.

        .. warning::  :meth:`_baked.Result.with_post_criteria`
           functions are applied
           to the :class:`_query.Query`
           object **after** the query's SQL statement
           object has been retrieved from the cache.   Only
           :meth:`_query.Query.params` and
           :meth:`_query.Query.execution_options`
           methods should be used.


        .. versionadded:: 1.2


        )rp)rr-rrr�with_post_criteriaYszResult.with_post_criteriacCs.|j�|j��|j�}|jD]}||�}q|Sr
)rgrUr9rmrhri)rrFr-rrrrUrs

zResult._as_querycCst|���Sr
)�strrU�rrrr�__str__x�zResult.__str__cC�|����Sr
)�_iter�__iter__rsrrrrx{ruzResult.__iter__c	Cs�|j}|jjr
|jr|����S|j�|�|j�d�\}}|dur)|�	|j�\}}|j
r3|�|j
�}n|}|jD]}||�}q8|j
}t
|j�}|�|j|jd��|jj|||d�}|j�dd�re|��}|j�dd�rp|��}|S)N)NN)�_sa_orm_load_options�compiled_cache)�execution_options�is_single_entityF�filtered)rgr9�enable_baked_queriesrrUrwrrTrAr\rhrmri�dict�_execution_optionsrk�load_options�execute�_attributes�scalars�unique)	rrgrWr[rFr-rmr{�resultrrrrw~s8�


���zResult._itercs:t�td���|j��fdd��}|�|j��|j��	�S)z�return the 'count'.

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

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

        �*cs
|���Sr
)�_legacy_from_selfrJ��colrrrL�s
zResult.count.<locals>.<lambda>)
r�countrrgr4r:r9rmrh�scalar�rrgrr�rr��s
zResult.countcCs<z|��}t|tj�s|WS|dWStjyYdSw)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.Query.scalar`.

        rN)�oner.�collections_abc�Sequence�orm_exc�
NoResultFound)r�retrrrr��s
�z
Result.scalarcCs4|j�dd��}|�|j��|j��|j����	�S)zRReturn the first row.

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

        cSs|�dd�Src)�slicerJrrrrL�szResult.first.<locals>.<lambda>)
rgr4r:r9rmrhrprirw�firstr�rrrr��s
�zResult.firstcCrv)zkReturn exactly one result or raise an exception.

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

        )rwr�rsrrrr���z
Result.onecCrv)z�Return one or zero results, or raise an exception for multiple
        rows.

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

        )rw�one_or_nonersrrrr��szResult.one_or_nonecCrv)zKReturn all rows.

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

        )rw�allrsrrrr��r�z
Result.allcCs |jjd|j�}|�||j�S)z`Retrieve an object based on identity.

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

        r)rgrr9�	_get_impl�_load_on_pk_identity)r�identrWrrrrT�sz
Result.getc
s�|jdjd��j\�������fdd�}�j}|��}|j�f7_|�|tdd��D���}�fdd�t��j	�D�}t
|��j�j
di|���}t|�}	|	d	krZt���|	r`|dSd
S)z6Load the given primary key identity from the database.r�parententitycst�}|��}|��d|_d�vr$�fdd�t�j��D�}t�||�}t�|ddi�f|_�j	D]}||�}q1|S)Ncs"h|]
\}}|dur�|j�qSr
�rQ)�.0r��value��_get_paramsrr�	<setcomp>s
�z=Result._load_on_pk_identity.<locals>.setup.<locals>.<setcomp>�
_orm_adaptT)
r*�_get_condition�	_order_by�zip�primary_key�sql_util�adapt_criterion_to_null�_deep_annotate�_where_criteriari)rW�_lcl_get_clauserF�nonesr-��_get_clauser��mapper�primary_key_identityrrr�setup�s$
����

z*Result._load_on_pk_identity.<locals>.setupcss�|]}|duVqdSr
r)r��elemrrr�	<genexpr>&s�z.Result._load_on_pk_identity.<locals>.<genexpr>csi|]
\}}�|j|�qSrr�)r��id_valr�r�rr�
<dictcomp>)s
��z/Result._load_on_pk_identity.<locals>.<dictcomp>rHNr)�_raw_columns�_annotationsr�rgr*rr4r/r�r�r(r:r9rmrjr��MultipleResultsFound)
rr9rWr�rlr�rgrmr��lrr�rr��s*
 �
��zResult._load_on_pk_identityN)rrrrrrrmrprqrUrtrxrwr�r�r�r�r�r�rTr�rrrrr76s&
'	
r7)r�collections.abc�abcr��logging�rr]r�ormr��	orm.queryr�orm.sessionr�sqlrrr��	getLoggerr�logr	rr7r!rrrr�<module>s&		
~