streamSql<R> method

Stream<R> streamSql<R>(
  1. SqlQuery<R> query, {
  2. int batchSize = 128,
  3. ExecutionOptions options = const ExecutionOptions(),
})

Streams typed rows through one cursor with bounded batch fetching. Compilation is immediate; a connection is acquired on listen. Result labels are bound on the first batch, even when empty, without a second query. Cancel or finish before leaving a session/transaction callback.

Implementation

Stream<R> streamSql<R>(
  SqlQuery<R> query, {
  int batchSize = 128,
  ExecutionOptions options = const ExecutionOptions(),
}) {
  if (batchSize < 1) throw ArgumentError.value(batchSize, 'batchSize');
  final command = query.sql.compile(capabilities);
  R Function(List<Object?>)? decode;
  return streamRows(
    command,
    batchSize: batchSize,
    options: options,
    decode: (_, batch, _) async {
      try {
        return observeDecode<List<R>>(command.sql, batch.rows.length, () {
          decode ??= query.result.bind(batch.columns);
          return [for (final row in batch.rows) decode!(row)];
        });
      } catch (_) {
        if (inTransaction) markFailed();
        rethrow;
      }
    },
  );
}