aioodbc.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # connectors/aioodbc.py
  2. # Copyright (C) 2005-2025 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. # mypy: ignore-errors
  8. from __future__ import annotations
  9. from typing import TYPE_CHECKING
  10. from .asyncio import AsyncAdapt_dbapi_connection
  11. from .asyncio import AsyncAdapt_dbapi_cursor
  12. from .asyncio import AsyncAdapt_dbapi_ss_cursor
  13. from .asyncio import AsyncAdaptFallback_dbapi_connection
  14. from .pyodbc import PyODBCConnector
  15. from .. import pool
  16. from .. import util
  17. from ..util.concurrency import await_fallback
  18. from ..util.concurrency import await_only
  19. if TYPE_CHECKING:
  20. from ..engine.interfaces import ConnectArgsType
  21. from ..engine.url import URL
  22. class AsyncAdapt_aioodbc_cursor(AsyncAdapt_dbapi_cursor):
  23. __slots__ = ()
  24. def setinputsizes(self, *inputsizes):
  25. # see https://github.com/aio-libs/aioodbc/issues/451
  26. return self._cursor._impl.setinputsizes(*inputsizes)
  27. # how it's supposed to work
  28. # return self.await_(self._cursor.setinputsizes(*inputsizes))
  29. class AsyncAdapt_aioodbc_ss_cursor(
  30. AsyncAdapt_aioodbc_cursor, AsyncAdapt_dbapi_ss_cursor
  31. ):
  32. __slots__ = ()
  33. class AsyncAdapt_aioodbc_connection(AsyncAdapt_dbapi_connection):
  34. _cursor_cls = AsyncAdapt_aioodbc_cursor
  35. _ss_cursor_cls = AsyncAdapt_aioodbc_ss_cursor
  36. __slots__ = ()
  37. @property
  38. def autocommit(self):
  39. return self._connection.autocommit
  40. @autocommit.setter
  41. def autocommit(self, value):
  42. # https://github.com/aio-libs/aioodbc/issues/448
  43. # self._connection.autocommit = value
  44. self._connection._conn.autocommit = value
  45. def ping(self, reconnect):
  46. return self.await_(self._connection.ping(reconnect))
  47. def add_output_converter(self, *arg, **kw):
  48. self._connection.add_output_converter(*arg, **kw)
  49. def character_set_name(self):
  50. return self._connection.character_set_name()
  51. def cursor(self, server_side=False):
  52. # aioodbc sets connection=None when closed and just fails with
  53. # AttributeError here. Here we use the same ProgrammingError +
  54. # message that pyodbc uses, so it triggers is_disconnect() as well.
  55. if self._connection.closed:
  56. raise self.dbapi.ProgrammingError(
  57. "Attempt to use a closed connection."
  58. )
  59. return super().cursor(server_side=server_side)
  60. def rollback(self):
  61. # aioodbc sets connection=None when closed and just fails with
  62. # AttributeError here. should be a no-op
  63. if not self._connection.closed:
  64. super().rollback()
  65. def commit(self):
  66. # aioodbc sets connection=None when closed and just fails with
  67. # AttributeError here. should be a no-op
  68. if not self._connection.closed:
  69. super().commit()
  70. def close(self):
  71. # aioodbc sets connection=None when closed and just fails with
  72. # AttributeError here. should be a no-op
  73. if not self._connection.closed:
  74. super().close()
  75. class AsyncAdaptFallback_aioodbc_connection(
  76. AsyncAdaptFallback_dbapi_connection, AsyncAdapt_aioodbc_connection
  77. ):
  78. __slots__ = ()
  79. class AsyncAdapt_aioodbc_dbapi:
  80. def __init__(self, aioodbc, pyodbc):
  81. self.aioodbc = aioodbc
  82. self.pyodbc = pyodbc
  83. self.paramstyle = pyodbc.paramstyle
  84. self._init_dbapi_attributes()
  85. self.Cursor = AsyncAdapt_dbapi_cursor
  86. self.version = pyodbc.version
  87. def _init_dbapi_attributes(self):
  88. for name in (
  89. "Warning",
  90. "Error",
  91. "InterfaceError",
  92. "DataError",
  93. "DatabaseError",
  94. "OperationalError",
  95. "InterfaceError",
  96. "IntegrityError",
  97. "ProgrammingError",
  98. "InternalError",
  99. "NotSupportedError",
  100. "NUMBER",
  101. "STRING",
  102. "DATETIME",
  103. "BINARY",
  104. "Binary",
  105. "BinaryNull",
  106. "SQL_VARCHAR",
  107. "SQL_WVARCHAR",
  108. ):
  109. setattr(self, name, getattr(self.pyodbc, name))
  110. def connect(self, *arg, **kw):
  111. async_fallback = kw.pop("async_fallback", False)
  112. creator_fn = kw.pop("async_creator_fn", self.aioodbc.connect)
  113. if util.asbool(async_fallback):
  114. return AsyncAdaptFallback_aioodbc_connection(
  115. self,
  116. await_fallback(creator_fn(*arg, **kw)),
  117. )
  118. else:
  119. return AsyncAdapt_aioodbc_connection(
  120. self,
  121. await_only(creator_fn(*arg, **kw)),
  122. )
  123. class aiodbcConnector(PyODBCConnector):
  124. is_async = True
  125. supports_statement_cache = True
  126. supports_server_side_cursors = True
  127. @classmethod
  128. def import_dbapi(cls):
  129. return AsyncAdapt_aioodbc_dbapi(
  130. __import__("aioodbc"), __import__("pyodbc")
  131. )
  132. def create_connect_args(self, url: URL) -> ConnectArgsType:
  133. arg, kw = super().create_connect_args(url)
  134. if arg and arg[0]:
  135. kw["dsn"] = arg[0]
  136. return (), kw
  137. @classmethod
  138. def get_pool_class(cls, url):
  139. async_fallback = url.query.get("async_fallback", False)
  140. if util.asbool(async_fallback):
  141. return pool.FallbackAsyncAdaptedQueuePool
  142. else:
  143. return pool.AsyncAdaptedQueuePool
  144. def get_driver_connection(self, connection):
  145. return connection._connection