queryMulti method Null safety

Future<List<Results>> queryMulti(
  1. String sql,
  2. Iterable<List<Object?>> values
)

Run sql query multiple times for each set of positional sql parameters in values.

e.g. queryMulti('INSERT INTO USERS (name) VALUES (?)', ['Adam', 'Eve']).

Implementation

Future<List<Results>> queryMulti(String sql, Iterable<List<Object?>> values) async {
  PreparedQuery? prepared;
  var ret = <Results>[];
  try {
    prepared = await _conn.processHandler<PreparedQuery>(PrepareHandler(sql), _timeout);
    _log.fine('Prepared queryMulti query for: $sql');

    for (final v in values) {
      if (v.length != prepared.parameterCount) {
        throw MySqlClientError(
            'Length of parameters (${v.length}) does not match parameter count in query (${prepared.parameterCount})');
      }
      var handler = ExecuteQueryHandler(prepared, false /* executed */, v);
      ret.add(await _conn.processHandlerWithResults(handler, _timeout));
    }
  } finally {
    if (prepared != null) {
      await _conn.processHandlerNoResponse(CloseStatementHandler(prepared.statementHandlerId), _timeout);
    }
  }
  return ret;
}