runInTransaction method

  1. @override
Future<RunResult> runInTransaction(
  1. List<Statement> statements
)
override

Implementation

@override
Future<RunResult> runInTransaction(List<Statement> statements) async {
  return txLock.synchronized(() async {
    try {
      // SQL-js accepts multiple statements in a string and does
      // not run them as transaction.
      db.execute('BEGIN');

      int rowsAffected = 0;
      for (final statement in statements) {
        db.execute(statement.sql, statement.args ?? []);
        rowsAffected += db.updatedRows;
      }

      db.execute('COMMIT');

      return RunResult(rowsAffected: rowsAffected);
    } catch (error) {
      db.execute('ROLLBACK');

      rethrow; // rejects the promise with the reason for the rollback
    }
  });
}