19
19
from .. import _utilities
20
20
21
21
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
+
22
36
class QueryClientSettings :
23
37
pass
24
38
@@ -60,24 +74,59 @@ def set_attached(self, attached: bool) -> "IQuerySessionState":
60
74
61
75
62
76
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
+
63
81
@abc .abstractmethod
64
82
def __init__ (self , driver : SupportedDriverType , settings : QueryClientSettings = None ):
65
83
pass
66
84
67
85
@abc .abstractmethod
68
86
def create (self ) -> "IQuerySession" :
87
+ """
88
+ Creates a Session of Query Service on server side and attaches it.
89
+
90
+ :return: Session object.
91
+ """
69
92
pass
70
93
71
94
@abc .abstractmethod
72
95
def delete (self ) -> None :
96
+ """
97
+ Deletes a Session of Query Service on server side and releases resources.
98
+
99
+ :return: None
100
+ """
73
101
pass
74
102
75
103
@abc .abstractmethod
76
104
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
+ """
77
116
pass
78
117
79
118
80
119
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
+ """
81
130
@abc .abstractmethod
82
131
def __init__ (
83
132
self ,
@@ -90,36 +139,109 @@ def __init__(
90
139
91
140
@abc .abstractmethod
92
141
def __enter__ (self ):
142
+ """
143
+ Enters a context manager and returns a transaction
144
+
145
+ :return: A transaction instance
146
+ """
93
147
pass
94
148
95
149
@abc .abstractmethod
96
150
def __exit__ (self , * args , ** kwargs ):
151
+ """
152
+ Closes a transaction context manager and rollbacks transaction if
153
+ it is not rolled back explicitly
154
+ """
97
155
pass
98
156
99
157
@property
100
158
@abc .abstractmethod
101
159
def session_id (self ):
160
+ """
161
+ A transaction's session id
162
+
163
+ :return: A transaction's session id
164
+ """
102
165
pass
103
166
104
167
@property
105
168
@abc .abstractmethod
106
169
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
+ """
107
175
pass
108
176
109
177
@abc .abstractmethod
110
178
def begin (settings : QueryClientSettings = None ):
179
+ """
180
+ Explicitly begins a transaction
181
+
182
+ :param settings: A request settings
183
+
184
+ :return: None
185
+ """
111
186
pass
112
187
113
188
@abc .abstractmethod
114
189
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
+ """
115
198
pass
116
199
117
200
@abc .abstractmethod
118
201
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
+ """
119
210
pass
120
211
121
212
@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
+ """
123
245
pass
124
246
125
247
@@ -132,20 +254,6 @@ def session(self) -> IQuerySession:
132
254
pass
133
255
134
256
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
-
149
257
def create_execute_query_request (
150
258
query : str ,
151
259
session_id : str ,
0 commit comments