characteristics.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import abc
  2. from ..util import ABC
  3. class ConnectionCharacteristic(ABC):
  4. """An abstract base for an object that can set, get and reset a
  5. per-connection characteristic, typically one that gets reset when the
  6. connection is returned to the connection pool.
  7. transaction isolation is the canonical example, and the
  8. ``IsolationLevelCharacteristic`` implementation provides this for the
  9. ``DefaultDialect``.
  10. The ``ConnectionCharacteristic`` class should call upon the ``Dialect`` for
  11. the implementation of each method. The object exists strictly to serve as
  12. a dialect visitor that can be placed into the
  13. ``DefaultDialect.connection_characteristics`` dictionary where it will take
  14. effect for calls to :meth:`_engine.Connection.execution_options` and
  15. related APIs.
  16. .. versionadded:: 1.4
  17. """
  18. __slots__ = ()
  19. transactional = False
  20. @abc.abstractmethod
  21. def reset_characteristic(self, dialect, dbapi_conn):
  22. """Reset the characteristic on the connection to its default value."""
  23. @abc.abstractmethod
  24. def set_characteristic(self, dialect, dbapi_conn, value):
  25. """set characteristic on the connection to a given value."""
  26. @abc.abstractmethod
  27. def get_characteristic(self, dialect, dbapi_conn):
  28. """Given a DBAPI connection, get the current value of the
  29. characteristic.
  30. """
  31. class IsolationLevelCharacteristic(ConnectionCharacteristic):
  32. transactional = True
  33. def reset_characteristic(self, dialect, dbapi_conn):
  34. dialect.reset_isolation_level(dbapi_conn)
  35. def set_characteristic(self, dialect, dbapi_conn, value):
  36. dialect.set_isolation_level(dbapi_conn, value)
  37. def get_characteristic(self, dialect, dbapi_conn):
  38. return dialect.get_isolation_level(dbapi_conn)