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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
U
¸ý°dO•ã@sÞdZddlmZddlZddlZddlZddlZddlZddl    m
Z
ddl    m Z ddl mZddl m Z dd    lmZdd
lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddl m Z ddl m!Z!ddl m"Z"ddl#m$Z$ddl#m%Z%ddl&m'Z'ddl(m)Z)ddl(m*Z*dd l(m+Z+e rŽdd!l    m,Z,Gd"d#„d#eƒZ-Gd$d%„d%e'j.ƒZ/Gd&d'„d'eƒZ0Gd(d)„d)e'j1ƒZ2Gd*d+„d+eƒZ3Gd,d-„d-e'j4ƒZ5Gd.d/„d/e'j6ƒZ7Gd0d1„d1e'j8ƒZ9Gd2d3„d3eƒZ:Gd4d5„d5eƒZ;Gd6d7„d7e'j<ƒZ=Gd8d9„d9e'j>ƒZ?Gd:d;„d;ej@ƒZAGd<d=„d=ejBƒZCGd>d?„d?e'j@jDƒZEGd@dA„dAe'j@jFƒZGGdBdC„dCe'j@jHƒZIGdDdE„dEejJƒZKGdFdG„dGe'jLƒZMGdHdI„dIeMe'jNƒZOGdJdK„dKeƒZPGdLdM„dMeƒZQGdNdO„dOe'jRƒZSGdPdQ„dQe jTƒZUGdRdS„dSe jVƒZWGdTdU„dUeƒZXGdVdW„dWeƒZYGdXdY„dYeƒZZGdZd[„d[ƒZ[Gd\d]„d]e[ƒZ\Gd^d_„d_e$ƒZ]Gd`da„dae]ƒZ^Gdbdc„dcƒZ_Gddde„deeƒZ`e`ZadS)faè
.. dialect:: postgresql+asyncpg
    :name: asyncpg
    :dbapi: asyncpg
    :connectstring: postgresql+asyncpg://user:password@host:port/dbname[?key=value&key=value...]
    :url: https://magicstack.github.io/asyncpg/
 
The asyncpg dialect is SQLAlchemy's first Python asyncio dialect.
 
Using a special asyncio mediation layer, the asyncpg dialect is usable
as the backend for the :ref:`SQLAlchemy asyncio <asyncio_toplevel>`
extension package.
 
This dialect should normally be used only with the
:func:`_asyncio.create_async_engine` engine creation function::
 
    from sqlalchemy.ext.asyncio import create_async_engine
    engine = create_async_engine("postgresql+asyncpg://user:pass@hostname/dbname")
 
The dialect can also be run as a "synchronous" dialect within the
:func:`_sa.create_engine` function, which will pass "await" calls into
an ad-hoc event loop.  This mode of operation is of **limited use**
and is for special testing scenarios only.  The mode can be enabled by
adding the SQLAlchemy-specific flag ``async_fallback`` to the URL
in conjunction with :func:`_sa.create_engine`::
 
    # for testing purposes only; do not use in production!
    engine = create_engine("postgresql+asyncpg://user:pass@hostname/dbname?async_fallback=true")
 
 
.. versionadded:: 1.4
 
.. note::
 
    By default asyncpg does not decode the ``json`` and ``jsonb`` types and
    returns them as strings. SQLAlchemy sets default type decoder for ``json``
    and ``jsonb`` types using the python builtin ``json.loads`` function.
    The json implementation used can be changed by setting the attribute
    ``json_deserializer`` when creating the engine with
    :func:`create_engine` or :func:`create_async_engine`.
 
 
.. _asyncpg_prepared_statement_cache:
 
Prepared Statement Cache
--------------------------
 
The asyncpg SQLAlchemy dialect makes use of ``asyncpg.connection.prepare()``
for all statements.   The prepared statement objects are cached after
construction which appears to grant a 10% or more performance improvement for
statement invocation.   The cache is on a per-DBAPI connection basis, which
means that the primary storage for prepared statements is within DBAPI
connections pooled within the connection pool.   The size of this cache
defaults to 100 statements per DBAPI connection and may be adjusted using the
``prepared_statement_cache_size`` DBAPI argument (note that while this argument
is implemented by SQLAlchemy, it is part of the DBAPI emulation portion of the
asyncpg dialect, therefore is handled as a DBAPI argument, not a dialect
argument)::
 
 
    engine = create_async_engine("postgresql+asyncpg://user:pass@hostname/dbname?prepared_statement_cache_size=500")
 
To disable the prepared statement cache, use a value of zero::
 
    engine = create_async_engine("postgresql+asyncpg://user:pass@hostname/dbname?prepared_statement_cache_size=0")
 
