diff options
| author | Federico Caselli <cfederico87@gmail.com> | 2021-08-31 23:03:18 +0200 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-09-17 18:08:42 -0400 |
| commit | 26140c08111da9833dd2eff0b5091494f253db46 (patch) | |
| tree | 61a64b7361ab0890521771a5d185db787482eaaf /lib/sqlalchemy/engine | |
| parent | 204fe7ea206a1b0ab4ae248006f99afd15fa7f72 (diff) | |
| download | sqlalchemy-26140c08111da9833dd2eff0b5091494f253db46.tar.gz | |
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
Diffstat (limited to 'lib/sqlalchemy/engine')
| -rw-r--r-- | lib/sqlalchemy/engine/__init__.py | 1 | ||||
| -rw-r--r-- | lib/sqlalchemy/engine/base.py | 5 | ||||
| -rw-r--r-- | lib/sqlalchemy/engine/default.py | 5 | ||||
| -rw-r--r-- | lib/sqlalchemy/engine/interfaces.py | 37 |
4 files changed, 46 insertions, 2 deletions
diff --git a/lib/sqlalchemy/engine/__init__.py b/lib/sqlalchemy/engine/__init__.py index 3761f5005..6306e201d 100644 --- a/lib/sqlalchemy/engine/__init__.py +++ b/lib/sqlalchemy/engine/__init__.py @@ -33,6 +33,7 @@ from .cursor import CursorResult from .cursor import FullyBufferedResultProxy from .cursor import LegacyCursorResult from .cursor import ResultProxy +from .interfaces import AdaptedConnection from .interfaces import Compiled from .interfaces import Connectable from .interfaces import CreateEnginePlugin diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py index 25ced0343..2444b5c7f 100644 --- a/lib/sqlalchemy/engine/base.py +++ b/lib/sqlalchemy/engine/base.py @@ -440,6 +440,11 @@ class Connection(Connectable): def connection(self): """The underlying DB-API connection managed by this Connection. + This is a SQLAlchemy connection-pool proxied connection + which then has the attribute + :attr:`_pool._ConnectionFairy.dbapi_connection` that refers to the + actual driver connection. + .. seealso:: diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py index 8bd8a121b..eff28e340 100644 --- a/lib/sqlalchemy/engine/default.py +++ b/lib/sqlalchemy/engine/default.py @@ -646,7 +646,7 @@ class DefaultDialect(interfaces.Dialect): % (", ".join(name for name, obj in trans_objs)) ) - dbapi_connection = connection.connection.connection + dbapi_connection = connection.connection.dbapi_connection for name, characteristic, value in characteristic_values: characteristic.set_characteristic(self, dbapi_connection, value) connection.connection._connection_record.finalize_callback.append( @@ -779,6 +779,9 @@ class DefaultDialect(interfaces.Dialect): name = unicode(name) # noqa return name + def get_driver_connection(self, connection): + return connection + class _RendersLiteral(object): def literal_processor(self, dialect): diff --git a/lib/sqlalchemy/engine/interfaces.py b/lib/sqlalchemy/engine/interfaces.py index 8379c731a..d1484718e 100644 --- a/lib/sqlalchemy/engine/interfaces.py +++ b/lib/sqlalchemy/engine/interfaces.py @@ -1056,7 +1056,21 @@ class Dialect(object): .. versionadded:: 1.0.3 """ - pass + + def get_driver_connection(self, connection): + """Returns the connection object as returned by the external driver + package. + + For normal dialects that use a DBAPI compliant driver this call + will just return the ``connection`` passed as argument. + For dialects that instead adapt a non DBAPI compliant driver, like + when adapting an asyncio driver, this call will return the + connection-like object as returned by the driver. + + .. versionadded:: 1.4.24 + + """ + raise NotImplementedError() class CreateEnginePlugin(object): @@ -1719,3 +1733,24 @@ class ExceptionContext(object): .. versionadded:: 1.0.3 """ + + +class AdaptedConnection(object): + """Interface of an adapted connection object to support the DBAPI protocol. + + Used by asyncio dialects to provide a sync-style pep-249 facade on top + of the asyncio connection/cursor API provided by the driver. + + .. versionadded:: 1.4.24 + + """ + + __slots__ = ("_connection",) + + @property + def driver_connection(self): + """The connection object as returned by the driver after a connect.""" + return self._connection + + def __repr__(self): + return "<AdaptedConnection %s>" % self._connection |
