Skip to content

Commit fe65ddd

Browse files
committed
refactor useless interfaces
1 parent 52a6bfd commit fe65ddd

File tree

8 files changed

+49
-250
lines changed

8 files changed

+49
-250
lines changed

ydb/aio/query/pool.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
retry_operation_async,
1515
)
1616
from ... import convert
17+
from ..._grpc.grpcwrapper import common_utils
1718

1819
logger = logging.getLogger(__name__)
1920

2021

2122
class QuerySessionPoolAsync:
2223
"""QuerySessionPoolAsync is an object to simplify operations with sessions of Query Service."""
2324

24-
def __init__(self, driver: base.SupportedDriverType):
25+
def __init__(self, driver: common_utils.SupportedDriverType):
2526
"""
2627
:param driver: A driver instance
2728
"""
@@ -85,7 +86,7 @@ def __init__(self, pool: QuerySessionPoolAsync):
8586
self._pool = pool
8687
self._session = QuerySessionAsync(pool._driver)
8788

88-
async def __aenter__(self) -> base.IQuerySession:
89+
async def __aenter__(self) -> QuerySessionAsync:
8990
await self._session.create()
9091
return self._session
9192

ydb/aio/query/session.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class QuerySessionAsync(BaseQuerySession):
2828

2929
def __init__(
3030
self,
31-
driver: base.SupportedDriverType,
31+
driver: common_utils.SupportedDriverType,
3232
settings: Optional[base.QueryClientSettings] = None,
3333
loop: asyncio.AbstractEventLoop = None,
3434
):
@@ -90,7 +90,7 @@ async def create(self) -> "QuerySessionAsync":
9090

9191
return self
9292

93-
def transaction(self, tx_mode=None) -> base.IQueryTxContext:
93+
def transaction(self, tx_mode=None) -> QueryTxContextAsync:
9494
self._state._check_session_ready_to_use()
9595
tx_mode = tx_mode if tx_mode else _ydb_query_public.QuerySerializableReadWrite()
9696

ydb/aio/query/transaction.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async def _ensure_prev_stream_finished(self) -> None:
4646
pass
4747
self._prev_stream = None
4848

49-
async def begin(self, settings: Optional[base.QueryClientSettings] = None) -> None:
49+
async def begin(self, settings: Optional[base.QueryClientSettings] = None) -> "QueryTxContextAsync":
5050
"""WARNING: This API is experimental and could be changed.
5151
5252
Explicitly begins a transaction
@@ -79,6 +79,15 @@ async def commit(self, settings: Optional[base.QueryClientSettings] = None) -> N
7979
await self._commit_call(settings)
8080

8181
async def rollback(self, settings: Optional[base.QueryClientSettings] = None) -> None:
82+
"""WARNING: This API is experimental and could be changed.
83+
84+
Calls rollback on a transaction if it is open otherwise is no-op. If transaction execution
85+
failed then this method raises PreconditionFailed.
86+
87+
:param settings: A request settings
88+
89+
:return: A committed transaction or exception if commit is failed
90+
"""
8291
if self._tx_state._already_in(QueryTxStateEnum.ROLLBACKED):
8392
return
8493

@@ -93,16 +102,18 @@ async def rollback(self, settings: Optional[base.QueryClientSettings] = None) ->
93102
async def execute(
94103
self,
95104
query: str,
105+
parameters: Optional[dict] = None,
96106
commit_tx: Optional[bool] = False,
97107
syntax: Optional[base.QuerySyntax] = None,
98108
exec_mode: Optional[base.QueryExecMode] = None,
99-
parameters: Optional[dict] = None,
100109
concurrent_result_sets: Optional[bool] = False,
110+
settings: Optional[base.QueryClientSettings] = None,
101111
) -> AsyncResponseContextIterator:
102112
"""WARNING: This API is experimental and could be changed.
103113
104114
Sends a query to Query Service
105115
:param query: (YQL or SQL text) to be executed.
116+
:param parameters: dict with parameters and YDB types;
106117
:param commit_tx: A special flag that allows transaction commit.
107118
:param syntax: Syntax of the query, which is a one from the following choises:
108119
1) QuerySyntax.YQL_V1, which is default;
@@ -112,7 +123,6 @@ async def execute(
112123
2) QueryExecMode.EXPLAIN;
113124
3) QueryExecMode.VALIDATE;
114125
4) QueryExecMode.PARSE.
115-
:param parameters: dict with parameters and YDB types;
116126
:param concurrent_result_sets: A flag to allow YDB mix parts of different result sets. Default is False;
117127
118128
:return: Iterator with result sets
@@ -128,6 +138,7 @@ async def execute(
128138
concurrent_result_sets=concurrent_result_sets,
129139
)
130140

141+
settings = settings if settings is not None else self.session._settings
131142
self._prev_stream = AsyncResponseContextIterator(
132143
stream_it,
133144
lambda resp: base.wrap_execute_query_response(

ydb/query/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111
import logging
1212

1313
from .base import (
14-
IQueryClient,
15-
SupportedDriverType,
1614
QueryClientSettings,
1715
)
1816

1917
from .session import QuerySessionSync
2018

19+
from .._grpc.grpcwrapper import common_utils
2120
from .._grpc.grpcwrapper.ydb_query_public_types import (
2221
QueryOnlineReadOnly,
2322
QuerySerializableReadWrite,
@@ -30,8 +29,8 @@
3029
logger = logging.getLogger(__name__)
3130

3231

33-
class QueryClientSync(IQueryClient):
34-
def __init__(self, driver: SupportedDriverType, query_client_settings: QueryClientSettings = None):
32+
class QueryClientSync:
33+
def __init__(self, driver: common_utils.SupportedDriverType, query_client_settings: QueryClientSettings = None):
3534
logger.warning("QueryClientSync is an experimental API, which could be changed.")
3635
self._driver = driver
3736
self._settings = query_client_settings

ydb/query/base.py

Lines changed: 5 additions & 230 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
import enum
33
import functools
44

5+
import typing
56
from typing import (
6-
Iterator,
77
Optional,
88
)
99

10-
from .._grpc.grpcwrapper.common_utils import (
11-
SupportedDriverType,
12-
)
1310
from .._grpc.grpcwrapper import ydb_query
1411
from .._grpc.grpcwrapper.ydb_query_public_types import (
1512
BaseQueryTxMode,
@@ -20,6 +17,9 @@
2017
from .. import _utilities
2118
from .. import _apis
2219

20+
if typing.TYPE_CHECKING:
21+
from .transaction import BaseQueryTxContext
22+
2323

2424
class QuerySyntax(enum.IntEnum):
2525
UNSPECIFIED = 0
@@ -117,231 +117,6 @@ def set_attached(self, attached: bool) -> "IQuerySessionState":
117117
pass
118118

119119

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-
345120
def create_execute_query_request(
346121
query: str,
347122
session_id: str,
@@ -392,7 +167,7 @@ def create_execute_query_request(
392167
def wrap_execute_query_response(
393168
rpc_state: RpcState,
394169
response_pb: _apis.ydb_query.ExecuteQueryResponsePart,
395-
tx: Optional[IQueryTxContext] = None,
170+
tx: Optional["BaseQueryTxContext"] = None,
396171
commit_tx: Optional[bool] = False,
397172
settings: Optional[QueryClientSettings] = None,
398173
) -> convert.ResultSet:

0 commit comments

Comments
 (0)