Skip to content

Commit f13fa1a

Browse files
committed
docstrings for base interfaces
1 parent aa76736 commit f13fa1a

File tree

3 files changed

+168
-15
lines changed

3 files changed

+168
-15
lines changed

ydb/query/base.py

Lines changed: 123 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,59 @@ 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+
"""
81130
@abc.abstractmethod
82131
def __init__(
83132
self,
@@ -90,36 +139,109 @@ def __init__(
90139

91140
@abc.abstractmethod
92141
def __enter__(self):
142+
"""
143+
Enters a context manager and returns a transaction
144+
145+
:return: A transaction instance
146+
"""
93147
pass
94148

95149
@abc.abstractmethod
96150
def __exit__(self, *args, **kwargs):
151+
"""
152+
Closes a transaction context manager and rollbacks transaction if
153+
it is not rolled back explicitly
154+
"""
97155
pass
98156

99157
@property
100158
@abc.abstractmethod
101159
def session_id(self):
160+
"""
161+
A transaction's session id
162+
163+
:return: A transaction's session id
164+
"""
102165
pass
103166

104167
@property
105168
@abc.abstractmethod
106169
def tx_id(self):
170+
"""
171+
Returns a id of open transaction or None otherwise
172+
173+
:return: A id of open transaction or None otherwise
174+
"""
107175
pass
108176

109177
@abc.abstractmethod
110178
def begin(settings: QueryClientSettings = None):
179+
"""
180+
Explicitly begins a transaction
181+
182+
:param settings: A request settings
183+
184+
:return: None
185+
"""
111186
pass
112187

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

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

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

125247

@@ -132,20 +254,6 @@ def session(self) -> IQuerySession:
132254
pass
133255

134256

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-
149257
def create_execute_query_request(
150258
query: str,
151259
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)