summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/engine/characteristics.py
blob: 10455451fdc667bbb3f777dfc18d88f607b9e167 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import abc


class ConnectionCharacteristic(abc.ABC):
    """An abstract base for an object that can set, get and reset a
    per-connection characteristic, typically one that gets reset when the
    connection is returned to the connection pool.

    transaction isolation is the canonical example, and the
    ``IsolationLevelCharacteristic`` implementation provides this for the
    ``DefaultDialect``.

    The ``ConnectionCharacteristic`` class should call upon the ``Dialect`` for
    the implementation of each method.   The object exists strictly to serve as
    a dialect visitor that can be placed into the
    ``DefaultDialect.connection_characteristics`` dictionary where it will take
    effect for calls to :meth:`_engine.Connection.execution_options` and
    related APIs.

    .. versionadded:: 1.4

    """

    __slots__ = ()

    transactional = False

    @abc.abstractmethod
    def reset_characteristic(self, dialect, dbapi_conn):
        """Reset the characteristic on the connection to its default value."""

    @abc.abstractmethod
    def set_characteristic(self, dialect, dbapi_conn, value):
        """set characteristic on the connection to a given value."""

    @abc.abstractmethod
    def get_characteristic(self, dialect, dbapi_conn):
        """Given a DBAPI connection, get the current value of the
        characteristic.

        """


class IsolationLevelCharacteristic(ConnectionCharacteristic):
    transactional = True

    def reset_characteristic(self, dialect, dbapi_conn):
        dialect.reset_isolation_level(dbapi_conn)

    def set_characteristic(self, dialect, dbapi_conn, value):
        dialect._assert_and_set_isolation_level(dbapi_conn, value)

    def get_characteristic(self, dialect, dbapi_conn):
        return dialect.get_isolation_level(dbapi_conn)