-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathinterfaces.py
More file actions
216 lines (156 loc) · 5.41 KB
/
Copy pathinterfaces.py
File metadata and controls
216 lines (156 loc) · 5.41 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from abc import ABC, abstractmethod
from collections.abc import AsyncGenerator, Callable, Mapping
from types import TracebackType
from typing import Any
try:
from typing import LiteralString
except ImportError:
from typing_extensions import LiteralString
ValueType = dict[str, Any] | list[Any]
RecordType = Mapping[str, Any]
TypeConverters = dict[str, dict[str, tuple[Callable, Callable, type | None]]]
class UndefinedParameterError(Exception):
pass
class MultipleRowsError(Exception):
pass
class TransactionABC(ABC):
@abstractmethod
async def __aenter__(self) -> "TransactionABC":
pass
@abstractmethod
async def __aexit__(self, exc_type: type, exc_value: BaseException, tb: TracebackType) -> None:
pass
@abstractmethod
async def start(self) -> None:
pass
@abstractmethod
async def commit(self) -> None:
pass
@abstractmethod
async def rollback(self) -> None:
pass
class ConnectionABC(ABC):
@property
@abstractmethod
def supports_advisory_lock(self) -> bool:
pass
@property
@abstractmethod
def supports_for_update(self) -> bool:
pass
@abstractmethod
async def execute(self, query: LiteralString, values: ValueType | None = None) -> None:
"""Execute a query, with bind values if needed
The query accepts named arguments i.e. `:name`in the query
with values set being a dictionary.
"""
pass
@abstractmethod
async def execute_many(self, query: LiteralString, values: list[ValueType]) -> None:
"""Execute a query for each set of values
The query accepts a list of named arguments i.e. `:name`in the
query with values set being a list of dictionaries.
"""
pass
@abstractmethod
async def fetch_all(
self, query: LiteralString, values: ValueType | None = None
) -> list[RecordType]:
"""Execute a query, returning all the result rows
The query accepts named arguments i.e. `:name`in the query
with values set being a dictionary.
"""
pass
@abstractmethod
async def fetch_first(
self, query: LiteralString, values: ValueType | None = None
) -> RecordType | None:
"""Execute a query, returning only the first result row
The query accepts named arguments i.e. `:name`in the query
with values set being a dictionary.
"""
pass
@abstractmethod
async def fetch_sole(
self, query: LiteralString, values: ValueType | None = None
) -> RecordType | None:
"""Execute a query, returning the only row or raising if there
are multiple rows returned.
The query accepts named arguments i.e. `:name`in the query
with values set being a dictionary.
"""
pass
@abstractmethod
async def fetch_val(self, query: LiteralString, values: ValueType | None = None) -> Any | None:
"""Execute a query, returning only a value
The query accepts named arguments i.e. `:name`in the query
with values set being a dictionary.
"""
pass
@abstractmethod
def iterate(
self,
query: LiteralString,
values: ValueType | None = None,
) -> AsyncGenerator[RecordType, None]:
"""Execute a query, and iterate over the result rows
The query accepts named arguments i.e. `:name`in the query
with values set being a dictionary.
"""
pass
@abstractmethod
def transaction(self, *, force_rollback: bool = False) -> "TransactionABC":
"""Open a transaction
.. code-block:: python
async with connection.transaction():
await connection.execute("SELECT 1")
Arguments:
force_rollback: Force the transaction to rollback on completion.
"""
pass
class BackendABC(ABC):
@abstractmethod
def __init__(
self, url: str, options: dict[str, Any] | None, type_converters: TypeConverters
) -> None:
pass
@abstractmethod
async def connect(self) -> None:
"""Connect to the database.
This will establish a connection pool if the backend supports
it.
"""
pass
@abstractmethod
async def disconnect(self, timeout: int | None = None) -> None:
"""Disconnect from the database.
This will wait up to timeout for any active queries to
complete.
"""
pass
@abstractmethod
async def acquire(self) -> ConnectionABC:
"""Acquire a connection to the database.
Don't forget to release it after usage,
.. code-block::: python
connection = await backend.acquire()
await connection.execute("SELECT 1")
await quart_db.release(connection)
"""
pass
@abstractmethod
async def release(self, connection: ConnectionABC) -> None:
"""Release a connection to the database.
This should be used with :meth:`acquire`,
.. code-block::: python
connection = await backend.acquire() await
connection.execute("SELECT 1") await
quart_db.release(connection)
"""
pass
@abstractmethod
async def _acquire_migration_connection(self) -> ConnectionABC:
pass
@abstractmethod
async def _release_migration_connection(self, connection: ConnectionABC) -> None:
pass