aiomysql.py 9.5 KB

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