runBatch method

Future<void> runBatch(
  1. SqlBatch batch
)

Runs every query added to batch and fills its SqlBatchResults:

final batch = SqlBatch();
final users = batch.add('SELECT id, name FROM users WHERE id = @id',
    User.fromRow, params: {'id': id});
final grants = batch.add('SELECT * FROM grants WHERE user_id = @id',
    Grant.fromRow, params: {'id': id});
await exec.runBatch(batch);
users.rows; // List<User>

Same round-trip, ordering, failure and release rules as queryBatch. The results are filled only when every query and mapper succeeded. A batch runs once: a second call throws StateError, also after a failure. An empty batch sends nothing.

Implementation

Future<void> runBatch(SqlBatch batch) async {
  batch._start();
  try {
    final slots = batch._results;
    if (slots.isEmpty) {
      batch._finish(true);
      return;
    }
    final lists = await _runQueries([for (final r in slots) r._query]);
    for (var i = 0; i < slots.length; i++) {
      slots[i]._fill(lists[i]);
    }
    batch._finish(true);
  } catch (_) {
    batch._finish(false);
    rethrow;
  }
}