|
| 1 | +import asyncio |
| 2 | + |
| 3 | +from typing import ( |
| 4 | + Optional, |
| 5 | +) |
| 6 | + |
| 7 | +from .. import _utilities |
| 8 | +from ... import issues |
| 9 | +from ..._grpc.grpcwrapper import common_utils |
| 10 | +from ...query import base |
| 11 | +from ...query.session import ( |
| 12 | + BaseQuerySession, |
| 13 | + QuerySessionStateEnum, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class AsyncResponseContextIterator(_utilities.AsyncResponseIterator): |
| 18 | + async def __aenter__(self) -> "AsyncResponseContextIterator": |
| 19 | + return self |
| 20 | + |
| 21 | + async def __aexit__(self, exc_type, exc_val, exc_tb): |
| 22 | + async for _ in self: |
| 23 | + pass |
| 24 | + |
| 25 | + |
| 26 | +class QuerySessionAsync(BaseQuerySession): |
| 27 | + """Session object for Query Service. It is not recommended to control |
| 28 | + session's lifecycle manually - use a QuerySessionPool is always a better choise. |
| 29 | + """ |
| 30 | + |
| 31 | + _loop: asyncio.AbstractEventLoop |
| 32 | + _status_stream: _utilities.AsyncResponseIterator = None |
| 33 | + |
| 34 | + def __init__(self, driver: base.SupportedDriverType, settings: Optional[base.QueryClientSettings] = None): |
| 35 | + super(QuerySessionAsync, self).__init__(driver, settings) |
| 36 | + self._loop = asyncio.get_running_loop() |
| 37 | + |
| 38 | + async def _attach(self) -> None: |
| 39 | + self._stream = await self._attach_call() |
| 40 | + self._status_stream = _utilities.AsyncResponseIterator( |
| 41 | + self._stream, |
| 42 | + lambda response: common_utils.ServerStatus.from_proto(response), |
| 43 | + ) |
| 44 | + |
| 45 | + first_response = await self._status_stream.next() |
| 46 | + if first_response.status != issues.StatusCode.SUCCESS: |
| 47 | + pass |
| 48 | + |
| 49 | + self._state.set_attached(True) |
| 50 | + self._state._change_state(QuerySessionStateEnum.CREATED) |
| 51 | + |
| 52 | + self._loop.create_task(self._check_session_status_loop(), name="check session status task") |
| 53 | + |
| 54 | + |
| 55 | + async def _check_session_status_loop(self) -> None: |
| 56 | + try: |
| 57 | + async for status in self._status_stream: |
| 58 | + if status.status != issues.StatusCode.SUCCESS: |
| 59 | + self._state.reset() |
| 60 | + self._state._change_state(QuerySessionStateEnum.CLOSED) |
| 61 | + except Exception: |
| 62 | + pass |
| 63 | + |
| 64 | + async def delete(self) -> None: |
| 65 | + """WARNING: This API is experimental and could be changed. |
| 66 | +
|
| 67 | + Deletes a Session of Query Service on server side and releases resources. |
| 68 | +
|
| 69 | + :return: None |
| 70 | + """ |
| 71 | + if self._state._already_in(QuerySessionStateEnum.CLOSED): |
| 72 | + return |
| 73 | + |
| 74 | + self._state._check_invalid_transition(QuerySessionStateEnum.CLOSED) |
| 75 | + await self._delete_call() |
| 76 | + self._stream.cancel() |
| 77 | + |
| 78 | + async def create(self) -> "QuerySessionAsync": |
| 79 | + """WARNING: This API is experimental and could be changed. |
| 80 | +
|
| 81 | + Creates a Session of Query Service on server side and attaches it. |
| 82 | +
|
| 83 | + :return: QuerySessionSync object. |
| 84 | + """ |
| 85 | + if self._state._already_in(QuerySessionStateEnum.CREATED): |
| 86 | + return |
| 87 | + |
| 88 | + self._state._check_invalid_transition(QuerySessionStateEnum.CREATED) |
| 89 | + await self._create_call() |
| 90 | + await self._attach() |
| 91 | + |
| 92 | + return self |
| 93 | + |
| 94 | + async def transaction(self, tx_mode) -> base.IQueryTxContext: |
| 95 | + return super().transaction(tx_mode) |
| 96 | + |
| 97 | + async def execute( |
| 98 | + self, |
| 99 | + query: str, |
| 100 | + parameters: dict = None, |
| 101 | + commit_tx: bool = False, |
| 102 | + syntax: base.QuerySyntax = None, |
| 103 | + exec_mode: base.QueryExecMode = None, |
| 104 | + concurrent_result_sets: bool = False, |
| 105 | + ) -> AsyncResponseContextIterator: |
| 106 | + """WARNING: This API is experimental and could be changed. |
| 107 | +
|
| 108 | + Sends a query to Query Service |
| 109 | + :param query: (YQL or SQL text) to be executed. |
| 110 | + :param syntax: Syntax of the query, which is a one from the following choises: |
| 111 | + 1) QuerySyntax.YQL_V1, which is default; |
| 112 | + 2) QuerySyntax.PG. |
| 113 | + :param parameters: dict with parameters and YDB types; |
| 114 | + :param concurrent_result_sets: A flag to allow YDB mix parts of different result sets. Default is False; |
| 115 | +
|
| 116 | + :return: Iterator with result sets |
| 117 | + """ |
| 118 | + self._state._check_session_ready_to_use() |
| 119 | + |
| 120 | + stream_it = await self._execute_call( |
| 121 | + query=query, |
| 122 | + commit_tx=True, |
| 123 | + syntax=syntax, |
| 124 | + exec_mode=exec_mode, |
| 125 | + parameters=parameters, |
| 126 | + concurrent_result_sets=concurrent_result_sets, |
| 127 | + ) |
| 128 | + |
| 129 | + return AsyncResponseContextIterator( |
| 130 | + stream_it, |
| 131 | + lambda resp: base.wrap_execute_query_response(rpc_state=None, response_pb=resp), |
| 132 | + ) |
0 commit comments