asyncpg.py 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. # postgresql/asyncpg.py
  2. # Copyright (C) 2005-2022 the SQLAlchemy authors and contributors <see AUTHORS
  3. # file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. r"""
  8. .. dialect:: postgresql+asyncpg
  9. :name: asyncpg
  10. :dbapi: asyncpg
  11. :connectstring: postgresql+asyncpg://user:password@host:port/dbname[?key=value&key=value...]
  12. :url: https://magicstack.github.io/asyncpg/
  13. The asyncpg dialect is SQLAlchemy's first Python asyncio dialect.
  14. Using a special asyncio mediation layer, the asyncpg dialect is usable
  15. as the backend for the :ref:`SQLAlchemy asyncio <asyncio_toplevel>`
  16. extension package.
  17. This dialect should normally be used only with the
  18. :func:`_asyncio.create_async_engine` engine creation function::
  19. from sqlalchemy.ext.asyncio import create_async_engine
  20. engine = create_async_engine("postgresql+asyncpg://user:pass@hostname/dbname")
  21. The dialect can also be run as a "synchronous" dialect within the
  22. :func:`_sa.create_engine` function, which will pass "await" calls into
  23. an ad-hoc event loop. This mode of operation is of **limited use**
  24. and is for special testing scenarios only. The mode can be enabled by
  25. adding the SQLAlchemy-specific flag ``async_fallback`` to the URL
  26. in conjunction with :func:`_sa.create_engine`::
  27. # for testing purposes only; do not use in production!
  28. engine = create_engine("postgresql+asyncpg://user:pass@hostname/dbname?async_fallback=true")
  29. .. versionadded:: 1.4
  30. .. note::
  31. By default asyncpg does not decode the ``json`` and ``jsonb`` types and
  32. returns them as strings. SQLAlchemy sets default type decoder for ``json``
  33. and ``jsonb`` types using the python builtin ``json.loads`` function.
  34. The json implementation used can be changed by setting the attribute
  35. ``json_deserializer`` when creating the engine with
  36. :func:`create_engine` or :func:`create_async_engine`.
  37. .. _asyncpg_prepared_statement_cache:
  38. Prepared Statement Cache
  39. --------------------------
  40. The asyncpg SQLAlchemy dialect makes use of ``asyncpg.connection.prepare()``
  41. for all statements. The prepared statement objects are cached after
  42. construction which appears to grant a 10% or more performance improvement for
  43. statement invocation. The cache is on a per-DBAPI connection basis, which
  44. means that the primary storage for prepared statements is within DBAPI
  45. connections pooled within the connection pool. The size of this cache
  46. defaults to 100 statements per DBAPI connection and may be adjusted using the
  47. ``prepared_statement_cache_size`` DBAPI argument (note that while this argument
  48. is implemented by SQLAlchemy, it is part of the DBAPI emulation portion of the
  49. asyncpg dialect, therefore is handled as a DBAPI argument, not a dialect
  50. argument)::
  51. engine = create_async_engine("postgresql+asyncpg://user:pass@hostname/dbname?prepared_statement_cache_size=500")
  52. To disable the prepared statement cache, use a value of zero::
  53. engine = create_async_engine("postgresql+asyncpg://user:pass@hostname/dbname?prepared_statement_cache_size=0")
  54. .. versionadded:: 1.4.0b2 Added ``prepared_statement_cache_size`` for asyncpg.
  55. .. warning:: The ``asyncpg`` database driver necessarily uses caches for
  56. PostgreSQL type OIDs, which become stale when custom PostgreSQL datatypes
  57. such as ``ENUM`` objects are changed via DDL operations. Additionally,
  58. prepared statements themselves which are optionally cached by SQLAlchemy's
  59. driver as described above may also become "stale" when DDL has been emitted
  60. to the PostgreSQL database which modifies the tables or other objects
  61. involved in a particular prepared statement.
  62. The SQLAlchemy asyncpg dialect will invalidate these caches within its local
  63. process when statements that represent DDL are emitted on a local
  64. connection, but this is only controllable within a single Python process /
  65. database engine. If DDL changes are made from other database engines
  66. and/or processes, a running application may encounter asyncpg exceptions
  67. ``InvalidCachedStatementError`` and/or ``InternalServerError("cache lookup
  68. failed for type <oid>")`` if it refers to pooled database connections which
  69. operated upon the previous structures. The SQLAlchemy asyncpg dialect will
  70. recover from these error cases when the driver raises these exceptions by
  71. clearing its internal caches as well as those of the asyncpg driver in
  72. response to them, but cannot prevent them from being raised in the first
  73. place if the cached prepared statement or asyncpg type caches have gone
  74. stale, nor can it retry the statement as the PostgreSQL transaction is
  75. invalidated when these errors occur.
  76. """ # noqa
  77. import collections
  78. import decimal
  79. import json as _py_json
  80. import re
  81. import time
  82. from . import json
  83. from .base import _DECIMAL_TYPES
  84. from .base import _FLOAT_TYPES
  85. from .base import _INT_TYPES
  86. from .base import ENUM
  87. from .base import INTERVAL
  88. from .base import OID
  89. from .base import PGCompiler
  90. from .base import PGDialect
  91. from .base import PGExecutionContext
  92. from .base import PGIdentifierPreparer
  93. from .base import REGCLASS
  94. from .base import UUID
  95. from ... import exc
  96. from ... import pool
  97. from ... import processors
  98. from ... import util
  99. from ...engine import AdaptedConnection
  100. from ...sql import sqltypes
  101. from ...util.concurrency import asyncio
  102. from ...util.concurrency import await_fallback
  103. from ...util.concurrency import await_only
  104. try:
  105. from uuid import UUID as _python_UUID # noqa
  106. except ImportError:
  107. _python_UUID = None
  108. class AsyncpgTime(sqltypes.Time):
  109. def get_dbapi_type(self, dbapi):
  110. if self.timezone:
  111. return dbapi.TIME_W_TZ
  112. else:
  113. return dbapi.TIME
  114. class AsyncpgDate(sqltypes.Date):
  115. def get_dbapi_type(self, dbapi):
  116. return dbapi.DATE
  117. class AsyncpgDateTime(sqltypes.DateTime):
  118. def get_dbapi_type(self, dbapi):
  119. if self.timezone:
  120. return dbapi.TIMESTAMP_W_TZ
  121. else:
  122. return dbapi.TIMESTAMP
  123. class AsyncpgBoolean(sqltypes.Boolean):
  124. def get_dbapi_type(self, dbapi):
  125. return dbapi.BOOLEAN
  126. class AsyncPgInterval(INTERVAL):
  127. def get_dbapi_type(self, dbapi):
  128. return dbapi.INTERVAL
  129. @classmethod
  130. def adapt_emulated_to_native(cls, interval, **kw):
  131. return AsyncPgInterval(precision=interval.second_precision)
  132. class AsyncPgEnum(ENUM):
  133. def get_dbapi_type(self, dbapi):
  134. return dbapi.ENUM
  135. class AsyncpgInteger(sqltypes.Integer):
  136. def get_dbapi_type(self, dbapi):
  137. return dbapi.INTEGER
  138. class AsyncpgBigInteger(sqltypes.BigInteger):
  139. def get_dbapi_type(self, dbapi):
  140. return dbapi.BIGINTEGER
  141. class AsyncpgJSON(json.JSON):
  142. def get_dbapi_type(self, dbapi):
  143. return dbapi.JSON
  144. def result_processor(self, dialect, coltype):
  145. return None
  146. class AsyncpgJSONB(json.JSONB):
  147. def get_dbapi_type(self, dbapi):
  148. return dbapi.JSONB
  149. def result_processor(self, dialect, coltype):
  150. return None
  151. class AsyncpgJSONIndexType(sqltypes.JSON.JSONIndexType):
  152. def get_dbapi_type(self, dbapi):
  153. raise NotImplementedError("should not be here")
  154. class AsyncpgJSONIntIndexType(sqltypes.JSON.JSONIntIndexType):
  155. def get_dbapi_type(self, dbapi):
  156. return dbapi.INTEGER
  157. class AsyncpgJSONStrIndexType(sqltypes.JSON.JSONStrIndexType):
  158. def get_dbapi_type(self, dbapi):
  159. return dbapi.STRING
  160. class AsyncpgJSONPathType(json.JSONPathType):
  161. def bind_processor(self, dialect):
  162. def process(value):
  163. assert isinstance(value, util.collections_abc.Sequence)
  164. tokens = [util.text_type(elem) for elem in value]
  165. return tokens
  166. return process
  167. class AsyncpgUUID(UUID):
  168. def get_dbapi_type(self, dbapi):
  169. return dbapi.UUID
  170. def bind_processor(self, dialect):
  171. if not self.as_uuid and dialect.use_native_uuid:
  172. def process(value):
  173. if value is not None:
  174. value = _python_UUID(value)
  175. return value
  176. return process
  177. def result_processor(self, dialect, coltype):
  178. if not self.as_uuid and dialect.use_native_uuid:
  179. def process(value):
  180. if value is not None:
  181. value = str(value)
  182. return value
  183. return process
  184. class AsyncpgNumeric(sqltypes.Numeric):
  185. def get_dbapi_type(self, dbapi):
  186. return dbapi.NUMBER
  187. def bind_processor(self, dialect):
  188. return None
  189. def result_processor(self, dialect, coltype):
  190. if self.asdecimal:
  191. if coltype in _FLOAT_TYPES:
  192. return processors.to_decimal_processor_factory(
  193. decimal.Decimal, self._effective_decimal_return_scale
  194. )
  195. elif coltype in _DECIMAL_TYPES or coltype in _INT_TYPES:
  196. # pg8000 returns Decimal natively for 1700
  197. return None
  198. else:
  199. raise exc.InvalidRequestError(
  200. "Unknown PG numeric type: %d" % coltype
  201. )
  202. else:
  203. if coltype in _FLOAT_TYPES:
  204. # pg8000 returns float natively for 701
  205. return None
  206. elif coltype in _DECIMAL_TYPES or coltype in _INT_TYPES:
  207. return processors.to_float
  208. else:
  209. raise exc.InvalidRequestError(
  210. "Unknown PG numeric type: %d" % coltype
  211. )
  212. class AsyncpgFloat(AsyncpgNumeric):
  213. def get_dbapi_type(self, dbapi):
  214. return dbapi.FLOAT
  215. class AsyncpgREGCLASS(REGCLASS):
  216. def get_dbapi_type(self, dbapi):
  217. return dbapi.STRING
  218. class AsyncpgOID(OID):
  219. def get_dbapi_type(self, dbapi):
  220. return dbapi.INTEGER
  221. class PGExecutionContext_asyncpg(PGExecutionContext):
  222. def handle_dbapi_exception(self, e):
  223. if isinstance(
  224. e,
  225. (
  226. self.dialect.dbapi.InvalidCachedStatementError,
  227. self.dialect.dbapi.InternalServerError,
  228. ),
  229. ):
  230. self.dialect._invalidate_schema_cache()
  231. def pre_exec(self):
  232. if self.isddl:
  233. self.dialect._invalidate_schema_cache()
  234. self.cursor._invalidate_schema_cache_asof = (
  235. self.dialect._invalidate_schema_cache_asof
  236. )
  237. if not self.compiled:
  238. return
  239. # we have to exclude ENUM because "enum" not really a "type"
  240. # we can cast to, it has to be the name of the type itself.
  241. # for now we just omit it from casting
  242. self.exclude_set_input_sizes = {AsyncAdapt_asyncpg_dbapi.ENUM}
  243. def create_server_side_cursor(self):
  244. return self._dbapi_connection.cursor(server_side=True)
  245. class PGCompiler_asyncpg(PGCompiler):
  246. pass
  247. class PGIdentifierPreparer_asyncpg(PGIdentifierPreparer):
  248. pass
  249. class AsyncAdapt_asyncpg_cursor:
  250. __slots__ = (
  251. "_adapt_connection",
  252. "_connection",
  253. "_rows",
  254. "description",
  255. "arraysize",
  256. "rowcount",
  257. "_inputsizes",
  258. "_cursor",
  259. "_invalidate_schema_cache_asof",
  260. )
  261. server_side = False
  262. def __init__(self, adapt_connection):
  263. self._adapt_connection = adapt_connection
  264. self._connection = adapt_connection._connection
  265. self._rows = []
  266. self._cursor = None
  267. self.description = None
  268. self.arraysize = 1
  269. self.rowcount = -1
  270. self._inputsizes = None
  271. self._invalidate_schema_cache_asof = 0
  272. def close(self):
  273. self._rows[:] = []
  274. def _handle_exception(self, error):
  275. self._adapt_connection._handle_exception(error)
  276. def _parameter_placeholders(self, params):
  277. if not self._inputsizes:
  278. return tuple("$%d" % idx for idx, _ in enumerate(params, 1))
  279. else:
  280. return tuple(
  281. "$%d::%s" % (idx, typ) if typ else "$%d" % idx
  282. for idx, typ in enumerate(
  283. (_pg_types.get(typ) for typ in self._inputsizes), 1
  284. )
  285. )
  286. async def _prepare_and_execute(self, operation, parameters):
  287. adapt_connection = self._adapt_connection
  288. async with adapt_connection._execute_mutex:
  289. if not adapt_connection._started:
  290. await adapt_connection._start_transaction()
  291. if parameters is not None:
  292. operation = operation % self._parameter_placeholders(
  293. parameters
  294. )
  295. else:
  296. parameters = ()
  297. try:
  298. prepared_stmt, attributes = await adapt_connection._prepare(
  299. operation, self._invalidate_schema_cache_asof
  300. )
  301. if attributes:
  302. self.description = [
  303. (
  304. attr.name,
  305. attr.type.oid,
  306. None,
  307. None,
  308. None,
  309. None,
  310. None,
  311. )
  312. for attr in attributes
  313. ]
  314. else:
  315. self.description = None
  316. if self.server_side:
  317. self._cursor = await prepared_stmt.cursor(*parameters)
  318. self.rowcount = -1
  319. else:
  320. self._rows = await prepared_stmt.fetch(*parameters)
  321. status = prepared_stmt.get_statusmsg()
  322. reg = re.match(
  323. r"(?:UPDATE|DELETE|INSERT \d+) (\d+)", status
  324. )
  325. if reg:
  326. self.rowcount = int(reg.group(1))
  327. else:
  328. self.rowcount = -1
  329. except Exception as error:
  330. self._handle_exception(error)
  331. async def _executemany(self, operation, seq_of_parameters):
  332. adapt_connection = self._adapt_connection
  333. async with adapt_connection._execute_mutex:
  334. await adapt_connection._check_type_cache_invalidation(
  335. self._invalidate_schema_cache_asof
  336. )
  337. if not adapt_connection._started:
  338. await adapt_connection._start_transaction()
  339. operation = operation % self._parameter_placeholders(
  340. seq_of_parameters[0]
  341. )
  342. try:
  343. return await self._connection.executemany(
  344. operation, seq_of_parameters
  345. )
  346. except Exception as error:
  347. self._handle_exception(error)
  348. def execute(self, operation, parameters=None):
  349. self._adapt_connection.await_(
  350. self._prepare_and_execute(operation, parameters)
  351. )
  352. def executemany(self, operation, seq_of_parameters):
  353. return self._adapt_connection.await_(
  354. self._executemany(operation, seq_of_parameters)
  355. )
  356. def setinputsizes(self, *inputsizes):
  357. self._inputsizes = inputsizes
  358. def __iter__(self):
  359. while self._rows:
  360. yield self._rows.pop(0)
  361. def fetchone(self):
  362. if self._rows:
  363. return self._rows.pop(0)
  364. else:
  365. return None
  366. def fetchmany(self, size=None):
  367. if size is None:
  368. size = self.arraysize
  369. retval = self._rows[0:size]
  370. self._rows[:] = self._rows[size:]
  371. return retval
  372. def fetchall(self):
  373. retval = self._rows[:]
  374. self._rows[:] = []
  375. return retval
  376. class AsyncAdapt_asyncpg_ss_cursor(AsyncAdapt_asyncpg_cursor):
  377. server_side = True
  378. __slots__ = ("_rowbuffer",)
  379. def __init__(self, adapt_connection):
  380. super(AsyncAdapt_asyncpg_ss_cursor, self).__init__(adapt_connection)
  381. self._rowbuffer = None
  382. def close(self):
  383. self._cursor = None
  384. self._rowbuffer = None
  385. def _buffer_rows(self):
  386. new_rows = self._adapt_connection.await_(self._cursor.fetch(50))
  387. self._rowbuffer = collections.deque(new_rows)
  388. def __aiter__(self):
  389. return self
  390. async def __anext__(self):
  391. if not self._rowbuffer:
  392. self._buffer_rows()
  393. while True:
  394. while self._rowbuffer:
  395. yield self._rowbuffer.popleft()
  396. self._buffer_rows()
  397. if not self._rowbuffer:
  398. break
  399. def fetchone(self):
  400. if not self._rowbuffer:
  401. self._buffer_rows()
  402. if not self._rowbuffer:
  403. return None
  404. return self._rowbuffer.popleft()
  405. def fetchmany(self, size=None):
  406. if size is None:
  407. return self.fetchall()
  408. if not self._rowbuffer:
  409. self._buffer_rows()
  410. buf = list(self._rowbuffer)
  411. lb = len(buf)
  412. if size > lb:
  413. buf.extend(
  414. self._adapt_connection.await_(self._cursor.fetch(size - lb))
  415. )
  416. result = buf[0:size]
  417. self._rowbuffer = collections.deque(buf[size:])
  418. return result
  419. def fetchall(self):
  420. ret = list(self._rowbuffer) + list(
  421. self._adapt_connection.await_(self._all())
  422. )
  423. self._rowbuffer.clear()
  424. return ret
  425. async def _all(self):
  426. rows = []
  427. # TODO: looks like we have to hand-roll some kind of batching here.
  428. # hardcoding for the moment but this should be improved.
  429. while True:
  430. batch = await self._cursor.fetch(1000)
  431. if batch:
  432. rows.extend(batch)
  433. continue
  434. else:
  435. break
  436. return rows
  437. def executemany(self, operation, seq_of_parameters):
  438. raise NotImplementedError(
  439. "server side cursor doesn't support executemany yet"
  440. )
  441. class AsyncAdapt_asyncpg_connection(AdaptedConnection):
  442. __slots__ = (
  443. "dbapi",
  444. "_connection",
  445. "isolation_level",
  446. "_isolation_setting",
  447. "readonly",
  448. "deferrable",
  449. "_transaction",
  450. "_started",
  451. "_prepared_statement_cache",
  452. "_invalidate_schema_cache_asof",
  453. "_execute_mutex",
  454. )
  455. await_ = staticmethod(await_only)
  456. def __init__(self, dbapi, connection, prepared_statement_cache_size=100):
  457. self.dbapi = dbapi
  458. self._connection = connection
  459. self.isolation_level = self._isolation_setting = "read_committed"
  460. self.readonly = False
  461. self.deferrable = False
  462. self._transaction = None
  463. self._started = False
  464. self._invalidate_schema_cache_asof = time.time()
  465. self._execute_mutex = asyncio.Lock()
  466. if prepared_statement_cache_size:
  467. self._prepared_statement_cache = util.LRUCache(
  468. prepared_statement_cache_size
  469. )
  470. else:
  471. self._prepared_statement_cache = None
  472. async def _check_type_cache_invalidation(self, invalidate_timestamp):
  473. if invalidate_timestamp > self._invalidate_schema_cache_asof:
  474. await self._connection.reload_schema_state()
  475. self._invalidate_schema_cache_asof = invalidate_timestamp
  476. async def _prepare(self, operation, invalidate_timestamp):
  477. await self._check_type_cache_invalidation(invalidate_timestamp)
  478. cache = self._prepared_statement_cache
  479. if cache is None:
  480. prepared_stmt = await self._connection.prepare(operation)
  481. attributes = prepared_stmt.get_attributes()
  482. return prepared_stmt, attributes
  483. # asyncpg uses a type cache for the "attributes" which seems to go
  484. # stale independently of the PreparedStatement itself, so place that
  485. # collection in the cache as well.
  486. if operation in cache:
  487. prepared_stmt, attributes, cached_timestamp = cache[operation]
  488. # preparedstatements themselves also go stale for certain DDL
  489. # changes such as size of a VARCHAR changing, so there is also
  490. # a cross-connection invalidation timestamp
  491. if cached_timestamp > invalidate_timestamp:
  492. return prepared_stmt, attributes
  493. prepared_stmt = await self._connection.prepare(operation)
  494. attributes = prepared_stmt.get_attributes()
  495. cache[operation] = (prepared_stmt, attributes, time.time())
  496. return prepared_stmt, attributes
  497. def _handle_exception(self, error):
  498. if self._connection.is_closed():
  499. self._transaction = None
  500. self._started = False
  501. if not isinstance(error, AsyncAdapt_asyncpg_dbapi.Error):
  502. exception_mapping = self.dbapi._asyncpg_error_translate
  503. for super_ in type(error).__mro__:
  504. if super_ in exception_mapping:
  505. translated_error = exception_mapping[super_](
  506. "%s: %s" % (type(error), error)
  507. )
  508. translated_error.pgcode = (
  509. translated_error.sqlstate
  510. ) = getattr(error, "sqlstate", None)
  511. raise translated_error from error
  512. else:
  513. raise error
  514. else:
  515. raise error
  516. @property
  517. def autocommit(self):
  518. return self.isolation_level == "autocommit"
  519. @autocommit.setter
  520. def autocommit(self, value):
  521. if value:
  522. self.isolation_level = "autocommit"
  523. else:
  524. self.isolation_level = self._isolation_setting
  525. def set_isolation_level(self, level):
  526. if self._started:
  527. self.rollback()
  528. self.isolation_level = self._isolation_setting = level
  529. async def _start_transaction(self):
  530. if self.isolation_level == "autocommit":
  531. return
  532. try:
  533. self._transaction = self._connection.transaction(
  534. isolation=self.isolation_level,
  535. readonly=self.readonly,
  536. deferrable=self.deferrable,
  537. )
  538. await self._transaction.start()
  539. except Exception as error:
  540. self._handle_exception(error)
  541. else:
  542. self._started = True
  543. def cursor(self, server_side=False):
  544. if server_side:
  545. return AsyncAdapt_asyncpg_ss_cursor(self)
  546. else:
  547. return AsyncAdapt_asyncpg_cursor(self)
  548. def rollback(self):
  549. if self._started:
  550. try:
  551. self.await_(self._transaction.rollback())
  552. except Exception as error:
  553. self._handle_exception(error)
  554. finally:
  555. self._transaction = None
  556. self._started = False
  557. def commit(self):
  558. if self._started:
  559. try:
  560. self.await_(self._transaction.commit())
  561. except Exception as error:
  562. self._handle_exception(error)
  563. finally:
  564. self._transaction = None
  565. self._started = False
  566. def close(self):
  567. self.rollback()
  568. self.await_(self._connection.close())
  569. class AsyncAdaptFallback_asyncpg_connection(AsyncAdapt_asyncpg_connection):
  570. __slots__ = ()
  571. await_ = staticmethod(await_fallback)
  572. class AsyncAdapt_asyncpg_dbapi:
  573. def __init__(self, asyncpg):
  574. self.asyncpg = asyncpg
  575. self.paramstyle = "format"
  576. def connect(self, *arg, **kw):
  577. async_fallback = kw.pop("async_fallback", False)
  578. prepared_statement_cache_size = kw.pop(
  579. "prepared_statement_cache_size", 100
  580. )
  581. if util.asbool(async_fallback):
  582. return AsyncAdaptFallback_asyncpg_connection(
  583. self,
  584. await_fallback(self.asyncpg.connect(*arg, **kw)),
  585. prepared_statement_cache_size=prepared_statement_cache_size,
  586. )
  587. else:
  588. return AsyncAdapt_asyncpg_connection(
  589. self,
  590. await_only(self.asyncpg.connect(*arg, **kw)),
  591. prepared_statement_cache_size=prepared_statement_cache_size,
  592. )
  593. class Error(Exception):
  594. pass
  595. class Warning(Exception): # noqa
  596. pass
  597. class InterfaceError(Error):
  598. pass
  599. class DatabaseError(Error):
  600. pass
  601. class InternalError(DatabaseError):
  602. pass
  603. class OperationalError(DatabaseError):
  604. pass
  605. class ProgrammingError(DatabaseError):
  606. pass
  607. class IntegrityError(DatabaseError):
  608. pass
  609. class DataError(DatabaseError):
  610. pass
  611. class NotSupportedError(DatabaseError):
  612. pass
  613. class InternalServerError(InternalError):
  614. pass
  615. class InvalidCachedStatementError(NotSupportedError):
  616. def __init__(self, message):
  617. super(
  618. AsyncAdapt_asyncpg_dbapi.InvalidCachedStatementError, self
  619. ).__init__(
  620. message + " (SQLAlchemy asyncpg dialect will now invalidate "
  621. "all prepared caches in response to this exception)",
  622. )
  623. @util.memoized_property
  624. def _asyncpg_error_translate(self):
  625. import asyncpg
  626. return {
  627. asyncpg.exceptions.IntegrityConstraintViolationError: self.IntegrityError, # noqa: E501
  628. asyncpg.exceptions.PostgresError: self.Error,
  629. asyncpg.exceptions.SyntaxOrAccessError: self.ProgrammingError,
  630. asyncpg.exceptions.InterfaceError: self.InterfaceError,
  631. asyncpg.exceptions.InvalidCachedStatementError: self.InvalidCachedStatementError, # noqa: E501
  632. asyncpg.exceptions.InternalServerError: self.InternalServerError,
  633. }
  634. def Binary(self, value):
  635. return value
  636. STRING = util.symbol("STRING")
  637. TIMESTAMP = util.symbol("TIMESTAMP")
  638. TIMESTAMP_W_TZ = util.symbol("TIMESTAMP_W_TZ")
  639. TIME = util.symbol("TIME")
  640. TIME_W_TZ = util.symbol("TIME_W_TZ")
  641. DATE = util.symbol("DATE")
  642. INTERVAL = util.symbol("INTERVAL")
  643. NUMBER = util.symbol("NUMBER")
  644. FLOAT = util.symbol("FLOAT")
  645. BOOLEAN = util.symbol("BOOLEAN")
  646. INTEGER = util.symbol("INTEGER")
  647. BIGINTEGER = util.symbol("BIGINTEGER")
  648. BYTES = util.symbol("BYTES")
  649. DECIMAL = util.symbol("DECIMAL")
  650. JSON = util.symbol("JSON")
  651. JSONB = util.symbol("JSONB")
  652. ENUM = util.symbol("ENUM")
  653. UUID = util.symbol("UUID")
  654. BYTEA = util.symbol("BYTEA")
  655. DATETIME = TIMESTAMP
  656. BINARY = BYTEA
  657. _pg_types = {
  658. AsyncAdapt_asyncpg_dbapi.STRING: "varchar",
  659. AsyncAdapt_asyncpg_dbapi.TIMESTAMP: "timestamp",
  660. AsyncAdapt_asyncpg_dbapi.TIMESTAMP_W_TZ: "timestamp with time zone",
  661. AsyncAdapt_asyncpg_dbapi.DATE: "date",
  662. AsyncAdapt_asyncpg_dbapi.TIME: "time",
  663. AsyncAdapt_asyncpg_dbapi.TIME_W_TZ: "time with time zone",
  664. AsyncAdapt_asyncpg_dbapi.INTERVAL: "interval",
  665. AsyncAdapt_asyncpg_dbapi.NUMBER: "numeric",
  666. AsyncAdapt_asyncpg_dbapi.FLOAT: "float",
  667. AsyncAdapt_asyncpg_dbapi.BOOLEAN: "bool",
  668. AsyncAdapt_asyncpg_dbapi.INTEGER: "integer",
  669. AsyncAdapt_asyncpg_dbapi.BIGINTEGER: "bigint",
  670. AsyncAdapt_asyncpg_dbapi.BYTES: "bytes",
  671. AsyncAdapt_asyncpg_dbapi.DECIMAL: "decimal",
  672. AsyncAdapt_asyncpg_dbapi.JSON: "json",
  673. AsyncAdapt_asyncpg_dbapi.JSONB: "jsonb",
  674. AsyncAdapt_asyncpg_dbapi.ENUM: "enum",
  675. AsyncAdapt_asyncpg_dbapi.UUID: "uuid",
  676. AsyncAdapt_asyncpg_dbapi.BYTEA: "bytea",
  677. }
  678. class PGDialect_asyncpg(PGDialect):
  679. driver = "asyncpg"
  680. supports_statement_cache = True
  681. supports_unicode_statements = True
  682. supports_server_side_cursors = True
  683. supports_unicode_binds = True
  684. default_paramstyle = "format"
  685. supports_sane_multi_rowcount = False
  686. execution_ctx_cls = PGExecutionContext_asyncpg
  687. statement_compiler = PGCompiler_asyncpg
  688. preparer = PGIdentifierPreparer_asyncpg
  689. use_setinputsizes = True
  690. use_native_uuid = True
  691. colspecs = util.update_copy(
  692. PGDialect.colspecs,
  693. {
  694. sqltypes.Time: AsyncpgTime,
  695. sqltypes.Date: AsyncpgDate,
  696. sqltypes.DateTime: AsyncpgDateTime,
  697. sqltypes.Interval: AsyncPgInterval,
  698. INTERVAL: AsyncPgInterval,
  699. UUID: AsyncpgUUID,
  700. sqltypes.Boolean: AsyncpgBoolean,
  701. sqltypes.Integer: AsyncpgInteger,
  702. sqltypes.BigInteger: AsyncpgBigInteger,
  703. sqltypes.Numeric: AsyncpgNumeric,
  704. sqltypes.Float: AsyncpgFloat,
  705. sqltypes.JSON: AsyncpgJSON,
  706. json.JSONB: AsyncpgJSONB,
  707. sqltypes.JSON.JSONPathType: AsyncpgJSONPathType,
  708. sqltypes.JSON.JSONIndexType: AsyncpgJSONIndexType,
  709. sqltypes.JSON.JSONIntIndexType: AsyncpgJSONIntIndexType,
  710. sqltypes.JSON.JSONStrIndexType: AsyncpgJSONStrIndexType,
  711. sqltypes.Enum: AsyncPgEnum,
  712. OID: AsyncpgOID,
  713. REGCLASS: AsyncpgREGCLASS,
  714. },
  715. )
  716. is_async = True
  717. _invalidate_schema_cache_asof = 0
  718. def _invalidate_schema_cache(self):
  719. self._invalidate_schema_cache_asof = time.time()
  720. @util.memoized_property
  721. def _dbapi_version(self):
  722. if self.dbapi and hasattr(self.dbapi, "__version__"):
  723. return tuple(
  724. [
  725. int(x)
  726. for x in re.findall(
  727. r"(\d+)(?:[-\.]?|$)", self.dbapi.__version__
  728. )
  729. ]
  730. )
  731. else:
  732. return (99, 99, 99)
  733. @classmethod
  734. def dbapi(cls):
  735. return AsyncAdapt_asyncpg_dbapi(__import__("asyncpg"))
  736. @util.memoized_property
  737. def _isolation_lookup(self):
  738. return {
  739. "AUTOCOMMIT": "autocommit",
  740. "READ COMMITTED": "read_committed",
  741. "REPEATABLE READ": "repeatable_read",
  742. "SERIALIZABLE": "serializable",
  743. }
  744. def set_isolation_level(self, connection, level):
  745. try:
  746. level = self._isolation_lookup[level.replace("_", " ")]
  747. except KeyError as err:
  748. util.raise_(
  749. exc.ArgumentError(
  750. "Invalid value '%s' for isolation_level. "
  751. "Valid isolation levels for %s are %s"
  752. % (level, self.name, ", ".join(self._isolation_lookup))
  753. ),
  754. replace_context=err,
  755. )
  756. connection.set_isolation_level(level)
  757. def set_readonly(self, connection, value):
  758. connection.readonly = value
  759. def get_readonly(self, connection):
  760. return connection.readonly
  761. def set_deferrable(self, connection, value):
  762. connection.deferrable = value
  763. def get_deferrable(self, connection):
  764. return connection.deferrable
  765. def create_connect_args(self, url):
  766. opts = url.translate_connect_args(username="user")
  767. opts.update(url.query)
  768. util.coerce_kw_type(opts, "prepared_statement_cache_size", int)
  769. util.coerce_kw_type(opts, "port", int)
  770. return ([], opts)
  771. @classmethod
  772. def get_pool_class(cls, url):
  773. async_fallback = url.query.get("async_fallback", False)
  774. if util.asbool(async_fallback):
  775. return pool.FallbackAsyncAdaptedQueuePool
  776. else:
  777. return pool.AsyncAdaptedQueuePool
  778. def is_disconnect(self, e, connection, cursor):
  779. if connection:
  780. return connection._connection.is_closed()
  781. else:
  782. return isinstance(
  783. e, self.dbapi.InterfaceError
  784. ) and "connection is closed" in str(e)
  785. def do_set_input_sizes(self, cursor, list_of_tuples, context):
  786. if self.positional:
  787. cursor.setinputsizes(
  788. *[dbtype for key, dbtype, sqltype in list_of_tuples]
  789. )
  790. else:
  791. cursor.setinputsizes(
  792. **{
  793. key: dbtype
  794. for key, dbtype, sqltype in list_of_tuples
  795. if dbtype
  796. }
  797. )
  798. async def setup_asyncpg_json_codec(self, conn):
  799. """set up JSON codec for asyncpg.
  800. This occurs for all new connections and
  801. can be overridden by third party dialects.
  802. .. versionadded:: 1.4.27
  803. """
  804. asyncpg_connection = conn._connection
  805. deserializer = self._json_deserializer or _py_json.loads
  806. def _json_decoder(bin_value):
  807. return deserializer(bin_value.decode())
  808. await asyncpg_connection.set_type_codec(
  809. "json",
  810. encoder=str.encode,
  811. decoder=_json_decoder,
  812. schema="pg_catalog",
  813. format="binary",
  814. )
  815. async def setup_asyncpg_jsonb_codec(self, conn):
  816. """set up JSONB codec for asyncpg.
  817. This occurs for all new connections and
  818. can be overridden by third party dialects.
  819. .. versionadded:: 1.4.27
  820. """
  821. asyncpg_connection = conn._connection
  822. deserializer = self._json_deserializer or _py_json.loads
  823. def _jsonb_encoder(str_value):
  824. # \x01 is the prefix for jsonb used by PostgreSQL.
  825. # asyncpg requires it when format='binary'
  826. return b"\x01" + str_value.encode()
  827. deserializer = self._json_deserializer or _py_json.loads
  828. def _jsonb_decoder(bin_value):
  829. # the byte is the \x01 prefix for jsonb used by PostgreSQL.
  830. # asyncpg returns it when format='binary'
  831. return deserializer(bin_value[1:].decode())
  832. await asyncpg_connection.set_type_codec(
  833. "jsonb",
  834. encoder=_jsonb_encoder,
  835. decoder=_jsonb_decoder,
  836. schema="pg_catalog",
  837. format="binary",
  838. )
  839. def on_connect(self):
  840. """on_connect for asyncpg
  841. A major component of this for asyncpg is to set up type decoders at the
  842. asyncpg level.
  843. See https://github.com/MagicStack/asyncpg/issues/623 for
  844. notes on JSON/JSONB implementation.
  845. """
  846. super_connect = super(PGDialect_asyncpg, self).on_connect()
  847. def connect(conn):
  848. conn.await_(self.setup_asyncpg_json_codec(conn))
  849. conn.await_(self.setup_asyncpg_jsonb_codec(conn))
  850. if super_connect is not None:
  851. super_connect(conn)
  852. return connect
  853. def get_driver_connection(self, connection):
  854. return connection._connection
  855. dialect = PGDialect_asyncpg