1# engine/characteristics.py
2# Copyright (C) 2005-2024 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
7import abc
8
9from ..util import ABC
10
11
12class ConnectionCharacteristic(ABC):
13 """An abstract base for an object that can set, get and reset a
14 per-connection characteristic, typically one that gets reset when the
15 connection is returned to the connection pool.
16
17 transaction isolation is the canonical example, and the
18 ``IsolationLevelCharacteristic`` implementation provides this for the
19 ``DefaultDialect``.
20
21 The ``ConnectionCharacteristic`` class should call upon the ``Dialect`` for
22 the implementation of each method. The object exists strictly to serve as
23 a dialect visitor that can be placed into the
24 ``DefaultDialect.connection_characteristics`` dictionary where it will take
25 effect for calls to :meth:`_engine.Connection.execution_options` and
26 related APIs.
27
28 .. versionadded:: 1.4
29
30 """
31
32 __slots__ = ()
33
34 transactional = False
35
36 @abc.abstractmethod
37 def reset_characteristic(self, dialect, dbapi_conn):
38 """Reset the characteristic on the connection to its default value."""
39
40 @abc.abstractmethod
41 def set_characteristic(self, dialect, dbapi_conn, value):
42 """set characteristic on the connection to a given value."""
43
44 @abc.abstractmethod
45 def get_characteristic(self, dialect, dbapi_conn):
46 """Given a DBAPI connection, get the current value of the
47 characteristic.
48
49 """
50
51
52class IsolationLevelCharacteristic(ConnectionCharacteristic):
53 transactional = True
54
55 def reset_characteristic(self, dialect, dbapi_conn):
56 dialect.reset_isolation_level(dbapi_conn)
57
58 def set_characteristic(self, dialect, dbapi_conn, value):
59 dialect.set_isolation_level(dbapi_conn, value)
60
61 def get_characteristic(self, dialect, dbapi_conn):
62 return dialect.get_isolation_level(dbapi_conn)