From 26140c08111da9833dd2eff0b5091494f253db46 Mon Sep 17 00:00:00 2001 From: Federico Caselli Date: Tue, 31 Aug 2021 23:03:18 +0200 Subject: Surface driver connection object when using a proxied dialect Improve the interface used by adapted drivers, like the asyncio ones, to access the actual connection object returned by the driver. The :class:`_engine._ConnectionRecord` and :class:`_engine._ConnectionFairy` now have two new attributes: * ``dbapi_connection`` always represents a DBAPI compatible object. For pep-249 drivers, this is the DBAPI connection as it always has been, previously accessed under the ``.connection`` attribute. For asyncio drivers that SQLAlchemy adapts into a pep-249 interface, the returned object will normally be a SQLAlchemy adaption object called :class:`_engine.AdaptedConnection`. * ``driver_connection`` always represents the actual connection object maintained by the third party pep-249 DBAPI or async driver in use. For standard pep-249 DBAPIs, this will always be the same object as that of the ``dbapi_connection``. For an asyncio driver, it will be the underlying asyncio-only connection object. The ``.connection`` attribute remains available and is now a legacy alias of ``.dbapi_connection``. Fixes: #6832 Change-Id: Ib72f97deefca96dce4e61e7c38ba430068d6a82e --- lib/sqlalchemy/ext/asyncio/engine.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'lib/sqlalchemy/ext/asyncio/engine.py') diff --git a/lib/sqlalchemy/ext/asyncio/engine.py b/lib/sqlalchemy/ext/asyncio/engine.py index ab29438ed..a9e43a65f 100644 --- a/lib/sqlalchemy/ext/asyncio/engine.py +++ b/lib/sqlalchemy/ext/asyncio/engine.py @@ -113,7 +113,6 @@ class AsyncConnection(ProxyComparable, StartableContext, AsyncConnectable): def connection(self): """Not implemented for async; call :meth:`_asyncio.AsyncConnection.get_raw_connection`. - """ raise exc.InvalidRequestError( "AsyncConnection.connection accessor is not implemented as the " @@ -125,9 +124,14 @@ class AsyncConnection(ProxyComparable, StartableContext, AsyncConnectable): """Return the pooled DBAPI-level connection in use by this :class:`_asyncio.AsyncConnection`. - This is typically the SQLAlchemy connection-pool proxied connection - which then has an attribute .connection that refers to the actual - DBAPI-level connection. + This is a SQLAlchemy connection-pool proxied connection + which then has the attribute + :attr:`_pool._ConnectionFairy.driver_connection` that refers to the + actual driver connection. Its + :attr:`_pool._ConnectionFairy.dbapi_connection` refers instead + to an :class:`_engine.AdaptedConnection` instance that + adapts the driver connection to the DBAPI protocol. + """ conn = self._sync_connection() -- cgit v1.2.1