Skip to content

Commit 8f3b8af

Browse files
committed
docstrings for base interfaces
1 parent aa76736 commit 8f3b8af

File tree

3 files changed

+169
-15
lines changed

3 files changed

+169
-15
lines changed

ydb/query/base.py

Lines changed: 124 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@
1919
from .. import _utilities
2020

2121

22+
class QuerySyntax(enum.IntEnum):
23+
UNSPECIFIED = 0
24+
YQL_V1 = 1
25+
PG = 2
26+
27+
28+
class QueryExecMode(enum.IntEnum):
29+
UNSPECIFIED = 0
30+
PARSE = 10
31+
VALIDATE = 20
32+
EXPLAIN = 30
33+
EXECUTE = 50
34+
35+
2236
class QueryClientSettings:
2337
pass
2438

@@ -60,24 +74,60 @@ def set_attached(self, attached: bool) -> "IQuerySessionState":
6074

6175

6276
class IQuerySession(abc.ABC):
77+
"""Session object for Query Service. It is not recommended to control
78+
session's lifecycle manually - use a QuerySessionPool is always a better choise.
79+
"""
80+
6381
@abc.abstractmethod
6482
def __init__(self, driver: SupportedDriverType, settings: QueryClientSettings = None):
6583
pass
6684

6785
@abc.abstractmethod
6886
def create(self) -> "IQuerySession":
87+
"""
88+
Creates a Session of Query Service on server side and attaches it.
89+
90+
:return: Session object.
91+
"""
6992
pass
7093

7194
@abc.abstractmethod
7295
def delete(self) -> None:
96+
"""
97+
Deletes a Session of Query Service on server side and releases resources.
98+
99+
:return: None
100+
"""
73101
pass
74102

75103
@abc.abstractmethod
76104
def transaction(self, tx_mode: BaseQueryTxMode) -> "IQueryTxContext":
105+
"""
106+
Creates a transaction context manager with specified transaction mode.
107+
108+
:param tx_mode: Transaction mode, which is a one from the following choises:
109+
1) QuerySerializableReadWrite() which is default mode;
110+
2) QueryOnlineReadOnly(allow_inconsistent_reads=False);
111+
3) QuerySnapshotReadOnly();
112+
4) QueryStaleReadOnly().
113+
114+
:return: transaction context manager.
115+
"""
77116
pass
78117

79118

