drift icon indicating copy to clipboard operation
drift copied to clipboard

Refactor `QueryExecutor` to reliably indicate which methods write

Open simolus3 opened this issue 1 year ago • 0 comments

Currently, QueryExecutor consists of the methods runSelect, runCustom, runInsert, runUpdate and runDelete to run statements. Across implementations, there is no difference between runUpdate and runDelete. The only difference between them and runInsert and runCustom is that some return the amount of affected rows while others return the last insert rowid or simply nothing.

Some query executors, e.g. MultiExecutor which delegates writes to a single executor and reads to a pool of other executors, need to know whether an invocation writes to the database. But since the introduction of RETURNING, runSelect is also used for some writes where we care about affected row contents.

To reduce the amount of methods, as well as being more reliable about which methods potentially write to the database, a new interface is needed. It could consist of the following two methods which enable the same functionality as the five methods today:

  1. Future<List<...>> runReturningRows(String sql, {List<Object?> variables = const [], bool isWrite = false}).
  2. Future<({int? lastInsertRowId, int? affectedRows})> run(String sql, {List<Object?> variables = const [], bool isWrite = true}).

simolus3 avatar Jul 23 '24 12:07 simolus3