1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
U
¸ý°dØGã@s¶dZddlmZddlZddlmZddlmZddl    mZ
ddl m 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
)ÚselfZcls_r ©rúKd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\sqlalchemy/ext/baked.pyÚ__init__1szBakery.__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„Z dd„Z dd„Z dd„Zdd„Zd.dd„Zdd„Zd/dd „Zd0d!d"„Zd#d$„Zd%d&„Zd'd(„Zd)d*„ZdS)1Ú
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`
 
        )Z
size_alert)r    rZLRUCache)r ÚsizeZ _size_alertrrrr EszBakedQuery.bakerycCs2t t¡}|j|_t|jƒ|_|j|_|j|_|Sr )rÚ__new__rÚlistrrr)rZb1rrrÚ_cloneOs 
 zBakedQuery._clonecCs|j|jf|7_dSr )rÚ__code__©rÚfnrrrrrWszBakedQuery._update_cache_keycCs$t|tƒr|j|Žn
| |¡|Sr )Ú
isinstanceÚtupleÚ add_criteria©rÚotherrrrÚ__iadd__Zs
 
zBakedQuery.__iadd__cCs"t|tƒr|j|ŽS| |¡SdSr )r)r*Ú with_criteriar,rrrÚ__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.
 
        )rrÚappendr'rrrr+gs  zBakedQuery.add_criteriacGs| ¡j|f|žŽ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%r+r'rrrr/rszBakedQuery.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|szBakedQuery.for_sessioncCs
| |¡Sr )r5r3rrrr†szBakedQuery.__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.
 
        )Z _query_onlyT)rr%rÚ_retrieve_baked_queryr)rÚfullZ _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.
 
        )rZ
_query_clsr3rrrÚ_effective_key¤s zBakedQuery._effective_keycCs| ¡}|j|||d|S)z)Cloning version of _add_lazyload_options.)Ú
cache_path)r%Ú_add_lazyload_options)rÚoptionsÚeffective_pathr:ÚqrrrÚ_with_lazyload_options±sz!BakedQuery._with_lazyload_optionscszd}|s ˆ}ˆD]J}|js |jr| ¡}|dkr>|jddq|drNtdƒ‚||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)r7é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 )Z_with_current_pathr<©r>©r=r<rrÚ<lambda>Õóz2BakedQuery._add_lazyload_options.<locals>.<lambda>)Z_is_legacy_optionZ_is_compile_stateZ_generate_cache_keyr8ÚAssertionErrorr+Úpath)rr<r=r:ÚkeyÚoptZckrrBrr;·s"
 
ÿ ýz BakedQuery._add_lazyload_optionscCsF|j | |¡d¡}|dkr<| |¡}| d¡|j| |¡<| |¡Sr )rÚgetr9Ú    _as_queryZ with_session)rr4Úqueryrrrr6Ús
ÿz BakedQuery._retrieve_baked_querycCs<| |¡}d|_| ¡}|jjr4||f|j| |¡<||fSr )rJr4Z _statement_20Z_compile_optionsZ_bake_okrr9)rr4rKÚ    statementrrrÚ_bakeãs
 þzBakedQuery._bakecCsNt|tƒr|}n4t|tƒr4|j}|dkrDt d¡‚ntdt|ƒƒ‚| |¡S)aLReturn 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)rrr4Úsa_excÚ ArgumentErrorÚ    TypeErrorÚtyperJ)rZquery_or_sessionr4rrrÚto_queryüs&
 
ÿÿÿzBakedQuery.to_querycCs.|jd|ƒ}|jdd…D] }||ƒ}q|S©Nrr@)r)rr4rKÚsteprrrrJ1s
zBakedQuery._as_query)r)r!N)r)F)N)N)rrrrrrÚ classmethodr r%rr.r0r+r/r5rr8r9r?r;r6rMrRrJrrrrr9s*
     
 
 
 
 
 
#    5rc@s”eZdZdZdZdd„Zdd„Zdd„Zd    d
„Zd d „Z    d d„Z
dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„Zdd„Zdd „Zd!d"„Zd#S)$r2aInvokes 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.
 
    ©Úbqr4Ú_paramsÚ_post_criteriacCs||_||_i|_g|_dSr rV)rrWr4rrrrEszResult.__init__cOsBt|ƒdkr| |d¡nt|ƒdkr2t d¡‚|j |¡|S)z@Specify parameters to be replaced into the string SQL statement.r@rzFparams() takes zero or one positional argument, which is a dictionary.)ÚlenÚupdaterNrOrX)rrÚkwrrrÚparamsKs  ÿ z Result.paramscCs|r|j |¡|Sr )rYÚextend)rÚfnsrrrÚ_using_post_criteriaXs 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
 
 
        )r`)rr(rrrÚwith_post_criteria]szResult.with_post_criteriacCs.|j |j¡ |j¡}|jD] }||ƒ}q|Sr )rWrJr4r]rXrY)rr>r(rrrrJvs
 
zResult._as_querycCs t| ¡ƒSr )ÚstrrJ©rrrrÚ__str__|szResult.__str__cCs | ¡ ¡Sr )Ú_iterÚ__iter__rcrrrrfszResult.__iter__c    Csä|j}|jjr|jr | ¡ ¡S|j | |j¡d¡\}}|dkrR|     |j¡\}}|j
rf|  |j
¡}n|}|j D] }||ƒ}qp|j
}t |jƒ}| |j|jdœ¡|jj|||d}|j dd¡rÊ| ¡}|j dd¡rà| ¡}|S)N)NN)Z_sa_orm_load_optionsZcompiled_cache)Úexecution_optionsZis_single_entityFÚfiltered)rWr4Zenable_baked_queriesrrJrerrIr9rMrXr]rYÚdictZ_execution_optionsr[Z load_optionsÚexecuteÚ _attributesZscalarsÚunique)    rrWrKrLr>r(r]rgÚresultrrrre‚s> 
ÿ
 
 
þÿÿ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 )Z_legacy_from_selfrA©ÚcolrrrC´rDzResult.count.<locals>.<lambda>)
rÚcountrrWr/r5r4r]rXÚscalar©rrWrrorrq©s
z Result.countcCsBz$| ¡}t|tjƒs|WS|dWStjk
r<YdSXdS)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_excZ NoResultFound)rÚretrrrrr·s 
z Result.scalarcCs4|j dd„¡}| |j¡ |j¡ |j¡ ¡     ¡S)zRReturn the first row.
 
        Equivalent to :meth:`_query.Query.first`.
 
        cSs | dd¡SrS)ÚslicerArrrrCÎrDzResult.first.<locals>.<lambda>)
