1from __future__ import annotations
 2
 3from typing import Any
 4
 5from plain import signals
 6
 7from .connections import get_connection, has_connection
 8from .exceptions import (
 9    DatabaseError,
10    DatabaseErrorWrapper,
11    DataError,
12    Error,
13    IntegrityError,
14    InterfaceError,
15    InternalError,
16    NotSupportedError,
17    OperationalError,
18    ProgrammingError,
19)
20
21PLAIN_VERSION_PICKLE_KEY = "_plain_version"
22
23
24# Register an event to reset saved queries when a Plain request is started.
25def reset_queries(**kwargs: Any) -> None:
26    if has_connection():
27        get_connection().queries_log.clear()
28
29
30signals.request_started.connect(reset_queries)
31
32
33# Register an event to reset transaction state and close connections past
34# their lifetime.
35def close_old_connections(**kwargs: Any) -> None:
36    if has_connection():
37        get_connection().close_if_unusable_or_obsolete()
38
39
40signals.request_started.connect(close_old_connections)
41signals.request_finished.connect(close_old_connections)
42
43
44__all__ = [
45    "get_connection",
46    "has_connection",
47    "PLAIN_VERSION_PICKLE_KEY",
48    "Error",
49    "InterfaceError",
50    "DatabaseError",
51    "DataError",
52    "OperationalError",
53    "IntegrityError",
54    "InternalError",
55    "ProgrammingError",
56    "NotSupportedError",
57    "DatabaseErrorWrapper",
58    "close_old_connections",
59]