58
58
from synapse .metrics .background_process_metrics import run_as_background_process
59
59
from synapse .storage .background_updates import BackgroundUpdater
60
60
from synapse .storage .engines import BaseDatabaseEngine , PostgresEngine , Sqlite3Engine
61
- from synapse .storage .types import Connection , Cursor
61
+ from synapse .storage .types import Connection , Cursor , SQLQueryParameters
62
62
from synapse .util .async_helpers import delay_cancellation
63
63
from synapse .util .iterutils import batch_iter
64
64
@@ -371,10 +371,18 @@ def execute_batch(self, sql: str, args: Iterable[Iterable[Any]]) -> None:
371
371
if isinstance (self .database_engine , PostgresEngine ):
372
372
from psycopg2 .extras import execute_batch
373
373
374
+ # TODO: is it safe for values to be Iterable[Iterable[Any]] here?
375
+ # https://www.psycopg.org/docs/extras.html?highlight=execute_batch#psycopg2.extras.execute_batch
376
+ # suggests each arg in args should be a sequence or mapping
374
377
self ._do_execute (
375
378
lambda the_sql : execute_batch (self .txn , the_sql , args ), sql
376
379
)
377
380
else :
381
+ # TODO: is it safe for values to be Iterable[Iterable[Any]] here?
382
+ # https://docs.python.org/3/library/sqlite3.html?highlight=sqlite3#sqlite3.Cursor.executemany
383
+ # suggests that the outer collection may be iterable, but
384
+ # https://docs.python.org/3/library/sqlite3.html?highlight=sqlite3#how-to-use-placeholders-to-bind-values-in-sql-queries
385
+ # suggests that the inner collection should be a sequence or dict.
378
386
self .executemany (sql , args )
379
387
380
388
def execute_values (
@@ -390,14 +398,20 @@ def execute_values(
390
398
from psycopg2 .extras import execute_values
391
399
392
400
return self ._do_execute (
401
+ # TODO: is it safe for values to be Iterable[Iterable[Any]] here?
402
+ # https://www.psycopg.org/docs/extras.html?highlight=execute_batch#psycopg2.extras.execute_values says values should be Sequence[Sequence]
393
403
lambda the_sql : execute_values (self .txn , the_sql , values , fetch = fetch ),
394
404
sql ,
395
405
)
396
406
397
- def execute (self , sql : str , * args : Any ) -> None :
398
- self ._do_execute (self .txn .execute , sql , * args )
407
+ def execute (self , sql : str , parameters : SQLQueryParameters = () ) -> None :
408
+ self ._do_execute (self .txn .execute , sql , parameters )
399
409
400
410
def executemany (self , sql : str , * args : Any ) -> None :
411
+ # TODO: we should add a type for *args here. Looking at Cursor.executemany
412
+ # and DBAPI2 it ought to be Sequence[_Parameter], but we pass in
413
+ # Iterable[Iterable[Any]] in execute_batch and execute_values above, which mypy
414
+ # complains about.
401
415
self ._do_execute (self .txn .executemany , sql , * args )
402
416
403
417
def executescript (self , sql : str ) -> None :
0 commit comments