aiomysql.py 9.8 KB

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