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,60 @@ 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
+ """
130
+
81
131
@abc .abstractmethod
82
132
def __init__ (
83
133
self ,
@@ -90,36 +140,109 @@ def __init__(
90
140
91
141
@abc .abstractmethod
92
142
def __enter__ (self ):
143
+ """
144
+ Enters a context manager and returns a transaction
145
+
146
+ :return: A transaction instance
147
+ """
93
148
pass
94
149
95
150
@abc .abstractmethod
96
151
def __exit__ (self , * args , ** kwargs ):
152
+ """
153
+ Closes a transaction context manager and rollbacks transaction if
154
+ it is not rolled back explicitly
155
+ """
97
156
pass
98
157
99
158
@property
100
159
@abc .abstractmethod
101
160
def session_id (self ):
161
+ """
162
+ A transaction's session id
163
+
164
+ :return: A transaction's session id
165
+ """
102
166
pass
103
167
104
168
@property
105
169
@abc .abstractmethod
106
170
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
+ """
107
176
pass
108
177
109
178
@abc .abstractmethod
110
179
def begin (settings : QueryClientSettings = None ):
180
+ """
181
+ Explicitly begins a transaction
182
+
183
+ :param settings: A request settings
184
+
185
+ :return: None
186
+ """
111
187
pass
112
188
113
189
@abc .abstractmethod
114
190
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
+ """
115
199
pass
116
200
117
201
@abc .abstractmethod
118
202
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
+ """
119
211
pass
120
212
121
213
@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
+ """
123
246
pass
124
247
125
248
@@ -132,20 +255,6 @@ def session(self) -> IQuerySession:
132
255
pass
133
256
134
257
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
258
def create_execute_query_request (
150
259
query : str ,
151
260
session_id : str ,
0 commit comments