aiosqlite.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. # dialects/sqlite/aiosqlite.py
  2. # Copyright (C) 2005-2026 the SQLAlchemy authors and contributors
  3. # <see AUTHORS 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:: sqlite+aiosqlite
  9. :name: aiosqlite
  10. :dbapi: aiosqlite
  11. :connectstring: sqlite+aiosqlite:///file_path
  12. :url: https://pypi.org/project/aiosqlite/
  13. The aiosqlite dialect provides support for the SQLAlchemy asyncio interface
  14. running on top of pysqlite.
  15. aiosqlite is a wrapper around pysqlite that uses a background thread for
  16. each connection. It does not actually use non-blocking IO, as SQLite
  17. databases are not socket-based. However it does provide a working asyncio
  18. interface that's useful for testing and prototyping purposes.
  19. Using a special asyncio mediation layer, the aiosqlite dialect is usable
  20. as the backend for the :ref:`SQLAlchemy asyncio <asyncio_toplevel>`
  21. extension package.
  22. This dialect should normally be used only with the
  23. :func:`_asyncio.create_async_engine` engine creation function::
  24. from sqlalchemy.ext.asyncio import create_async_engine
  25. engine = create_async_engine("sqlite+aiosqlite:///filename")
  26. The URL passes through all arguments to the ``pysqlite`` driver, so all
  27. connection arguments are the same as they are for that of :ref:`pysqlite`.
  28. .. _aiosqlite_udfs:
  29. User-Defined Functions
  30. ----------------------
  31. aiosqlite extends pysqlite to support async, so we can create our own user-defined functions (UDFs)
  32. in Python and use them directly in SQLite queries as described here: :ref:`pysqlite_udfs`.
  33. .. _aiosqlite_serializable:
  34. Serializable isolation / Savepoints / Transactional DDL (asyncio version)
  35. -------------------------------------------------------------------------
  36. A newly revised version of this important section is now available
  37. at the top level of the SQLAlchemy SQLite documentation, in the section
  38. :ref:`sqlite_transactions`.
  39. .. _aiosqlite_pooling:
  40. Pooling Behavior
  41. ----------------
  42. The SQLAlchemy ``aiosqlite`` DBAPI establishes the connection pool differently
  43. based on the kind of SQLite database that's requested:
  44. * When a ``:memory:`` SQLite database is specified, the dialect by default
  45. will use :class:`.StaticPool`. This pool maintains a single
  46. connection, so that all access to the engine
  47. use the same ``:memory:`` database.
  48. * When a file-based database is specified, the dialect will use
  49. :class:`.AsyncAdaptedQueuePool` as the source of connections.
  50. .. versionchanged:: 2.0.38
  51. SQLite file database engines now use :class:`.AsyncAdaptedQueuePool` by default.
  52. Previously, :class:`.NullPool` were used. The :class:`.NullPool` class
  53. may be used by specifying it via the
  54. :paramref:`_sa.create_engine.poolclass` parameter.
  55. """ # noqa
  56. from __future__ import annotations
  57. import asyncio
  58. from collections import deque
  59. from functools import partial
  60. from threading import Thread
  61. from types import ModuleType
  62. from typing import Any
  63. from typing import cast
  64. from typing import Deque
  65. from typing import Iterator
  66. from typing import NoReturn
  67. from typing import Optional
  68. from typing import Sequence
  69. from typing import TYPE_CHECKING
  70. from typing import Union
  71. from .base import SQLiteExecutionContext
  72. from .pysqlite import SQLiteDialect_pysqlite
  73. from ... import pool
  74. from ... import util
  75. from ...connectors.asyncio import AsyncAdapt_dbapi_module
  76. from ...connectors.asyncio import AsyncAdapt_terminate
  77. from ...engine import AdaptedConnection
  78. from ...util.concurrency import await_fallback
  79. from ...util.concurrency import await_only
  80. if TYPE_CHECKING:
  81. from ...connectors.asyncio import AsyncIODBAPIConnection
  82. from ...connectors.asyncio import AsyncIODBAPICursor
  83. from ...engine.interfaces import _DBAPICursorDescription
  84. from ...engine.interfaces import _DBAPIMultiExecuteParams
  85. from ...engine.interfaces import _DBAPISingleExecuteParams
  86. from ...engine.interfaces import DBAPIConnection
  87. from ...engine.interfaces import DBAPICursor
  88. from ...engine.interfaces import DBAPIModule
  89. from ...engine.url import URL
  90. from ...pool.base import PoolProxiedConnection
  91. class AsyncAdapt_aiosqlite_cursor:
  92. # TODO: base on connectors/asyncio.py
  93. # see #10415
  94. __slots__ = (
  95. "_adapt_connection",
  96. "_connection",
  97. "description",
  98. "await_",
  99. "_rows",
  100. "arraysize",
  101. "rowcount",
  102. "lastrowid",
  103. )
  104. server_side = False
  105. def __init__(self, adapt_connection: AsyncAdapt_aiosqlite_connection):
  106. self._adapt_connection = adapt_connection
  107. self._connection = adapt_connection._connection
  108. self.await_ = adapt_connection.await_
  109. self.arraysize = 1
  110. self.rowcount = -1
  111. self.description: Optional[_DBAPICursorDescription] = None
  112. self._rows: Deque[Any] = deque()
  113. async def _async_soft_close(self) -> None:
  114. return
  115. def close(self) -> None:
  116. self._rows.clear()
  117. def execute(
  118. self,
  119. operation: Any,
  120. parameters: Optional[_DBAPISingleExecuteParams] = None,
  121. ) -> Any:
  122. try:
  123. _cursor: AsyncIODBAPICursor = self.await_(self._connection.cursor()) # type: ignore[arg-type] # noqa: E501
  124. if parameters is None:
  125. self.await_(_cursor.execute(operation))
  126. else:
  127. self.await_(_cursor.execute(operation, parameters))
  128. if _cursor.description:
  129. self.description = _cursor.description
  130. self.lastrowid = self.rowcount = -1
  131. if not self.server_side:
  132. self._rows = deque(self.await_(_cursor.fetchall()))
  133. else:
  134. self.description = None
  135. self.lastrowid = _cursor.lastrowid
  136. self.rowcount = _cursor.rowcount
  137. if not self.server_side:
  138. self.await_(_cursor.close())
  139. else:
  140. self._cursor = _cursor # type: ignore[misc]
  141. except Exception as error:
  142. self._adapt_connection._handle_exception(error)
  143. def executemany(
  144. self,
  145. operation: Any,
  146. seq_of_parameters: _DBAPIMultiExecuteParams,
  147. ) -> Any:
  148. try:
  149. _cursor: AsyncIODBAPICursor = self.await_(self._connection.cursor()) # type: ignore[arg-type] # noqa: E501
  150. self.await_(_cursor.executemany(operation, seq_of_parameters))
  151. self.description = None
  152. self.lastrowid = _cursor.lastrowid
  153. self.rowcount = _cursor.rowcount
  154. self.await_(_cursor.close())
  155. except Exception as error:
  156. self._adapt_connection._handle_exception(error)
  157. def setinputsizes(self, *inputsizes: Any) -> None:
  158. pass
  159. def __iter__(self) -> Iterator[Any]:
  160. while self._rows:
  161. yield self._rows.popleft()
  162. def fetchone(self) -> Optional[Any]:
  163. if self._rows:
  164. return self._rows.popleft()
  165. else:
  166. return None
  167. def fetchmany(self, size: Optional[int] = None) -> Sequence[Any]:
  168. if size is None:
  169. size = self.arraysize
  170. rr = self._rows
  171. return [rr.popleft() for _ in range(min(size, len(rr)))]
  172. def fetchall(self) -> Sequence[Any]:
  173. retval = list(self._rows)
  174. self._rows.clear()
  175. return retval
  176. class AsyncAdapt_aiosqlite_ss_cursor(AsyncAdapt_aiosqlite_cursor):
  177. # TODO: base on connectors/asyncio.py
  178. # see #10415
  179. __slots__ = "_cursor"
  180. server_side = True
  181. def __init__(self, *arg: Any, **kw: Any) -> None:
  182. super().__init__(*arg, **kw)
  183. self._cursor: Optional[AsyncIODBAPICursor] = None
  184. def close(self) -> None:
  185. if self._cursor is not None:
  186. self.await_(self._cursor.close())
  187. self._cursor = None
  188. def fetchone(self) -> Optional[Any]:
  189. assert self._cursor is not None
  190. return self.await_(self._cursor.fetchone())
  191. def fetchmany(self, size: Optional[int] = None) -> Sequence[Any]:
  192. assert self._cursor is not None
  193. if size is None:
  194. size = self.arraysize
  195. return self.await_(self._cursor.fetchmany(size=size))
  196. def fetchall(self) -> Sequence[Any]:
  197. assert self._cursor is not None
  198. return self.await_(self._cursor.fetchall())
  199. class AsyncAdapt_aiosqlite_connection(AsyncAdapt_terminate, AdaptedConnection):
  200. await_ = staticmethod(await_only)
  201. __slots__ = ("dbapi",)
  202. def __init__(self, dbapi: Any, connection: AsyncIODBAPIConnection) -> None:
  203. self.dbapi = dbapi
  204. self._connection = connection
  205. @property
  206. def isolation_level(self) -> Optional[str]:
  207. return cast(str, self._connection.isolation_level)
  208. @isolation_level.setter
  209. def isolation_level(self, value: Optional[str]) -> None:
  210. # aiosqlite's isolation_level setter works outside the Thread
  211. # that it's supposed to, necessitating setting check_same_thread=False.
  212. # for improved stability, we instead invent our own awaitable version
  213. # using aiosqlite's async queue directly.
  214. def set_iso(
  215. connection: AsyncAdapt_aiosqlite_connection, value: Optional[str]
  216. ) -> None:
  217. connection.isolation_level = value
  218. function = partial(set_iso, self._connection._conn, value)
  219. future = asyncio.get_event_loop().create_future()
  220. self._connection._tx.put_nowait((future, function))
  221. try:
  222. self.await_(future)
  223. except Exception as error:
  224. self._handle_exception(error)
  225. def create_function(self, *args: Any, **kw: Any) -> None:
  226. try:
  227. self.await_(self._connection.create_function(*args, **kw))
  228. except Exception as error:
  229. self._handle_exception(error)
  230. def cursor(self, server_side: bool = False) -> AsyncAdapt_aiosqlite_cursor:
  231. if server_side:
  232. return AsyncAdapt_aiosqlite_ss_cursor(self)
  233. else:
  234. return AsyncAdapt_aiosqlite_cursor(self)
  235. def execute(self, *args: Any, **kw: Any) -> Any:
  236. return self.await_(self._connection.execute(*args, **kw))
  237. def rollback(self) -> None:
  238. try:
  239. self.await_(self._connection.rollback())
  240. except Exception as error:
  241. self._handle_exception(error)
  242. def commit(self) -> None:
  243. try:
  244. self.await_(self._connection.commit())
  245. except Exception as error:
  246. self._handle_exception(error)
  247. def close(self) -> None:
  248. try:
  249. self.await_(self._connection.close())
  250. except ValueError:
  251. # this is undocumented for aiosqlite, that ValueError
  252. # was raised if .close() was called more than once, which is
  253. # both not customary for DBAPI and is also not a DBAPI.Error
  254. # exception. This is now fixed in aiosqlite via my PR
  255. # https://github.com/omnilib/aiosqlite/pull/238, so we can be
  256. # assured this will not become some other kind of exception,
  257. # since it doesn't raise anymore.
  258. pass
  259. except Exception as error:
  260. self._handle_exception(error)
  261. def _handle_exception(self, error: Exception) -> NoReturn:
  262. if (
  263. isinstance(error, ValueError)
  264. and error.args[0] == "no active connection"
  265. ):
  266. raise self.dbapi.sqlite.OperationalError(
  267. "no active connection"
  268. ) from error
  269. else:
  270. raise error
  271. async def _terminate_graceful_close(self) -> None:
  272. """Try to close connection gracefully"""
  273. await self._connection.close()
  274. def _terminate_force_close(self) -> None:
  275. """Terminate the connection"""
  276. # this was added in aiosqlite 0.22.1. if stop() is not present,
  277. # the dialect should indicate has_terminate=False
  278. try:
  279. meth = self._connection.stop
  280. except AttributeError as ae:
  281. raise NotImplementedError(
  282. "terminate_force_close() not implemented by this DBAPI shim"
  283. ) from ae
  284. else:
  285. meth()
  286. class AsyncAdaptFallback_aiosqlite_connection(AsyncAdapt_aiosqlite_connection):
  287. __slots__ = ()
  288. await_ = staticmethod(await_fallback)
  289. class AsyncAdapt_aiosqlite_dbapi(AsyncAdapt_dbapi_module):
  290. def __init__(self, aiosqlite: ModuleType, sqlite: ModuleType):
  291. self.aiosqlite = aiosqlite
  292. self.sqlite = sqlite
  293. self.paramstyle = "qmark"
  294. self.has_stop = hasattr(aiosqlite.Connection, "stop")
  295. self._init_dbapi_attributes()
  296. def _init_dbapi_attributes(self) -> None:
  297. for name in (
  298. "DatabaseError",
  299. "Error",
  300. "IntegrityError",
  301. "NotSupportedError",
  302. "OperationalError",
  303. "ProgrammingError",
  304. "sqlite_version",
  305. "sqlite_version_info",
  306. ):
  307. setattr(self, name, getattr(self.aiosqlite, name))
  308. for name in ("PARSE_COLNAMES", "PARSE_DECLTYPES"):
  309. setattr(self, name, getattr(self.sqlite, name))
  310. for name in ("Binary",):
  311. setattr(self, name, getattr(self.sqlite, name))
  312. def connect(self, *arg: Any, **kw: Any) -> AsyncAdapt_aiosqlite_connection:
  313. async_fallback = kw.pop("async_fallback", False)
  314. creator_fn = kw.pop("async_creator_fn", None)
  315. if creator_fn:
  316. connection = creator_fn(*arg, **kw)
  317. else:
  318. connection = self.aiosqlite.connect(*arg, **kw)
  319. # aiosqlite uses a Thread. you'll thank us later
  320. if isinstance(connection, Thread):
  321. # Connection itself was a thread in version prior to 0.22
  322. connection.daemon = True
  323. else:
  324. # in 0.22+ instead it contains a thread.
  325. connection._thread.daemon = True
  326. if util.asbool(async_fallback):
  327. return AsyncAdaptFallback_aiosqlite_connection(
  328. self,
  329. await_fallback(connection),
  330. )
  331. else:
  332. return AsyncAdapt_aiosqlite_connection(
  333. self,
  334. await_only(connection),
  335. )
  336. class SQLiteExecutionContext_aiosqlite(SQLiteExecutionContext):
  337. def create_server_side_cursor(self) -> DBAPICursor:
  338. return self._dbapi_connection.cursor(server_side=True)
  339. class SQLiteDialect_aiosqlite(SQLiteDialect_pysqlite):
  340. driver = "aiosqlite"
  341. supports_statement_cache = True
  342. is_async = True
  343. has_terminate = True
  344. supports_server_side_cursors = True
  345. execution_ctx_cls = SQLiteExecutionContext_aiosqlite
  346. def __init__(self, **kwargs: Any):
  347. super().__init__(**kwargs)
  348. if self.dbapi and not self.dbapi.has_stop:
  349. self.has_terminate = False
  350. @classmethod
  351. def import_dbapi(cls) -> AsyncAdapt_aiosqlite_dbapi:
  352. return AsyncAdapt_aiosqlite_dbapi(
  353. __import__("aiosqlite"), __import__("sqlite3")
  354. )
  355. @classmethod
  356. def get_pool_class(cls, url: URL) -> type[pool.Pool]:
  357. if cls._is_url_file_db(url):
  358. return pool.AsyncAdaptedQueuePool
  359. else:
  360. return pool.StaticPool
  361. def is_disconnect(
  362. self,
  363. e: DBAPIModule.Error,
  364. connection: Optional[Union[PoolProxiedConnection, DBAPIConnection]],
  365. cursor: Optional[DBAPICursor],
  366. ) -> bool:
  367. self.dbapi = cast("DBAPIModule", self.dbapi)
  368. if isinstance(
  369. e, self.dbapi.OperationalError
  370. ) and "no active connection" in str(e):
  371. return True
  372. return super().is_disconnect(e, connection, cursor)
  373. def get_driver_connection(
  374. self, connection: DBAPIConnection
  375. ) -> AsyncIODBAPIConnection:
  376. return connection._connection # type: ignore[no-any-return]
  377. def do_terminate(self, dbapi_connection: DBAPIConnection) -> None:
  378. dbapi_connection.terminate()
  379. dialect = SQLiteDialect_aiosqlite