1616 Optional ,
1717 Sequence ,
1818 Tuple ,
19+ Union ,
1920)
2021
2122from aiorwlock import RWLock
@@ -289,6 +290,7 @@ async def _do_execute(
289290 raw_query : str ,
290291 parameters : Sequence [Sequence [ParameterType ]],
291292 set_parameters : Optional [Dict ] = None ,
293+ skip_parsing : bool = False ,
292294 ) -> None :
293295 self ._reset ()
294296 if set_parameters is not None :
@@ -298,7 +300,16 @@ async def _do_execute(
298300 )
299301 try :
300302
301- queries = split_format_sql (raw_query , parameters )
303+ if parameters and skip_parsing :
304+ logger .warning (
305+ "Query formatting parameters are provided with skip_parsing."
306+ " They will be ignored"
307+ )
308+
309+ # Allow users to manually skip parsing for performance improvement
310+ queries : List [Union [SetParameter , str ]] = (
311+ [raw_query ] if skip_parsing else split_format_sql (raw_query , parameters )
312+ )
302313
303314 for query in queries :
304315
@@ -351,20 +362,70 @@ async def execute(
351362 query : str ,
352363 parameters : Optional [Sequence [ParameterType ]] = None ,
353364 set_parameters : Optional [Dict ] = None ,
365+ skip_parsing : bool = False ,
354366 ) -> int :
355- """Prepare and execute a database query. Return row count."""
356-
367+ """Prepare and execute a database query.
368+
369+ Supported features:
370+ Parameterized queries: placeholder characters ('?') are substituted
371+ with values provided in `parameters`. Values are formatted to
372+ be properly recognized by database and to exclude SQL injection.
373+ Multi-statement queries: multiple statements, provided in a single query
374+ and separated by semicolon are executed separatelly and sequentially.
375+ To switch to next statement result, `nextset` method should be used.
376+ SET statements: to provide additional query execution parameters, execute
377+ `SET param=value` statement before it. All parameters are stored in
378+ cursor object until it's closed. They can also be removed with
379+ `flush_parameters` method call.
380+
381+ Args:
382+ query (str): SQL query to execute
383+ parameters (Optional[Sequence[ParameterType]]): A sequence of substitution
384+ parameters. Used to replace '?' placeholders inside a query with
385+ actual values
386+ set_parameters (Optional[Dict]): List of set parameters to execute
387+ a query with. DEPRECATED: Use SET SQL statements instead
388+ skip_parsing (bool): Flag to disable query parsing. This will
389+ disable parameterized, multi-statement and SET queries,
390+ while improving performance
391+
392+ Returns:
393+ int: Query row count
394+ """
357395 params_list = [parameters ] if parameters else []
358- await self ._do_execute (query , params_list , set_parameters )
396+ await self ._do_execute (query , params_list , set_parameters , skip_parsing )
359397 return self .rowcount
360398
361399 @check_not_closed
362400 async def executemany (
363401 self , query : str , parameters_seq : Sequence [Sequence [ParameterType ]]
364402 ) -> int :
365- """
366- Prepare and execute a database query against all parameter
367- sequences provided. Return last query row count.
403+ """Prepare and execute a database query.
404+
405+ Supports providing multiple substitution parameter sets, executing them
406+ as multiple statements sequentially.
407+
408+ Supported features:
409+ Parameterized queries: placeholder characters ('?') are substituted
410+ with values provided in `parameters`. Values are formatted to
411+ be properly recognized by database and to exclude SQL injection.
412+ Multi-statement queries: multiple statements, provided in a single query
413+ and separated by semicolon are executed separatelly and sequentially.
414+ To switch to next statement result, `nextset` method should be used.
415+ SET statements: to provide additional query execution parameters, execute
416+ `SET param=value` statement before it. All parameters are stored in
417+ cursor object until it's closed. They can also be removed with
418+ `flush_parameters` method call.
419+
420+ Args:
421+ query (str): SQL query to execute
422+ parameters_seq (Sequence[Sequence[ParameterType]]): A sequence of
423+ substitution parameter sets. Used to replace '?' placeholders inside a
424+ query with actual values from each set in a sequence. Resulting queries
425+ for each subset are executed sequentially.
426+
427+ Returns:
428+ int: Query row count
368429 """
369430 await self ._do_execute (query , parameters_seq )
370431 return self .rowcount
@@ -472,10 +533,12 @@ async def execute(
472533 query : str ,
473534 parameters : Optional [Sequence [ParameterType ]] = None ,
474535 set_parameters : Optional [Dict ] = None ,
536+ skip_parsing : bool = False ,
475537 ) -> int :
476538 async with self ._async_query_lock .writer :
477- return await super ().execute (query , parameters , set_parameters )
478- """Prepare and execute a database query"""
539+ return await super ().execute (
540+ query , parameters , set_parameters , skip_parsing
541+ )
479542
480543 @wraps (BaseCursor .executemany )
481544 async def executemany (
0 commit comments