.. versionadded:: 1.4.0b2 Added ``prepared_statement_cache_size`` for asyncpg.
 
 
.. warning::  The ``asyncpg`` database driver necessarily uses caches for
   PostgreSQL type OIDs, which become stale when custom PostgreSQL datatypes
   such as ``ENUM`` objects are changed via DDL operations.   Additionally,
   prepared statements themselves which are optionally cached by SQLAlchemy's
   driver as described above may also become "stale" when DDL has been emitted
   to the PostgreSQL database which modifies the tables or other objects
   involved in a particular prepared statement.
 
   The SQLAlchemy asyncpg dialect will invalidate these caches within its local
   process when statements that represent DDL are emitted on a local
   connection, but this is only controllable within a single Python process /
   database engine.     If DDL changes are made from other database engines
   and/or processes, a running application may encounter asyncpg exceptions
   ``InvalidCachedStatementError`` and/or ``InternalServerError("cache lookup
   failed for type <oid>")`` if it refers to pooled database connections which
   operated upon the previous structures. The SQLAlchemy asyncpg dialect will
   recover from these error cases when the driver raises these exceptions by
   clearing its internal caches as well as those of the asyncpg driver in
   response to them, but cannot prevent them from being raised in the first
   place if the cached prepared statement or asyncpg type caches have gone
   stale, nor can it retry the statement as the PostgreSQL transaction is
   invalidated when these errors occur.
 
.. _asyncpg_prepared_statement_name:
 
Prepared Statement Name
-----------------------
 
By default, asyncpg enumerates prepared statements in numeric order, which
can lead to errors if a name has already been taken for another prepared
statement. This issue can arise if your application uses database proxies
such as PgBouncer to handle connections. One possible workaround is to
use dynamic prepared statement names, which asyncpg now supports through
an optional ``name`` value for the statement name. This allows you to
generate your own unique names that won't conflict with existing ones.
To achieve this, you can provide a function that will be called every time
a prepared statement is prepared::
 
    from uuid import uuid4
 
    engine = create_async_engine(
        "postgresql+asyncpg://user:pass@hostname/dbname",
        poolclass=NullPool,
        connect_args={
            'prepared_statement_name_func': lambda:  f'__asyncpg_{uuid4()}__',
        },
    )
 
.. seealso::
 
   https://github.com/MagicStack/asyncpg/issues/837
 
   https://github.com/sqlalchemy/sqlalchemy/issues/6467
 
.. warning:: To prevent a buildup of useless prepared statements in
   your application, it's important to use the :class:`.NullPool` pool
   class, and to configure PgBouncer to use `DISCARD <https://www.postgresql.org/docs/current/sql-discard.html>`_
   when returning connections.  The DISCARD command is used to release resources held by the db connection,
   including prepared statements. Without proper setup, prepared statements can
   accumulate quickly and cause performance issues.
 
Disabling the PostgreSQL JIT to improve ENUM datatype handling
---------------------------------------------------------------
 
Asyncpg has an `issue <https://github.com/MagicStack/asyncpg/issues/727>`_ when
using PostgreSQL ENUM datatypes, where upon the creation of new database
connections, an expensive query may be emitted in order to retrieve metadata
regarding custom types which has been shown to negatively affect performance.
To mitigate this issue, the PostgreSQL "jit" setting may be disabled from the
client using this setting passed to :func:`_asyncio.create_async_engine`::
 
    engine = create_async_engine(
        "postgresql+asyncpg://user:password@localhost/tmp",
        connect_args={"server_settings": {"jit": "off"}},
    )
 
.. seealso::
 
    https://github.com/MagicStack/asyncpg/issues/727
 
é)Ú annotationsN)Úcast)Ú TYPE_CHECKINGé)Újson)Úranges)ÚARRAY)Ú_DECIMAL_TYPES)Ú _FLOAT_TYPES)Ú
_INT_TYPES)ÚENUM)ÚINTERVAL)ÚOID)Ú
PGCompiler)Ú    PGDialect)ÚPGExecutionContext)ÚPGIdentifierPreparer)ÚREGCLASS)Ú    REGCONFIG)ÚBYTEAé)Úexc)Úpool)Úutil)ÚAdaptedConnection)Ú
processors)Úsqltypes)Úasyncio)Úawait_fallback)Ú
await_only)ÚIterablec@seZdZdZdS)Ú AsyncpgARRAYTN©Ú__name__Ú
__module__Ú __qualname__Úrender_bind_cast©r'r'ú]d:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\sqlalchemy/dialects/postgresql/asyncpg.pyr!Èsr!c@seZdZdZdS)Ú AsyncpgStringTNr"r'r'r'r(r)Ìsr)c@seZdZdZdS)ÚAsyncpgREGCONFIGTNr"r'r'r'r(r*Ðsr*c@seZdZdZdS)Ú AsyncpgTimeTNr"r'r'r'r(r+Ôsr+c@seZdZdZdS)Ú AsyncpgByteATNr"r'r'r'r(r,Øsr,c@seZdZdZdS)Ú AsyncpgDateTNr"r'r'r'r(r-Üsr-c@seZdZdZdS)ÚAsyncpgDateTimeTNr"r'r'r'r(r.àsr.c@seZdZdZdS)ÚAsyncpgBooleanTNr"r'r'r'r(r/äsr/c@seZdZdZedd„ƒZdS)ÚAsyncPgIntervalTcKs t|jdS)N)Z    precision)r0Zsecond_precision)ÚclsÚintervalÚkwr'r'r(Úadapt_emulated_to_nativeësz(AsyncPgInterval.adapt_emulated_to_nativeN)r#r$r%r&Ú classmethodr4r'r'r'r(r0èsr0c@seZdZdZdS)Ú AsyncPgEnumTNr"r'r'r'r(r6ñsr6c@seZdZdZdS)ÚAsyncpgIntegerTNr"r'r'r'r(r7õsr7c@seZdZdZdS)ÚAsyncpgBigIntegerTNr"r'r'r'r(r8ùsr8c@seZdZdZdd„ZdS)Ú AsyncpgJSONTcCsdS©Nr'©ÚselfÚdialectÚcoltyper'r'r(Úresult_processorszAsyncpgJSON.result_processorN©r#r$r%r&r?r'r'r'r(r9ýsr9c@seZdZdZdd„ZdS)Ú AsyncpgJSONBTcCsdSr:r'r;r'r'r(r?szAsyncpgJSONB.result_processorNr@r'r'r'r(rAsrAc@s eZdZdS)ÚAsyncpgJSONIndexTypeN©r#r$r%r'r'r'r(rB srBc@seZdZdZdZdS)ÚAsyncpgJSONIntIndexTypeZjson_int_indexTN©r#r$r%Z__visit_name__r&r'r'r'r(rDsrDc@seZdZdZdZdS)ÚAsyncpgJSONStrIndexTypeZjson_str_indexTNrEr'r'r'r(rFsrFc@seZdZdd„ZdS)ÚAsyncpgJSONPathTypecCs dd„}|S)NcSs,t|tƒr|S|r$dd„|Dƒ}|SgSdS)NcSsg|] }t|ƒ‘qSr')Ústr©Ú.0Úelemr'r'r(Ú
<listcomp>#szGAsyncpgJSONPathType.bind_processor.<locals>.process.<locals>.<listcomp>)Ú
isinstancerH)ÚvalueÚtokensr'r'r(Úprocesss 
z3AsyncpgJSONPathType.bind_processor.<locals>.processr')r<r=rPr'r'r(Úbind_processors z"AsyncpgJSONPathType.bind_processorN)r#r$r%rQr'r'r'r(rGsrGc@s eZdZdZdd„Zdd„ZdS)ÚAsyncpgNumericTcCsdSr:r')r<r=r'r'r(rQ.szAsyncpgNumeric.bind_processorcCsv|jrB|tkrt tj|j¡S|tks.|tkr2dSt     
d|¡‚n0|tkrNdS|tks^|tkrdtj St     
d|¡‚dS)NzUnknown PG numeric type: %d) Z    asdecimalr
rZto_decimal_processor_factoryÚdecimalÚDecimalZ_effective_decimal_return_scaler    r rZInvalidRequestErrorZto_floatr;r'r'r(r?1s$ÿÿÿzAsyncpgNumeric.result_processorN)r#r$r%r&rQr?r'r'r'r(rR+srRc@seZdZdZdZdS)Ú AsyncpgFloatÚfloatTNrEr'r'r'r(rUJsrUc@seZdZdZdS)ÚAsyncpgREGCLASSTNr"r'r'r'r(rWOsrWc@seZdZdZdS)Ú
AsyncpgOIDTNr"r'r'r'r(rXSsrXc@seZdZdZdS)Ú AsyncpgCHARTNr"r'r'r'r(rYWsrYc@seZdZdd„Zdd„ZdS)Ú _AsyncpgRangecs|jjj‰‡fdd„}|S)Ncs<t|tjƒr8ˆ|j|j|jddk|jddk|jd}|S©Nrú[rú])Ú    lower_incÚ    upper_incÚempty©rMrÚRangeÚlowerÚupperÚboundsr`©rN©Ú asyncpg_Ranger'r(Úto_range_s   ûz._AsyncpgRange.bind_processor.<locals>.to_range)ÚdbapiÚasyncpgrb©r<r=rir'rgr(rQ\s
 z_AsyncpgRange.bind_processorcCs dd„}|S)NcSsL|dk    rH|j}tj|j|j|s$|jr(dnd›|s:|jr:dnd›|d}|S©Nr\ú(r]ú))rer`©Zisemptyrrbrcrdr^r_)rNr`r'r'r(rims&ûz0_AsyncpgRange.result_processor.<locals>.to_ranger')r<r=r>rir'r'r(r?ls z_AsyncpgRange.result_processorN©r#r$r%rQr?r'r'r'r(rZ[srZc@seZdZdd„Zdd„ZdS)Ú_AsyncpgMultiRangecs$|jjj‰tdƒ‰‡‡fdd„}|S)Ncs6t|tˆfƒr|S‡fdd„‰‡fdd„td|ƒDƒS)Ncs<t|tjƒr8ˆ|j|j|jddk|jddk|jd}|Sr[rarfrgr'r(ri†s   ûzE_AsyncpgMultiRange.bind_processor.<locals>.to_range.<locals>.to_rangecsg|] }ˆ|ƒ‘qSr'r')rJÚelement©rir'r(rL‘sÿzG_AsyncpgMultiRange.bind_processor.<locals>.to_range.<locals>.<listcomp>zIterable[ranges.Range])rMrHrrf©ZNoneTyperhrtr(ri‚s 
þz3_AsyncpgMultiRange.bind_processor.<locals>.to_range)rjrkrbÚtyperlr'rur(rQ}s
z!_AsyncpgMultiRange.bind_processorcCs dd„}|S)Ncs&dd„‰|dk    r"‡fdd„|Dƒ}|S)NcSsL|dk    rH|j}tj|j|j|s$|jr(dnd›|s:|jr:dnd›|d}|Srmrp)Zrvaluer`r'r'r(rišs&ûzM_AsyncpgMultiRange.result_processor.<locals>.to_range_array.<locals>.to_rangecsg|] }ˆ|ƒ‘qSr'r'rIrtr'r(rL§szO_AsyncpgMultiRange.result_processor.<locals>.to_range_array.<locals>.<listcomp>r'rfr'rtr(Úto_range_array™s z;_AsyncpgMultiRange.result_processor.<locals>.to_range_arrayr')r<r=r>rwr'r'r(r?˜sz#_AsyncpgMultiRange.result_processorNrqr'r'r'r(rr|srrc@s$eZdZdd„Zdd„Zdd„ZdS)ÚPGExecutionContext_asyncpgcCs(t||jjj|jjjfƒr$|j ¡dSr:)rMr=rjÚInvalidCachedStatementErrorÚInternalServerErrorÚ_invalidate_schema_cache)r<Úer'r'r(Úhandle_dbapi_exception¯sþþz1PGExecutionContext_asyncpg.handle_dbapi_exceptioncCs*|jr|j ¡|jj|j_|js&dSdSr:)Zisddlr=r{Ú_invalidate_schema_cache_asofÚcursorZcompiled©r<r'r'r(Úpre_exec¹s 
ÿz#PGExecutionContext_asyncpg.pre_execcCs|jjddS)NT)Ú server_side)Z_dbapi_connectionrr€r'r'r(Úcreate_server_side_cursorÄsz4PGExecutionContext_asyncpg.create_server_side_cursorN)r#r$r%r}rrƒr'r'r'r(rx®s
 rxc@s eZdZdS)ÚPGCompiler_asyncpgNrCr'r'r'r(r„Èsr„c@s eZdZdS)ÚPGIdentifierPreparer_asyncpgNrCr'r'r'r(r…Ìsr…c@sxeZdZdZdZdd„Zdd„Zdd„Zd    d
„Zd d „Z    ddd„Z
dd„Z dd„Z dd„Z dd„Zddd„Zdd„Zd S)ÚAsyncAdapt_asyncpg_cursor)Ú_adapt_connectionÚ _connectionÚ_rowsÚ descriptionÚ    arraysizeÚrowcountÚ_cursorr~FcCs6||_|j|_g|_d|_d|_d|_d|_d|_dS)Nréÿÿÿÿr)r‡rˆr‰rrŠr‹rŒr~©r<Úadapt_connectionr'r'r(Ú__init__Þsz"AsyncAdapt_asyncpg_cursor.__init__cCsg|jdd…<dSr:©r‰r€r'r'r(ÚcloseèszAsyncAdapt_asyncpg_cursor.closecCs|j |¡dSr:)r‡Ú_handle_exception©r<Úerrorr'r'r(r”ësz+AsyncAdapt_asyncpg_cursor._handle_exceptionc     Ãs|j}|j4IdHšî|js*| ¡IdH|dkr6d}zš| ||j¡IdH\}}|rfdd„|Dƒ|_nd|_|jrŒ|j|ŽIdH|_    d|_
nB|j |ŽIdH|_ |  ¡}t d|¡}|rÈt| d¡ƒ|_
nd|_
Wn,tk
rü}z| |¡W5d}~XYnXW5QIdHRXdS)Nr'c    Ss$g|]}|j|jjdddddf‘qSr:)ÚnamervÚoid)rJÚattrr'r'r(rLÿs
øùzBAsyncAdapt_asyncpg_cursor._prepare_and_execute.<locals>.<listcomp>rŽz)(?:SELECT|UPDATE|DELETE|INSERT \d+) (\d+)r)r‡Ú_execute_mutexÚ_startedÚ_start_transactionÚ_preparer~rŠr‚rrrŒÚfetchr‰Z get_statusmsgÚreÚmatchÚintÚgroupÚ    Exceptionr”)    r<Ú    operationÚ
parametersrÚ prepared_stmtÚ
attributesÚstatusÚregr–r'r'r(Ú_prepare_and_executeîs<ÿ
ÿ
z.AsyncAdapt_asyncpg_cursor._prepare_and_executec Ãs¦|j}|j4IdHš‚| |j¡IdH|js<| ¡IdHz(|j ||¡IdHWW5QIdHR£Stk
r}z|     |¡W5d}~XYnXW5QIdHRXdSr:)
r‡ršÚ_check_type_cache_invalidationr~r›rœrˆÚ executemanyr£r”)r<r¤Úseq_of_parametersrr–r'r'r(Ú _executemany sÿ
ÿz&AsyncAdapt_asyncpg_cursor._executemanyNcCs|j | ||¡¡dSr:)r‡Úawait_rª)r<r¤r¥r'r'r(Úexecute2s
ÿz!AsyncAdapt_asyncpg_cursor.executecCs|j | ||¡¡Sr:)r‡r¯r®©r<r¤r­r'r'r(r¬7s
ÿz%AsyncAdapt_asyncpg_cursor.executemanycGs
tƒ‚dSr:©ÚNotImplementedError)r<Z
inputsizesr'r'r(Ú setinputsizes<sz'AsyncAdapt_asyncpg_cursor.setinputsizesccs|jr|j d¡VqdS©Nr©r‰Úpopr€r'r'r(Ú__iter__?sz"AsyncAdapt_asyncpg_cursor.__iter__cCs|jr|j d¡SdSdSrµr¶r€r'r'r(ÚfetchoneCs z"AsyncAdapt_asyncpg_cursor.fetchonecCs8|dkr|j}|jd|…}|j|d…|jdd…<|Srµ)r‹r‰)r<ÚsizeÚretvalr'r'r(Ú    fetchmanyIs
z#AsyncAdapt_asyncpg_cursor.fetchmanycCs |jdd…}g|jdd…<|Sr:r’)r<r»r'r'r(ÚfetchallQsz"AsyncAdapt_asyncpg_cursor.fetchall)N)N)r#r$r%Ú    __slots__r‚r‘r“r”rªr®r°r¬r´r¸r¹r¼r½r'r'r'r(r†Ðs 
2
 
r†csneZdZdZdZ‡fdd„Zdd„Zdd„Zd    d
„Zd d „Z    d d„Z
ddd„Z dd„Z dd„Z dd„Z‡ZS)ÚAsyncAdapt_asyncpg_ss_cursorT)Ú
_rowbuffercstƒ |¡d|_dSr:)Úsuperr‘rÀr©Ú    __class__r'r(r‘\s z%AsyncAdapt_asyncpg_ss_cursor.__init__cCsd|_d|_dSr:)rrÀr€r'r'r(r“`sz"AsyncAdapt_asyncpg_ss_cursor.closecCs$|j |j d¡¡}t |¡|_dS)Né2)r‡r¯rržÚ collectionsÚdequerÀ)r<Znew_rowsr'r'r(Ú _buffer_rowsdsz)AsyncAdapt_asyncpg_ss_cursor._buffer_rowscCs|Sr:r'r€r'r'r(Ú    __aiter__hsz&AsyncAdapt_asyncpg_ss_cursor.__aiter__cCs8|js| ¡|jr"|j ¡Vq| ¡|jsq4qdSr:©rÀrÇÚpopleftr€r'r'r(Ú    __anext__ksz&AsyncAdapt_asyncpg_ss_cursor.__anext__cCs"|js| ¡|jsdS|j ¡Sr:rÉr€r'r'r(r¹ws
z%AsyncAdapt_asyncpg_ss_cursor.fetchoneNcCsz|dkr| ¡S|js| ¡t|jƒ}t|ƒ}||krV| |j |j     ||¡¡¡|d|…}t
  ||d…¡|_|Srµ) r½rÀrÇÚlistÚlenÚextendr‡r¯rržrÅrÆ)r<rºÚbufZlbÚresultr'r'r(r¼~s
ÿ z&AsyncAdapt_asyncpg_ss_cursor.fetchmanycCs,t|jƒt|j | ¡¡ƒ}|j ¡|Sr:)rÌrÀr‡r¯Ú_allÚclear)r<Úretr'r'r(r½s
 
ÿ
z%AsyncAdapt_asyncpg_ss_cursor.fetchallcÃs0g}|j d¡IdH}|r,| |¡qqq,q|S)Niè)rržrÎ)r<ÚrowsÚbatchr'r'r(rїs
z!AsyncAdapt_asyncpg_ss_cursor._allcCs tdƒ‚dS)Nz2server side cursor doesn't support executemany yetr²r±r'r'r(r¬¥sÿz(AsyncAdapt_asyncpg_ss_cursor.executemany)N)r#r$r%r‚r¾r‘r“rÇrÈrËr¹r¼r½rÑr¬Ú __classcell__r'r'rÂr(r¿Ws  
r¿c@s¢eZdZdZeeƒZd"dd„Zdd„Zdd    „Z    d
d „Z
e d d „ƒZ e j dd „ƒZ dd„Zdd„Zdd„Zd#dd„Zdd„Zdd„Zdd„Zdd„Zed d!„ƒZdS)$ÚAsyncAdapt_asyncpg_connection) rjÚisolation_levelÚ_isolation_settingÚreadonlyÚ
deferrableÚ _transactionr›Ú_prepared_statement_cacheÚ_prepared_statement_name_funcr~ršédNcCst||_||_d|_|_d|_d|_d|_d|_t ¡|_    t
  ¡|_ |rVt  |¡|_nd|_|rh||_n|j|_dS)NÚread_committedF)rjrˆrØrÙrÚrÛrÜr›Útimer~rÚLockršrZLRUCacherÝrÞÚ_default_name_func)r<rjÚ
