@@ -295,58 +295,42 @@ def warnings(self):
295
295
return self ._query .warnings
296
296
return None
297
297
298
+ def _new_request_with_session_from (self , request ):
299
+ """
300
+ Returns a new request with the `ClientSession` set to the one from the
301
+ given request.
302
+ """
303
+ request = self .connection ._create_request ()
304
+ request ._client_session = request ._client_session
305
+ return request
306
+
298
307
def setinputsizes (self , sizes ):
299
308
raise trino .exceptions .NotSupportedError
300
309
301
310
def setoutputsize (self , size , column ):
302
311
raise trino .exceptions .NotSupportedError
303
312
304
- def _prepare_statement (self , operation , statement_name ):
313
+ def _prepare_statement (self , statement , name ):
305
314
"""
306
- Prepends the given `operation` with "PREPARE <statement_name> FROM" and
307
- executes as a prepare statement.
308
-
309
- :param operation: sql to be executed.
310
- :param statement_name: name that will be assigned to the prepare
311
- statement.
312
-
313
- :raises trino.exceptions.FailedToObtainAddedPrepareHeader: Error raised
314
- when unable to find the 'X-Trino-Added-Prepare' for the PREPARE
315
- statement request.
315
+ Registers a prepared statement for the provided `operation` with the
316
+ `name` assigned to it.
316
317
317
- :return: string representing the value of the 'X-Trino-Added-Prepare'
318
- header .
318
+ :param statement: sql to be executed.
319
+ :param name: name that will be assigned to the prepared statement .
319
320
"""
320
- sql = 'PREPARE {statement_name} FROM {operation}' .format (
321
- statement_name = statement_name ,
322
- operation = operation
323
- )
324
-
325
- # Send prepare statement. Copy the _request object to avoid polluting the
326
- # one that is going to be used to execute the actual operation.
327
- query = trino .client .TrinoQuery (copy .deepcopy (self ._request ), sql = sql ,
321
+ sql = f"PREPARE { name } FROM { statement } "
322
+ # TODO: Evaluate whether we can avoid the piggybacking on current request
323
+ query = trino .client .TrinoQuery (self ._new_request_with_session_from (self ._request ), sql = sql ,
328
324
experimental_python_types = self ._experimental_pyton_types )
329
- result = query .execute ()
330
-
331
- # Iterate until the 'X-Trino-Added-Prepare' header is found or
332
- # until there are no more results
333
- for _ in result :
334
- response_headers = result .response_headers
335
-
336
- if constants .HEADER_ADDED_PREPARE in response_headers :
337
- return response_headers [constants .HEADER_ADDED_PREPARE ]
325
+ query .execute ()
338
326
339
- raise trino .exceptions .FailedToObtainAddedPrepareHeader
340
327
341
- def _get_added_prepare_statement_trino_query (
328
+ def _execute_prepared_statement (
342
329
self ,
343
330
statement_name ,
344
331
params
345
332
):
346
333
sql = 'EXECUTE ' + statement_name + ' USING ' + ',' .join (map (self ._format_prepared_param , params ))
347
-
348
- # No need to deepcopy _request here because this is the actual request
349
- # operation
350
334
return trino .client .TrinoQuery (self ._request , sql = sql , experimental_python_types = self ._experimental_pyton_types )
351
335
352
336
def _format_prepared_param (self , param ):
@@ -422,28 +406,12 @@ def _format_prepared_param(self, param):
422
406
423
407
raise trino .exceptions .NotSupportedError ("Query parameter of type '%s' is not supported." % type (param ))
424
408
425
- def _deallocate_prepare_statement (self , added_prepare_header , statement_name ):
409
+ def _deallocate_prepared_statement (self , statement_name ):
426
410
sql = 'DEALLOCATE PREPARE ' + statement_name
427
-
428
- # Send deallocate statement. Copy the _request object to avoid poluting the
429
- # one that is going to be used to execute the actual operation.
430
- query = trino .client .TrinoQuery (copy .deepcopy (self ._request ), sql = sql ,
411
+ # TODO: Evaluate whether we can avoid the piggybacking on current request
412
+ query = trino .client .TrinoQuery (self ._new_request_with_session_from (self ._request ), sql = sql ,
431
413
experimental_python_types = self ._experimental_pyton_types )
432
- result = query .execute (
433
- additional_http_headers = {
434
- constants .HEADER_PREPARED_STATEMENT : added_prepare_header
435
- }
436
- )
437
-
438
- # Iterate until the 'X-Trino-Deallocated-Prepare' header is found or
439
- # until there are no more results
440
- for _ in result :
441
- response_headers = result .response_headers
442
-
443
- if constants .HEADER_DEALLOCATED_PREPARE in response_headers :
444
- return response_headers [constants .HEADER_DEALLOCATED_PREPARE ]
445
-
446
- raise trino .exceptions .FailedToObtainDeallocatedPrepareHeader
414
+ query .execute ()
447
415
448
416
def _generate_unique_statement_name (self ):
449
417
return 'st_' + uuid .uuid4 ().hex .replace ('-' , '' )
@@ -456,27 +424,21 @@ def execute(self, operation, params=None):
456
424
)
457
425
458
426
statement_name = self ._generate_unique_statement_name ()
459
- # Send prepare statement
460
- added_prepare_header = self ._prepare_statement (
461
- operation , statement_name
462
- )
427
+ self ._prepare_statement (operation , statement_name )
463
428
464
429
try :
465
430
# Send execute statement and assign the return value to `results`
466
431
# as it will be returned by the function
467
- self ._query = self ._get_added_prepare_statement_trino_query (
432
+ self ._query = self ._execute_prepared_statement (
468
433
statement_name , params
469
434
)
470
- result = self ._query .execute (
471
- additional_http_headers = {
472
- constants .HEADER_PREPARED_STATEMENT : added_prepare_header
473
- }
474
- )
435
+ result = self ._query .execute ()
475
436
finally :
476
437
# Send deallocate statement
477
438
# At this point the query can be deallocated since it has already
478
439
# been executed
479
- self ._deallocate_prepare_statement (added_prepare_header , statement_name )
440
+ # TODO: Consider caching prepared statements if requested by caller
441
+ self ._deallocate_prepared_statement (statement_name )
480
442
481
443
else :
482
444
self ._query = trino .client .TrinoQuery (self ._request , sql = operation ,
0 commit comments