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
8r"""
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
15
16Support for the SQL Server database in asyncio style, using the aioodbc
17driver which itself is a thread-wrapper around pyodbc.
18
19.. versionadded:: 2.0.23 Added the mssql+aioodbc dialect which builds
20 on top of the pyodbc and general aio* dialect architecture.
21
22Using a special asyncio mediation layer, the aioodbc dialect is usable
23as the backend for the :ref:`SQLAlchemy asyncio <asyncio_toplevel>`
24extension package.
25
26Most behaviors and caveats for this driver are the same as that of the
27pyodbc dialect used on SQL Server; see :ref:`mssql_pyodbc` for general
28background.
29
30This dialect should normally be used only with the
31:func:`_asyncio.create_async_engine` engine creation function; connection
32styles are otherwise equivalent to those documented in the pyodbc section::
33
34 from sqlalchemy.ext.asyncio import create_async_engine
35
36 engine = create_async_engine(
37 "mssql+aioodbc://scott:tiger@mssql2017:1433/test?"
38 "driver=ODBC+Driver+18+for+SQL+Server&TrustServerCertificate=yes"
39 )
40
41"""
42
43from __future__ import annotations
44
45from .pyodbc import MSDialect_pyodbc
46from .pyodbc import MSExecutionContext_pyodbc
47from ...connectors.aioodbc import aiodbcConnector
48
49
50class MSExecutionContext_aioodbc(MSExecutionContext_pyodbc):
51 def create_server_side_cursor(self):
52 return self._dbapi_connection.cursor(server_side=True)
53
54
55class MSDialectAsync_aioodbc(aiodbcConnector, MSDialect_pyodbc):
56 driver = "aioodbc"
57
58 supports_statement_cache = True
59
60 execution_ctx_cls = MSExecutionContext_aioodbc
61
62
63dialect = MSDialectAsync_aioodbc