connectionÚprepared_statement_cache_sizeÚprepared_statement_name_funcr'r'r(r‘¼s" 
 
ÿz&AsyncAdapt_asyncpg_connection.__init__cÃs$||jkr |j ¡IdH||_dSr:)r~rˆZreload_schema_state)r<Úinvalidate_timestampr'r'r(r«Ùs
z<AsyncAdapt_asyncpg_connection._check_type_cache_invalidationcÃsª| |¡IdH|j}|dkrH|jj|| ¡dIdH}| ¡}||fS||krn||\}}}||krn||fS|jj|| ¡dIdH}| ¡}||t ¡f||<||fS)N)r—)r«rÝrˆÚpreparerÞZget_attributesrá)r<r¤rçÚcacher¦r§Zcached_timestampr'r'r(rÞs(ÿ ÿ z&AsyncAdapt_asyncpg_connection._preparecCs‚|j ¡rd|_d|_t|tjƒsz|jj}t    |ƒj
D]>}||kr4||dt    |ƒ|fƒ}t |ddƒ|_ |_ ||‚q4|‚n|‚dS)NFz%s: %sÚsqlstate)rˆÚ    is_closedrÜr›rMÚAsyncAdapt_asyncpg_dbapiÚErrorrjÚ_asyncpg_error_translatervÚ__mro__ÚgetattrZpgcoderê)r<r–Zexception_mappingZsuper_Ztranslated_errorr'r'r(r”ýs 
 ÿ
þz/AsyncAdapt_asyncpg_connection._handle_exceptioncCs
|jdkS©NÚ
autocommit)rØr€r'r'r(ròsz(AsyncAdapt_asyncpg_connection.autocommitcCs|r d|_n|j|_dSrñ)rØrÙ©r<rNr'r'r(ròsc
CsHz| |j d¡¡}Wn,tk
rB}z| |¡W5d}~XYnXdS)Nú;)r¯rˆZfetchrowr£r”)r<Ú_r–r'r'r(Úpingsz"AsyncAdapt_asyncpg_connection.pingcCs|jr| ¡||_|_dSr:)r›ÚrollbackrØrÙ)r<Úlevelr'r'r(Úset_isolation_level$sz1AsyncAdapt_asyncpg_connection.set_isolation_levelc
Ãst|jdkrdSz.|jj|j|j|jd|_|j ¡IdHWn,tk
rh}z| |¡W5d}~XYnXd|_    dS)Nrò)Z    isolationrÚrÛT)
rØrˆZ transactionrÚrÛrÜÚstartr£r”r›r•r'r'r(rœ)s
ýz0AsyncAdapt_asyncpg_connection._start_transactionFcCs|r t|ƒSt|ƒSdSr:)r¿r†)r<r‚r'r'r(r9sz$AsyncAdapt_asyncpg_connection.cursorc
Cs`|jr\zFz| |j ¡¡Wn,tk
rH}z| |¡W5d}~XYnXW5d|_d|_XdS©NF)r›rÜr¯r÷r£r”r•r'r'r(r÷?s z&AsyncAdapt_asyncpg_connection.rollbackc
Cs`|jr\zFz| |j ¡¡Wn,tk
rH}z| |¡W5d}~XYnXW5d|_d|_XdSrû)r›rÜr¯Úcommitr£r”r•r'r'r(rüIs z$AsyncAdapt_asyncpg_connection.commitcCs| ¡| |j ¡¡dSr:)r÷r¯rˆr“r€r'r'r(r“Ssz#AsyncAdapt_asyncpg_connection.closecCs|j ¡d|_dSrû)rˆÚ    terminater›r€r'r'r(rýXs
z'AsyncAdapt_asyncpg_connection.terminatecCsdSr:r'r'r'r'r(rã\sz0AsyncAdapt_asyncpg_connection._default_name_func)rßN)F)r#r$r%r¾Ú staticmethodrr¯r‘r«rr”ÚpropertyròÚsetterrörùrœrr÷rür“rýrãr'r'r'r(r׫s,û
 
 
 
 
 
r×c@seZdZdZeeƒZdS)Ú%AsyncAdaptFallback_asyncpg_connectionr'N)r#r$r%r¾rþrr¯r'r'r'r(rasrc@seZdZdd„Zdd„ZGdd„deƒZGdd„deƒZGd    d
„d
eƒZGd d „d eƒZ    Gd d„de    ƒZ
Gdd„de    ƒZ Gdd„de    ƒZ Gdd„de    ƒZ Gdd„de    ƒZGdd„de    ƒZGdd„de
ƒZGdd„deƒZe d¡Ze d¡Ze d¡Zejd d!„ƒZd"d#„Zd$S)%rìcCs||_d|_dS)NÚnumeric_dollar)rkZ
paramstyle©r<rkr'r'r(r‘hsz!AsyncAdapt_asyncpg_dbapi.__init__cOsn| dd¡}| dd¡}| dd¡}t |¡rLt|t|jj||Žƒ||dSt|t|jj||Žƒ||dSdS)NÚasync_fallbackFrårßræ)råræ)    r·rÚasboolrrrkÚconnectr×r)r<Úargr3rrårær'r'r(rls, ÿÿ
üüz AsyncAdapt_asyncpg_dbapi.connectc@s eZdZdS)zAsyncAdapt_asyncpg_dbapi.ErrorNrCr'r'r'r(rí„sríc@s eZdZdS)z AsyncAdapt_asyncpg_dbapi.WarningNrCr'r'r'r(ÚWarning‡src@s eZdZdS)z'AsyncAdapt_asyncpg_dbapi.InterfaceErrorNrCr'r'r'r(ÚInterfaceErrorŠsr    c@s eZdZdS)z&AsyncAdapt_asyncpg_dbapi.DatabaseErrorNrCr'r'r'r(Ú DatabaseErrorsr
c@s eZdZdS)z&AsyncAdapt_asyncpg_dbapi.InternalErrorNrCr'r'r'r(Ú InternalErrorsr c@s eZdZdS)z)AsyncAdapt_asyncpg_dbapi.OperationalErrorNrCr'r'r'r(ÚOperationalError“sr c@s eZdZdS)z)AsyncAdapt_asyncpg_dbapi.ProgrammingErrorNrCr'r'r'r(ÚProgrammingError–sr c@s eZdZdS)z'AsyncAdapt_asyncpg_dbapi.IntegrityErrorNrCr'r'r'r(ÚIntegrityError™src@s eZdZdS)z"AsyncAdapt_asyncpg_dbapi.DataErrorNrCr'r'r'r(Ú    DataErrorœsrc@s eZdZdS)z*AsyncAdapt_asyncpg_dbapi.NotSupportedErrorNrCr'r'r'r(ÚNotSupportedErrorŸsrc@s eZdZdS)z,AsyncAdapt_asyncpg_dbapi.InternalServerErrorNrCr'r'r'r(rz¢srzcseZdZ‡fdd„Z‡ZS)z4AsyncAdapt_asyncpg_dbapi.InvalidCachedStatementErrorcstƒ |d¡dS)Nzc (SQLAlchemy asyncpg dialect will now invalidate all prepared caches in response to this exception))rÁr‘)r<ÚmessagerÂr'r(r‘¦sÿz=AsyncAdapt_asyncpg_dbapi.InvalidCachedStatementError.__init__)r#r$r%r‘rÖr'r'rÂr(ry¥sryÚSTRINGÚNUMBERÚDATETIMEc CsHddl}|jj|j|jj|j|jj|j|jj|j|jj    |j    |jj
|j
iSrµ) rkÚ
exceptionsZ!IntegrityConstraintViolationErrorrZ PostgresErrorríZSyntaxOrAccessErrorr r    ryrzrr'r'r(rî²súz1AsyncAdapt_asyncpg_dbapi._asyncpg_error_translatecCs|Sr:r'rór'r'r(ÚBinary¿szAsyncAdapt_asyncpg_dbapi.BinaryN)r#r$r%r‘rr£rírr    r
r r r rrrrzryrÚsymbolrrrÚmemoized_propertyrîrr'r'r'r(rìgs(    
 
 
 
rìc7s eZdZdZdZdZdZdZdZdZ    e
Z e Z eZe ejejeejeeeejeejeeje ej!e"e#e"ej$e%ej&e'ej(e)ej*e+ej,e-ej.e/ej0e1e2j3e4ej.j5e6ej.j7e8ej.j9e:ej.j;e<ej=e>e?e@eAeBejCeDeEjFeGeEjHeIi¡ZdZJdZKdd„ZLejMdd    „ƒZNeOd
d „ƒZPejMd d „ƒZQdd„ZRdd„ZSdd„ZTdd„ZUdd„ZVdd„ZWddœdd„ZXdd„ZYd d!„ZZeOd"d#„ƒZ[d$d%„Z\d&d'„Z]d(d)„Z^‡fd*d+„Z_d,d-„Z`‡ZaS).ÚPGDialect_asyncpgrkTrFrcCst ¡|_dSr:)rár~r€r'r'r(r{ôsz*PGDialect_asyncpg._invalidate_schema_cachecCs8|jr0t|jdƒr0tdd„t d|jj¡DƒƒSdSdS)NÚ __version__cSsg|] }t|ƒ‘qSr')r¡)rJÚxr'r'r(rLûsÿz4PGDialect_asyncpg._dbapi_version.<locals>.<listcomp>z(\d+)(?:[-\.]?|$))écrr)rjÚhasattrÚtuplerŸÚfindallrr€r'r'r(Ú_dbapi_version÷sÿþÿ    z PGDialect_asyncpg._dbapi_versioncCs ttdƒƒS)Nrk)rìÚ
__import__)r1r'r'r(Ú import_dbapiszPGDialect_asyncpg.import_dbapicCsdddddœS)NròràZrepeatable_readZ serializable)Z
AUTOCOMMITzREAD COMMITTEDzREPEATABLE READZ SERIALIZABLEr'r€r'r'r(Ú_isolation_lookup    s
üz#PGDialect_asyncpg._isolation_lookupcCs
t|jƒSr:)rÌr#©r<Údbapi_connectionr'r'r(Úget_isolation_level_valuessz,PGDialect_asyncpg.get_isolation_level_valuescCs| |j|¡dSr:)rùr#)r<r%rør'r'r(rùsz%PGDialect_asyncpg.set_isolation_levelcCs
||_dSr:©rÚ©r<rärNr'r'r(Ú set_readonlyszPGDialect_asyncpg.set_readonlycCs|jSr:r'©r<rär'r'r(Ú get_readonlyszPGDialect_asyncpg.get_readonlycCs
||_dSr:©rÛr(r'r'r(Úset_deferrablesz PGDialect_asyncpg.set_deferrablecCs|jSr:r,r*r'r'r(Úget_deferrable!sz PGDialect_asyncpg.get_deferrableÚNone)ÚreturncCs | ¡dSr:)rýr$r'r'r(Ú do_terminate$szPGDialect_asyncpg.do_terminatecCs<|jdd}| |j¡t |dt¡t |dt¡g|fS)NÚuser)ÚusernameråÚport)Ztranslate_connect_argsÚupdateÚqueryrZcoerce_kw_typer¡)r<ÚurlÚoptsr'r'r(Úcreate_connect_args's
  z%PGDialect_asyncpg.create_connect_argscCs | ¡dS)NT)rör$r'r'r(Údo_ping/szPGDialect_asyncpg.do_pingcCs(|j dd¡}t |¡rtjStjSdS)NrF)r6ÚgetrrrZFallbackAsyncAdaptedQueuePoolZAsyncAdaptedQueuePool)r1r7rr'r'r(Úget_pool_class3s
z PGDialect_asyncpg.get_pool_classcCs,|r|j ¡St||jjƒo&dt|ƒkSdS)Nzconnection is closed)rˆrërMrjr    rH)r<r|rärr'r'r(Ú is_disconnect=s
ÿ
þzPGDialect_asyncpg.is_disconnectcƒs>|j}|jptj‰‡fdd„}|jdtj|dddIdHdS)z®set up JSON codec for asyncpg.
 
        This occurs for all new connections and
        can be overridden by third party dialects.
 
        .. versionadded:: 1.4.27
 
        cs ˆ| ¡ƒSr:©Údecode©Z    bin_value©Z deserializerr'r(Ú _json_decoderRszAPGDialect_asyncpg.setup_asyncpg_json_codec.<locals>._json_decoderrÚ
