characteristics.py 2.0 KB

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