80119
class IQueryTxContext(abc.ABC):
120+
"""
121+
An object that provides a simple transaction context manager that allows statements execution
122+
in a transaction. You don't have to open transaction explicitly, because context manager encapsulates
123+
transaction control logic, and opens new transaction if:
124+
1) By explicit .begin();
125+
2) On execution of a first statement, which is strictly recommended method, because that avoids
126+
useless round trip
127+
128+
This context manager is not thread-safe, so you should not manipulate on it concurrently.
129+
"""
130+
81131
@abc.abstractmethod
82132
def __init__(
83133
self,
@@ -90,36 +140,109 @@ def __init__(
90140

91141
@abc.abstractmethod
92142
def __enter__(self):
143+
"""
144+
Enters a context manager and returns a transaction
145+
146+
:return: A transaction instance
147+
"""
93148
pass
94149

95150
@abc.abstractmethod
96151
def __exit__(self, *args, **kwargs):
152+
"""
153+
Closes a transaction context manager and rollbacks transaction if
154+
it is not rolled back explicitly
155+
"""
97156
pass
98157

99158
@property
100159
@abc.abstractmethod
101160
def session_id(self):
161+
"""
162+
A transaction's session id
163+
164+
:return: A transaction's session id
165+
"""
102166
pass
103167

104168
@property
105169
@abc.abstractmethod
106170
def tx_id(self):
171+
"""
172+
Returns a id of open transaction or None otherwise
173+
174+
:return: A id of open transaction or None otherwise
175+
"""
107176
pass
108177

109178
@abc.abstractmethod
110179
def begin(settings: QueryClientSettings = None):
180+
"""
181+
Explicitly begins a transaction
182+
183+
:param settings: A request settings
184+
185+
:return: None
186+
"""
111187
pass
112188

113189
@abc.abstractmethod
114190
def commit(settings: QueryClientSettings = None):
191+
"""
192+
Calls commit on a transaction if it is open. If transaction execution
193+
failed then this method raises PreconditionFailed.
194+
195+
:param settings: A request settings
196+
197+
:return: A committed transaction or exception if commit is failed
198+
"""
115199
pass
116200

117201
@abc.abstractmethod
118202
def rollback(settings: QueryClientSettings = None):
203+
"""
204+
Calls rollback on a transaction if it is open. If transaction execution
205+
failed then this method raises PreconditionFailed.
206+
207+
:param settings: A request settings
208+
209+
:return: A rolled back transaction or exception if rollback is failed
210+
"""
119211
pass
120212

121213
@abc.abstractmethod
122-
def execute(query: str, commit_tx=False):
214+
def execute(
215+
self,
216+
query: str,
217+
commit_tx: bool = False,
218+
tx_mode: BaseQueryTxMode = None,
219+
syntax: QuerySyntax = None,
220+
exec_mode: QueryExecMode = None,
221+
parameters: dict = None,
222+
concurrent_result_sets: bool = False,
223+
):
224+
"""
225+
Sends a query to Query Service
226+
:param query: (YQL or SQL text) to be executed.
227+
:param commit_tx: A special flag that allows transaction commit.
228+
:param tx_mode: Transaction mode, which is a one from the following choises:
229+
1) QuerySerializableReadWrite() which is default mode;
230+
2) QueryOnlineReadOnly(allow_inconsistent_reads=False);
231+
3) QuerySnapshotReadOnly();
232+
4) QueryStaleReadOnly().
233+
:param syntax: Syntax of the query, which is a one from the following choises:
234+
1) QuerySyntax.YQL_V1, which is default;
235+
2) QuerySyntax.PG.
236+
:param exec_mode: Exec mode of the query, which is a one from the following choises:
237+
1) QueryExecMode.EXECUTE, which is default;
238+
2) QueryExecMode.EXPLAIN;
239+
3) QueryExecMode.VALIDATE;
240+
4) QueryExecMode.PARSE.
241+
:param parameters: dict with parameters and YDB types;
242+
:param concurrent_result_sets: A flag to allow YDB mix parts of different result sets. Default is False;
243+
244+
:return: Iterator with result sets
245+
"""
123246
pass
124247

125248

@@ -132,20 +255,6 @@ def session(self) -> IQuerySession:
132255
pass
133256

134257

135-
class QuerySyntax(enum.IntEnum):
136-
UNSPECIFIED = 0
137-
YQL_V1 = 1
138-
PG = 2
139-
140-
141-
class QueryExecMode(enum.IntEnum):
142-
UNSPECIFIED = 0
143-
PARSE = 10
144-
VALIDATE = 20
145-
EXPLAIN = 30
146-
EXECUTE = 50
147-
148-
149258
def create_execute_query_request(
150259
query: str,
151260
session_id: str,

ydb/query/pool.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ def __init__(self, driver: base.SupportedDriverType):
1717
"""
1818
:param driver: A driver instance
1919
"""
20+
2021
self._driver = driver
2122

2223
def checkout(self):
2324
"""Return a Session context manager, that opens session on enter and closes session on exit."""
25+
2426
return SimpleQuerySessionCheckout(self)
2527

2628
def retry_operation_sync(self, callee: Callable, retry_settings: RetrySettings = None, *args, **kwargs):
@@ -31,6 +33,7 @@ def retry_operation_sync(self, callee: Callable, retry_settings: RetrySettings =
3133
3234
:return: Result sets or exception in case of execution errors.
3335
"""
36+
3437
retry_settings = RetrySettings() if retry_settings is None else retry_settings
3538

3639
def wrapped_callee():
@@ -47,6 +50,7 @@ def execute_with_retries(self, query: str, retry_settings: RetrySettings = None,
4750
4851
:return: Result sets or exception in case of execution errors.
4952
"""
53+
5054
retry_settings = RetrySettings() if retry_settings is None else retry_settings
5155
with self.checkout() as session:
5256

ydb/query/session.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ def _execute_call(
181181

182182

183183
class QuerySessionSync(BaseQuerySession):
184+
"""Session object for Query Service. It is not recommended to control
185+
session's lifecycle manually - use a QuerySessionPool is always a better choise.
186+
"""
187+
184188
_stream = None
185189

186190
def _attach(self):
@@ -214,6 +218,11 @@ def _check_session_status_loop(self, status_stream):
214218
pass
215219

216220
def delete(self) -> None:
221+
"""
222+
Deletes a Session of Query Service on server side and releases resources.
223+
224+
:return: None
225+
"""
217226
if self._state._already_in(QuerySessionStateEnum.CLOSED):
218227
return
219228

@@ -222,6 +231,11 @@ def delete(self) -> None:
222231
self._stream.cancel()
223232

224233
def create(self) -> "QuerySessionSync":
234+
"""
235+
Creates a Session of Query Service on server side and attaches it.
236+
237+
:return: QuerySessionSync object.
238+
"""
225239
if self._state._already_in(QuerySessionStateEnum.CREATED):
226240
return
227241

@@ -232,6 +246,17 @@ def create(self) -> "QuerySessionSync":
232246
return self
233247

234248
def transaction(self, tx_mode: base.BaseQueryTxMode = None) -> base.IQueryTxContext:
249+
"""
250+
Creates a transaction context manager with specified transaction mode.
251+
:param tx_mode: Transaction mode, which is a one from the following choises:
252+
1) QuerySerializableReadWrite() which is default mode;
253+
2) QueryOnlineReadOnly(allow_inconsistent_reads=False);
254+
3) QuerySnapshotReadOnly();
255+
4) QueryStaleReadOnly().
256+
257+
:return transaction context manager.
258+
259+
"""
235260
self._state._check_session_ready_to_use()
236261

237262
return BaseTxContext(
@@ -251,6 +276,22 @@ def execute(
251276
concurrent_result_sets: bool = False,
252277
empty_tx_control: bool = False,
253278
):
279+
"""
280+
Sends a query to Query Service
281+
:param query: (YQL or SQL text) to be executed.
282+
:param tx_mode: Transaction mode, which is a one from the following choises:
283+
1) QuerySerializableReadWrite() which is default mode;
284+
2) QueryOnlineReadOnly(allow_inconsistent_reads=False);
285+
3) QuerySnapshotReadOnly();
286+
4) QueryStaleReadOnly().
287+
:param syntax: Syntax of the query, which is a one from the following choises:
288+
1) QuerySyntax.YQL_V1, which is default;
289+
2) QuerySyntax.PG.
290+
:param parameters: dict with parameters and YDB types;
291+
:param concurrent_result_sets: A flag to allow YDB mix parts of different result sets. Default is False;
292+
293+
:return: Iterator with result sets
294+
"""
254295
self._state._check_session_ready_to_use()
255296

256297
stream_it = self._execute_call(

0 commit comments

Comments
 (0)