pg_catalogÚbinary©ÚencoderÚdecoderZschemaÚformatN)rˆÚ_json_deserializerÚ_py_jsonÚloadsÚset_type_codecrHÚencode)r<ÚconnÚasyncpg_connectionrBr'rAr(Úsetup_asyncpg_json_codecEs
  ûz*PGDialect_asyncpg.setup_asyncpg_json_codeccƒsP|j}|jptj‰dd„}|jp$tj‰‡fdd„}|jd||dddId    Hd    S)
z¯set up JSONB codec for asyncpg.
 
        This occurs for all new connections and
        can be overridden by third party dialects.
 
        .. versionadded:: 1.4.27
 
        cSs d| ¡S)Nó)rM)Z    str_valuer'r'r(Ú_jsonb_encoderjszCPGDialect_asyncpg.setup_asyncpg_jsonb_codec.<locals>._jsonb_encodercsˆ|dd… ¡ƒS)Nrr>r@rAr'r(Ú_jsonb_decoderqszCPGDialect_asyncpg.setup_asyncpg_jsonb_codec.<locals>._jsonb_decoderZjsonbrCrDrEN)rˆrIrJrKrL)r<rNrOrRrSr'rAr(Úsetup_asyncpg_jsonb_codec]s
   ûz+PGDialect_asyncpg.setup_asyncpg_jsonb_codeccstƒ ¡‰‡‡fdd„}|S)zöon_connect for asyncpg
 
        A major component of this for asyncpg is to set up type decoders at the
        asyncpg level.
 
        See https://github.com/MagicStack/asyncpg/issues/623 for
        notes on JSON/JSONB implementation.
 
        cs4| ˆ |¡¡| ˆ |¡¡ˆdk    r0ˆ|ƒdSr:)r¯rPrT)rN©r<Z super_connectr'r(r‹sz-PGDialect_asyncpg.on_connect.<locals>.connect)rÁÚ