rWr/r5r4r]rXr`rYreÚfirstrsrrrrzÇs ÿþ
ÿz Result.firstcCs | ¡ ¡S)zkReturn exactly one result or raise an exception.
 
        Equivalent to :meth:`_query.Query.one`.
 
        )rertrcrrrrt×sz
Result.onecCs | ¡ ¡S)zReturn one or zero results, or raise an exception for multiple
        rows.
 
        Equivalent to :meth:`_query.Query.one_or_none`.
 
        )reÚ one_or_nonercrrrr{ßszResult.one_or_nonecCs | ¡ ¡S)zKReturn all rows.
 
        Equivalent to :meth:`_query.Query.all`.
 
        )reÚallrcrrrr|èsz
Result.allcCs |jjd|jƒ}| ||j¡S)z`Retrieve an object based on identity.
 
        Equivalent to :meth:`_query.Query.get`.
 
        r)rWrr4Z    _get_implÚ_load_on_pk_identity)rÚidentrKrrrrIðsz
Result.getc
 sÆ|jdjd‰ˆj\‰‰‡‡‡‡‡fdd„}ˆj}| ¡}|jˆf7_| |tdd„ˆDƒƒ¡}‡fdd„tˆˆj    ƒDƒ}t
|  ˆj ¡j f|Žƒ}t|ƒ}    |    d    kr²t ¡‚n|    r¾|dSd
Sd
S) z6Load the given primary key identity from the database.rZ parententitycstˆ}| ¡}| ¡d|_dˆkrH‡fdd„tˆjˆƒDƒ}t ||¡}t |ddi¡f|_ˆj    D] }||ƒ}qb|S)Ncs"h|]\}}|dkrˆ|j’qSr ©rG)Ú.0rpÚvalue©Ú _get_paramsrrÚ    <setcomp>
süz=Result._load_on_pk_identity.<locals>.setup.<locals>.<setcomp>Z
_orm_adaptT)
r%Z_get_conditionZ    _order_byÚzipÚ primary_keyÚsql_utilZadapt_criterion_to_nullZ_deep_annotateZ_where_criteriarY)rKZ_lcl_get_clauser>Znonesr(©Ú _get_clauserƒZmapperÚprimary_key_identityrrrÚsetups(
ÿþÿÿ
 
z*Result._load_on_pk_identity.<locals>.setupcss|]}|dkVqdSr r)r€ÚelemrrrÚ    <genexpr>*sz.Result._load_on_pk_identity.<locals>.<genexpr>csi|]\}}ˆ|j|“qSrr)r€Zid_valr†r‚rrÚ
<dictcomp>-sÿz/Result._load_on_pk_identity.<locals>.<dictcomp>r@N)Z _raw_columnsZ _annotationsr‰rWr%rr/r*r…r†r$r5r4r]rZrwZMultipleResultsFound)
rr4rKrŠr\r‹rWr]rmÚlrrˆrr}ús.
 ÿ
ÿþ
zResult._load_on_pk_identityN)rrrrrrr]r`rarJrdrfrerqrrrzrtr{r|rIr}rrrrr2:s$ '    
r2)rÚcollections.abcÚabcruÚloggingÚrrNrZormrwZ    orm.queryrZ orm.sessionrZsqlrrr‡Ú    getLoggerrÚlogr    rr2r rrrrÚ<module>
s&