|
2 | 2 | import enum
|
3 | 3 | import functools
|
4 | 4 |
|
| 5 | +import typing |
5 | 6 | from typing import (
|
6 |
| - Iterator, |
7 | 7 | Optional,
|
8 | 8 | )
|
9 | 9 |
|
10 |
| -from .._grpc.grpcwrapper.common_utils import ( |
11 |
| - SupportedDriverType, |
12 |
| -) |
13 | 10 | from .._grpc.grpcwrapper import ydb_query
|
14 | 11 | from .._grpc.grpcwrapper.ydb_query_public_types import (
|
15 | 12 | BaseQueryTxMode,
|
|
20 | 17 | from .. import _utilities
|
21 | 18 | from .. import _apis
|
22 | 19 |
|
| 20 | +if typing.TYPE_CHECKING: |
| 21 | + from .transaction import BaseQueryTxContext |
| 22 | + |
23 | 23 |
|
24 | 24 | class QuerySyntax(enum.IntEnum):
|
25 | 25 | UNSPECIFIED = 0
|
@@ -117,231 +117,6 @@ def set_attached(self, attached: bool) -> "IQuerySessionState":
|
117 | 117 | pass
|
118 | 118 |
|
119 | 119 |
|
120 |
| -class IQuerySession(abc.ABC): |
121 |
| - """Session object for Query Service. It is not recommended to control |
122 |
| - session's lifecycle manually - use a QuerySessionPool is always a better choise. |
123 |
| - """ |
124 |
| - |
125 |
| - @abc.abstractmethod |
126 |
| - def __init__(self, driver: SupportedDriverType, settings: Optional[QueryClientSettings] = None): |
127 |
| - pass |
128 |
| - |
129 |
| - @abc.abstractmethod |
130 |
| - def create(self) -> "IQuerySession": |
131 |
| - """WARNING: This API is experimental and could be changed. |
132 |
| -
|
133 |
| - Creates a Session of Query Service on server side and attaches it. |
134 |
| -
|
135 |
| - :return: Session object. |
136 |
| - """ |
137 |
| - pass |
138 |
| - |
139 |
| - @abc.abstractmethod |
140 |
| - def delete(self) -> None: |
141 |
| - """WARNING: This API is experimental and could be changed. |
142 |
| -
|
143 |
| - Deletes a Session of Query Service on server side and releases resources. |
144 |
| -
|
145 |
| - :return: None |
146 |
| - """ |
147 |
| - pass |
148 |
| - |
149 |
| - @abc.abstractmethod |
150 |
| - def transaction(self, tx_mode: Optional[BaseQueryTxMode] = None) -> "IQueryTxContext": |
151 |
| - """WARNING: This API is experimental and could be changed. |
152 |
| -
|
153 |
| - Creates a transaction context manager with specified transaction mode. |
154 |
| -
|
155 |
| - :param tx_mode: Transaction mode, which is a one from the following choises: |
156 |
| - 1) QuerySerializableReadWrite() which is default mode; |
157 |
| - 2) QueryOnlineReadOnly(allow_inconsistent_reads=False); |
158 |
| - 3) QuerySnapshotReadOnly(); |
159 |
| - 4) QueryStaleReadOnly(). |
160 |
| -
|
161 |
| - :return: transaction context manager. |
162 |
| - """ |
163 |
| - pass |
164 |
| - |
165 |
| - @abc.abstractmethod |
166 |
| - def execute( |
167 |
| - self, |
168 |
| - query: str, |
169 |
| - parameters: Optional[dict] = None, |
170 |
| - syntax: Optional[QuerySyntax] = None, |
171 |
| - exec_mode: Optional[QueryExecMode] = None, |
172 |
| - concurrent_result_sets: Optional[bool] = False, |
173 |
| - ) -> Iterator: |
174 |
| - """WARNING: This API is experimental and could be changed. |
175 |
| -
|
176 |
| - Sends a query to Query Service |
177 |
| - :param query: (YQL or SQL text) to be executed. |
178 |
| - :param syntax: Syntax of the query, which is a one from the following choises: |
179 |
| - 1) QuerySyntax.YQL_V1, which is default; |
180 |
| - 2) QuerySyntax.PG. |
181 |
| - :param parameters: dict with parameters and YDB types; |
182 |
| - :param concurrent_result_sets: A flag to allow YDB mix parts of different result sets. Default is False; |
183 |
| -
|
184 |
| - :return: Iterator with result sets |
185 |
| - """ |
186 |
| - |
187 |
| - |
188 |
| -class IQueryTxContext(abc.ABC): |
189 |
| - """ |
190 |
| - An object that provides a simple transaction context manager that allows statements execution |
191 |
| - in a transaction. You don't have to open transaction explicitly, because context manager encapsulates |
192 |
| - transaction control logic, and opens new transaction if: |
193 |
| - 1) By explicit .begin(); |
194 |
| - 2) On execution of a first statement, which is strictly recommended method, because that avoids |
195 |
| - useless round trip |
196 |
| -
|
197 |
| - This context manager is not thread-safe, so you should not manipulate on it concurrently. |
198 |
| - """ |
199 |
| - |
200 |
| - @abc.abstractmethod |
201 |
| - def __init__( |
202 |
| - self, |
203 |
| - driver: SupportedDriverType, |
204 |
| - session_state: IQuerySessionState, |
205 |
| - session: IQuerySession, |
206 |
| - tx_mode: BaseQueryTxMode, |
207 |
| - ): |
208 |
| - """ |
209 |
| - An object that provides a simple transaction context manager that allows statements execution |
210 |
| - in a transaction. You don't have to open transaction explicitly, because context manager encapsulates |
211 |
| - transaction control logic, and opens new transaction if: |
212 |
| -
|
213 |
| - 1) By explicit .begin() method; |
214 |
| - 2) On execution of a first statement, which is strictly recommended method, because that avoids useless round trip |
215 |
| -
|
216 |
| - This context manager is not thread-safe, so you should not manipulate on it concurrently. |
217 |
| -
|
218 |
| - :param driver: A driver instance |
219 |
| - :param session_state: A state of session |
220 |
| - :param tx_mode: Transaction mode, which is a one from the following choises: |
221 |
| - 1) QuerySerializableReadWrite() which is default mode; |
222 |
| - 2) QueryOnlineReadOnly(allow_inconsistent_reads=False); |
223 |
| - 3) QuerySnapshotReadOnly(); |
224 |
| - 4) QueryStaleReadOnly(). |
225 |
| - """ |
226 |
| - pass |
227 |
| - |
228 |
| - @abc.abstractmethod |
229 |
| - def __enter__(self) -> "IQueryTxContext": |
230 |
| - """ |
231 |
| - Enters a context manager and returns a transaction |
232 |
| -
|
233 |
| - :return: A transaction instance |
234 |
| - """ |
235 |
| - pass |
236 |
| - |
237 |
| - @abc.abstractmethod |
238 |
| - def __exit__(self, *args, **kwargs): |
239 |
| - """ |
240 |
| - Closes a transaction context manager and rollbacks transaction if |
241 |
| - it is not finished explicitly |
242 |
| - """ |
243 |
| - pass |
244 |
| - |
245 |
| - @property |
246 |
| - @abc.abstractmethod |
247 |
| - def session_id(self) -> str: |
248 |
| - """ |
249 |
| - A transaction's session id |
250 |
| -
|
251 |
| - :return: A transaction's session id |
252 |
| - """ |
253 |
| - pass |
254 |
| - |
255 |
| - @property |
256 |
| - @abc.abstractmethod |
257 |
| - def tx_id(self) -> Optional[str]: |
258 |
| - """ |
259 |
| - Returns an id of open transaction or None otherwise |
260 |
| -
|
261 |
| - :return: An id of open transaction or None otherwise |
262 |
| - """ |
263 |
| - pass |
264 |
| - |
265 |
| - @abc.abstractmethod |
266 |
| - def begin(self, settings: Optional[QueryClientSettings] = None) -> "IQueryTxContext": |
267 |
| - """WARNING: This API is experimental and could be changed. |
268 |
| -
|
269 |
| - Explicitly begins a transaction |
270 |
| -
|
271 |
| - :param settings: A request settings |
272 |
| -
|
273 |
| - :return: Transaction object or exception if begin is failed |
274 |
| - """ |
275 |
| - pass |
276 |
| - |
277 |
| - @abc.abstractmethod |
278 |
| - def commit(self, settings: Optional[QueryClientSettings] = None) -> None: |
279 |
| - """WARNING: This API is experimental and could be changed. |
280 |
| -
|
281 |
| - Calls commit on a transaction if it is open. If transaction execution |
282 |
| - failed then this method raises PreconditionFailed. |
283 |
| -
|
284 |
| - :param settings: A request settings |
285 |
| -
|
286 |
| - :return: None or exception if commit is failed |
287 |
| - """ |
288 |
| - pass |
289 |
| - |
290 |
| - @abc.abstractmethod |
291 |
| - def rollback(self, settings: Optional[QueryClientSettings] = None) -> None: |
292 |
| - """WARNING: This API is experimental and could be changed. |
293 |
| -
|
294 |
| - Calls rollback on a transaction if it is open. If transaction execution |
295 |
| - failed then this method raises PreconditionFailed. |
296 |
| -
|
297 |
| - :param settings: A request settings |
298 |
| -
|
299 |
| - :return: None or exception if rollback is failed |
300 |
| - """ |
301 |
| - pass |
302 |
| - |
303 |
| - @abc.abstractmethod |
304 |
| - def execute( |
305 |
| - self, |
306 |
| - query: str, |
307 |
| - commit_tx: Optional[bool] = False, |
308 |
| - syntax: Optional[QuerySyntax] = None, |
309 |
| - exec_mode: Optional[QueryExecMode] = None, |
310 |
| - parameters: Optional[dict] = None, |
311 |
| - concurrent_result_sets: Optional[bool] = False, |
312 |
| - settings: Optional[QueryClientSettings] = None, |
313 |
| - ) -> Iterator: |
314 |
| - """WARNING: This API is experimental and could be changed. |
315 |
| -
|
316 |
| - Sends a query to Query Service |
317 |
| - :param query: (YQL or SQL text) to be executed. |
318 |
| - :param commit_tx: A special flag that allows transaction commit. |
319 |
| - :param syntax: Syntax of the query, which is a one from the following choises: |
320 |
| - 1) QuerySyntax.YQL_V1, which is default; |
321 |
| - 2) QuerySyntax.PG. |
322 |
| - :param exec_mode: Exec mode of the query, which is a one from the following choises: |
323 |
| - 1) QueryExecMode.EXECUTE, which is default; |
324 |
| - 2) QueryExecMode.EXPLAIN; |
325 |
| - 3) QueryExecMode.VALIDATE; |
326 |
| - 4) QueryExecMode.PARSE. |
327 |
| - :param parameters: dict with parameters and YDB types; |
328 |
| - :param concurrent_result_sets: A flag to allow YDB mix parts of different result sets. Default is False; |
329 |
| - :param settings: An additional request settings QueryClientSettings; |
330 |
| -
|
331 |
| - :return: Iterator with result sets |
332 |
| - """ |
333 |
| - pass |
334 |
| - |
335 |
| - |
336 |
| -class IQueryClient(abc.ABC): |
337 |
| - def __init__(self, driver: SupportedDriverType, query_client_settings: Optional[QueryClientSettings] = None): |
338 |
| - pass |
339 |
| - |
340 |
| - @abc.abstractmethod |
341 |
| - def session(self) -> IQuerySession: |
342 |
| - pass |
343 |
| - |
344 |
| - |
345 | 120 | def create_execute_query_request(
|
346 | 121 | query: str,
|
347 | 122 | session_id: str,
|
@@ -392,7 +167,7 @@ def create_execute_query_request(
|
392 | 167 | def wrap_execute_query_response(
|
393 | 168 | rpc_state: RpcState,
|
394 | 169 | response_pb: _apis.ydb_query.ExecuteQueryResponsePart,
|
395 |
| - tx: Optional[IQueryTxContext] = None, |
| 170 | + tx: Optional["BaseQueryTxContext"] = None, |
396 | 171 | commit_tx: Optional[bool] = False,
|
397 | 172 | settings: Optional[QueryClientSettings] = None,
|
398 | 173 | ) -> convert.ResultSet:
|
|
0 commit comments