aioodbc.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # dialects/mssql/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. r"""
  9. .. dialect:: mssql+aioodbc
  10. :name: aioodbc
  11. :dbapi: aioodbc
  12. :connectstring: mssql+aioodbc://<username>:<password>@<dsnname>
  13. :url: https://pypi.org/project/aioodbc/
  14. Support for the SQL Server database in asyncio style, using the aioodbc
  15. driver which itself is a thread-wrapper around pyodbc.
  16. .. versionadded:: 2.0.23 Added the mssql+aioodbc dialect which builds
  17. on top of the pyodbc and general aio* dialect architecture.
  18. Using a special asyncio mediation layer, the aioodbc dialect is usable
  19. as the backend for the :ref:`SQLAlchemy asyncio <asyncio_toplevel>`
  20. extension package.
  21. Most behaviors and caveats for this driver are the same as that of the
  22. pyodbc dialect used on SQL Server; see :ref:`mssql_pyodbc` for general
  23. background.
  24. This dialect should normally be used only with the
  25. :func:`_asyncio.create_async_engine` engine creation function; connection
  26. styles are otherwise equivalent to those documented in the pyodbc section::
  27. from sqlalchemy.ext.asyncio import create_async_engine
  28. engine = create_async_engine(
  29. "mssql+aioodbc://scott:tiger@mssql2017:1433/test?"
  30. "driver=ODBC+Driver+18+for+SQL+Server&TrustServerCertificate=yes"
  31. )
  32. """
  33. from __future__ import annotations
  34. from .pyodbc import MSDialect_pyodbc
  35. from .pyodbc import MSExecutionContext_pyodbc
  36. from ...connectors.aioodbc import aiodbcConnector
  37. class MSExecutionContext_aioodbc(MSExecutionContext_pyodbc):
  38. def create_server_side_cursor(self):
  39. return self._dbapi_connection.cursor(server_side=True)
  40. class MSDialectAsync_aioodbc(aiodbcConnector, MSDialect_pyodbc):
  41. driver = "aioodbc"
  42. supports_statement_cache = True
  43. execution_ctx_cls = MSExecutionContext_aioodbc
  44. dialect = MSDialectAsync_aioodbc