queryMany method
Execute sql query with each list of positional parameters from params.
Parameters may be referenced in the sql query using $1, $2, ...
Returns a stream of rows, where each row is a list columns. If there are no data to be returned, this yields an empty stream.
The following types can be passed in and out.
Throws DatabaseException, if the query fails.
Implementation
Stream<RowReader> queryMany(
String sql,
Iterable<List<Object?>> params,
) {
final c = StreamController<RowReader>();
c.onListen = () async {
try {
// Await the entire transaction block
await transact((tx) async {
for (final p in params) {
if (c.isClosed) {
break;
}
await c.addStream(tx.query(sql, p), cancelOnError: true);
}
});
} catch (error, stackTrace) {
// If the transaction aborts (or addStream throws), pipe it out
if (!c.isClosed) {
c.addError(error, stackTrace);
}
} finally {
// Guarantee the stream closes cleanly, whether success or failure
if (!c.isClosed) {
await c.close();
}
}
};
return c.stream;
}