asyncmy.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. # dialects/mysql/asyncmy.py
  2. # Copyright (C) 2005-2025 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. # mypy: ignore-errors
  8. r"""
  9. .. dialect:: mysql+asyncmy
  10. :name: asyncmy
  11. :dbapi: asyncmy
  12. :connectstring: mysql+asyncmy://user:password@host:port/dbname[?key=value&key=value...]
  13. :url: https://github.com/long2ice/asyncmy
  14. Using a special asyncio mediation layer, the asyncmy 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(
  21. "mysql+asyncmy://user:pass@hostname/dbname?charset=utf8mb4"
  22. )
  23. """ # noqa
  24. from collections import deque
  25. from contextlib import asynccontextmanager
  26. from .pymysql import MySQLDialect_pymysql
  27. from ... import pool
  28. from ... import util
  29. from ...engine import AdaptedConnection
  30. from ...util.concurrency import asyncio
  31. from ...util.concurrency import await_fallback
  32. from ...util.concurrency import await_only
  33. class AsyncAdapt_asyncmy_cursor:
  34. # TODO: base on connectors/asyncio.py
  35. # see #10415
  36. server_side = False
  37. __slots__ = (
  38. "_adapt_connection",
  39. "_connection",
  40. "await_",
  41. "_cursor",
  42. "_rows",
  43. )
  44. def __init__(self, adapt_connection):
  45. self._adapt_connection = adapt_connection
  46. self._connection = adapt_connection._connection
  47. self.await_ = adapt_connection.await_
  48. cursor = self._connection.cursor()
  49. self._cursor = self.await_(cursor.__aenter__())
  50. self._rows = deque()
  51. @property
  52. def description(self):
  53. return self._cursor.description
  54. @property
  55. def rowcount(self):
  56. return self._cursor.rowcount
  57. @property
  58. def arraysize(self):
  59. return self._cursor.arraysize
  60. @arraysize.setter
  61. def arraysize(self, value):
  62. self._cursor.arraysize = value
  63. @property
  64. def lastrowid(self):
  65. return self._cursor.lastrowid
  66. def close(self):
  67. # note we aren't actually closing the cursor here,
  68. # we are just letting GC do it. to allow this to be async
  69. # we would need the Result to change how it does "Safe close cursor".
  70. # MySQL "cursors" don't actually have state to be "closed" besides
  71. # exhausting rows, which we already have done for sync cursor.
  72. # another option would be to emulate aiosqlite dialect and assign
  73. # cursor only if we are doing server side cursor operation.
  74. self._rows.clear()
  75. def execute(self, operation, parameters=None):
  76. return self.await_(self._execute_async(operation, parameters))
  77. def executemany(self, operation, seq_of_parameters):
  78. return self.await_(
  79. self._executemany_async(operation, seq_of_parameters)
  80. )
  81. async def _execute_async(self, operation, parameters):
  82. async with self._adapt_connection._mutex_and_adapt_errors():
  83. if parameters is None:
  84. result = await self._cursor.execute(operation)
  85. else:
  86. result = await self._cursor.execute(operation, parameters)
  87. if not self.server_side:
  88. # asyncmy has a "fake" async result, so we have to pull it out
  89. # of that here since our default result is not async.
  90. # we could just as easily grab "_rows" here and be done with it
  91. # but this is safer.
  92. self._rows = deque(await self._cursor.fetchall())
  93. return result
  94. async def _executemany_async(self, operation, seq_of_parameters):
  95. async with self._adapt_connection._mutex_and_adapt_errors():
  96. return await self._cursor.executemany(operation, seq_of_parameters)
  97. def setinputsizes(self, *inputsizes):
  98. pass
  99. def __iter__(self):
  100. while self._rows:
  101. yield self._rows.popleft()
  102. def fetchone(self):
  103. if self._rows:
  104. return self._rows.popleft()
  105. else:
  106. return None
  107. def fetchmany(self, size=None):
  108. if size is None:
  109. size = self.arraysize
  110. rr = self._rows
  111. return [rr.popleft() for _ in range(min(size, len(rr)))]
  112. def fetchall(self):
  113. retval = list(self._rows)
  114. self._rows.clear()
  115. return retval
  116. class AsyncAdapt_asyncmy_ss_cursor(AsyncAdapt_asyncmy_cursor):
  117. # TODO: base on connectors/asyncio.py
  118. # see #10415
  119. __slots__ = ()
  120. server_side = True
  121. def __init__(self, adapt_connection):
  122. self._adapt_connection = adapt_connection
  123. self._connection = adapt_connection._connection
  124. self.await_ = adapt_connection.await_
  125. cursor = self._connection.cursor(
  126. adapt_connection.dbapi.asyncmy.cursors.SSCursor
  127. )
  128. self._cursor = self.await_(cursor.__aenter__())
  129. def close(self):
  130. if self._cursor is not None:
  131. self.await_(self._cursor.close())
  132. self._cursor = None
  133. def fetchone(self):
  134. return self.await_(self._cursor.fetchone())
  135. def fetchmany(self, size=None):
  136. return self.await_(self._cursor.fetchmany(size=size))
  137. def fetchall(self):
  138. return self.await_(self._cursor.fetchall())
  139. class AsyncAdapt_asyncmy_connection(AdaptedConnection):
  140. # TODO: base on connectors/asyncio.py
  141. # see #10415
  142. await_ = staticmethod(await_only)
  143. __slots__ = ("dbapi", "_execute_mutex")
  144. def __init__(self, dbapi, connection):
  145. self.dbapi = dbapi
  146. self._connection = connection
  147. self._execute_mutex = asyncio.Lock()
  148. @asynccontextmanager
  149. async def _mutex_and_adapt_errors(self):
  150. async with self._execute_mutex:
  151. try:
  152. yield
  153. except AttributeError:
  154. raise self.dbapi.InternalError(
  155. "network operation failed due to asyncmy attribute error"
  156. )
  157. def ping(self, reconnect):
  158. assert not reconnect
  159. return self.await_(self._do_ping())
  160. async def _do_ping(self):
  161. async with self._mutex_and_adapt_errors():
  162. return await self._connection.ping(False)
  163. def character_set_name(self):
  164. return self._connection.character_set_name()
  165. def autocommit(self, value):
  166. self.await_(self._connection.autocommit(value))
  167. def cursor(self, server_side=False):
  168. if server_side:
  169. return AsyncAdapt_asyncmy_ss_cursor(self)
  170. else:
  171. return AsyncAdapt_asyncmy_cursor(self)
  172. def rollback(self):
  173. self.await_(self._connection.rollback())
  174. def commit(self):
  175. self.await_(self._connection.commit())
  176. def terminate(self):
  177. # it's not awaitable.
  178. self._connection.close()
  179. def close(self) -> None:
  180. self.await_(self._connection.ensure_closed())
  181. class AsyncAdaptFallback_asyncmy_connection(AsyncAdapt_asyncmy_connection):
  182. __slots__ = ()
  183. await_ = staticmethod(await_fallback)
  184. def _Binary(x):
  185. """Return x as a binary type."""
  186. return bytes(x)
  187. class AsyncAdapt_asyncmy_dbapi:
  188. def __init__(self, asyncmy):
  189. self.asyncmy = asyncmy
  190. self.paramstyle = "format"
  191. self._init_dbapi_attributes()
  192. def _init_dbapi_attributes(self):
  193. for name in (
  194. "Warning",
  195. "Error",
  196. "InterfaceError",
  197. "DataError",
  198. "DatabaseError",
  199. "OperationalError",
  200. "InterfaceError",
  201. "IntegrityError",
  202. "ProgrammingError",
  203. "InternalError",
  204. "NotSupportedError",
  205. ):
  206. setattr(self, name, getattr(self.asyncmy.errors, name))
  207. STRING = util.symbol("STRING")
  208. NUMBER = util.symbol("NUMBER")
  209. BINARY = util.symbol("BINARY")
  210. DATETIME = util.symbol("DATETIME")
  211. TIMESTAMP = util.symbol("TIMESTAMP")
  212. Binary = staticmethod(_Binary)
  213. def connect(self, *arg, **kw):
  214. async_fallback = kw.pop("async_fallback", False)
  215. creator_fn = kw.pop("async_creator_fn", self.asyncmy.connect)
  216. if util.asbool(async_fallback):
  217. return AsyncAdaptFallback_asyncmy_connection(
  218. self,
  219. await_fallback(creator_fn(*arg, **kw)),
  220. )
  221. else:
  222. return AsyncAdapt_asyncmy_connection(
  223. self,
  224. await_only(creator_fn(*arg, **kw)),
  225. )
  226. class MySQLDialect_asyncmy(MySQLDialect_pymysql):
  227. driver = "asyncmy"
  228. supports_statement_cache = True
  229. supports_server_side_cursors = True
  230. _sscursor = AsyncAdapt_asyncmy_ss_cursor
  231. is_async = True
  232. has_terminate = True
  233. @classmethod
  234. def import_dbapi(cls):
  235. return AsyncAdapt_asyncmy_dbapi(__import__("asyncmy"))
  236. @classmethod
  237. def get_pool_class(cls, url):
  238. async_fallback = url.query.get("async_fallback", False)
  239. if util.asbool(async_fallback):
  240. return pool.FallbackAsyncAdaptedQueuePool
  241. else:
  242. return pool.AsyncAdaptedQueuePool
  243. def do_terminate(self, dbapi_connection) -> None:
  244. dbapi_connection.terminate()
  245. def create_connect_args(self, url):
  246. return super().create_connect_args(
  247. url, _translate_args=dict(username="user", database="db")
  248. )
  249. def is_disconnect(self, e, connection, cursor):
  250. if super().is_disconnect(e, connection, cursor):
  251. return True
  252. else:
  253. str_e = str(e).lower()
  254. return (
  255. "not connected" in str_e or "network operation failed" in str_e
  256. )
  257. def _found_rows_client_flag(self):
  258. from asyncmy.constants import CLIENT
  259. return CLIENT.FOUND_ROWS
  260. def get_driver_connection(self, connection):
  261. return connection._connection
  262. dialect = MySQLDialect_asyncmy