on_connect)r<rrÂrUr(rV~s
zPGDialect_asyncpg.on_connectcCs|jSr:)rˆr*r'r'r(Úget_driver_connection“sz'PGDialect_asyncpg.get_driver_connection)br#r$r%ZdriverZsupports_statement_cacheZsupports_server_side_cursorsr&Z has_terminateZdefault_paramstyleZsupports_sane_multi_rowcountrxZexecution_ctx_clsr„Zstatement_compilerr…ÚpreparerrZ update_copyrZcolspecsrÚStringr)rr!rr*ÚTimer+ÚDater-ÚDateTimer.ZIntervalr0r ÚBooleanr/ÚIntegerr7Ú
BigIntegerr8ÚNumericrRÚFloatrUÚJSONr9Z LargeBinaryr,rÚJSONBrAÚ JSONPathTyperGÚ JSONIndexTyperBÚJSONIntIndexTyperDÚJSONStrIndexTyperFÚEnumr6rrXrrWÚCHARrYrZ AbstractRangerZZAbstractMultiRangerrZis_asyncr~r{rr r5r"r#r&rùr)r+r-r.r1r9r:r<r=rPrTrVrWrÖr'r'rÂr(rÃs¶æþ
 
 
 
    ! r)bÚ__doc__Ú
__future__rrÅrSrrJrŸráÚtypingrrÚrÚarrayrZPGARRAYÚbaser    r
r r r rrrrrrrÚtypesrrrrZenginerrZsqlrZutil.concurrencyrrrr r!rYr)r*rZr+r,r[r-r\r.r]r/r0r6r^r7r_r8rbr9rcrArerBrfrDrgrFrdrGr`rRrarUrWrXrirYZAbstractRangeImplrZZAbstractMultiRangeImplrrrxr„r…r†r¿r×rrìrr=r'r'r'r(Ú<module>    s”                                 !2T7\U