@@ -264,6 +264,24 @@ def execute_query_take_single(logger: logging.Logger, rai_config: RaiConfig, env
264
264
return rsp .results [0 ]['table' ].to_pydict ()["v1" ][0 ]
265
265
266
266
267
+ def execute_query_take_tuples (logger : logging .Logger , rai_config : RaiConfig , env_config : EnvConfig , query : str ,
268
+ readonly : bool = True , ignore_problems : bool = False ) -> any :
269
+ """
270
+ Execute query and take the results as an array of tuples.
271
+ :param logger: logger
272
+ :param rai_config: RAI config
273
+ :param env_config: Env config
274
+ :param query: Rel query
275
+ :param readonly: Parameter to specify transaction type: Read/Write
276
+ :param ignore_problems: Ignore SDK problems if any
277
+ """
278
+ rsp = execute_query (logger , rai_config , env_config , query , readonly = readonly , ignore_problems = ignore_problems )
279
+ if not rsp .results :
280
+ logger .debug (f"Query returned no results: { query } " )
281
+ return {}
282
+ return _parse_as_dict (rsp )
283
+
284
+
267
285
def list_transactions (logger : logging .Logger , rai_config : RaiConfig ) -> List :
268
286
"""
269
287
List all transactions for the engine
@@ -322,13 +340,29 @@ def _parse_csv_string(rsp: api.TransactionAsyncResponse) -> Dict:
322
340
Parse the output for a csv_string query ie
323
341
def output:{rel_name} = csv_string[...]
324
342
"""
325
- resp = {}
326
343
rel_pattern = r'^/:output/:(.*)/String$'
344
+ return _parse_matching_pattern (rsp , rel_pattern )
345
+
346
+
347
+ def _parse_as_dict (rsp : api .TransactionAsyncResponse ) -> Dict :
348
+ """
349
+ Parse the output as dictionary, everything starting with :output
350
+ """
351
+ rel_pattern = r'^/:output/(.*)$'
352
+ return _parse_matching_pattern (rsp , rel_pattern )
353
+
354
+
355
+ def _parse_matching_pattern (rsp : api .TransactionAsyncResponse , pattern : str ) -> Dict :
356
+ resp = {}
327
357
for result in rsp .results :
328
- match = re .search (rel_pattern , result ['relationId' ])
358
+ match = re .search (pattern , result ['relationId' ])
329
359
if match :
330
360
relation_id = match .group (1 )
331
- data = result ['table' ].to_pydict ()["v1" ][0 ]
361
+ result_dict = result ['table' ].to_pydict ()
362
+ if "v1" in result_dict :
363
+ data = result_dict ["v1" ][0 ]
364
+ else :
365
+ data = True # if only relation label present, the value is True
332
366
resp [relation_id ] = data
333
367
return resp
334
368
0 commit comments