streamQueryBuffer method

Stream<ParsedRowBuffer> streamQueryBuffer(
  1. int connectionId,
  2. String sql, {
  3. int chunkSize = 1000,
  4. bool lazyStrings = false,
})
inherited

Legacy buffer-mode streaming via odbc_stream_start. Materialises the full result in native memory before yielding a single parsed chunk. Prefer streamQuery or streamQueryBatched for large result sets.

Implementation

Stream<ParsedRowBuffer> streamQueryBuffer(
  int connectionId,
  String sql, {
  int chunkSize = 1000,
  bool lazyStrings = false,
}) async* {
  final streamId =
      _native.streamStart(connectionId, sql, chunkSize: chunkSize);

  if (streamId == 0) {
    throw Exception('Failed to start stream: ${_native.getError()}');
  }

  final buffer = BytesBuilder(copy: false);
  var completed = false;
  OdbcError? primary;
  try {
    while (true) {
      final result = streamFetch(streamId);

      if (!result.success) {
        throw QueryError(
          message: 'Stream fetch failed: ${_native.getError()}',
        );
      }

      final data = result.data;
      if (data == null || data.isEmpty) {
        break;
      }
      buffer.add(data);

      if (!result.hasMore) {
        break;
      }
    }
    if (buffer.length > 0) {
      final parsed = decodeBatchedStreamFrame(
        buffer.toBytes(),
        lazyStrings: lazyStrings,
      );
      yield parsed;
    }
    completed = true;
  } on Object catch (error, stack) {
    primary = translateOdbcError(
      error,
      operation: 'streamQuery',
      stackTrace: stack,
    );
  } finally {
    final failure = await cleanupNativeResources(
      [
        if (!completed)
          (
            operation: 'streamCancel',
            action: () async => streamCancel(streamId)
          ),
        (operation: 'streamClose', action: () async => streamClose(streamId)),
      ],
      primary: primary,
    );
    if (failure != null) {
      Error.throwWithStackTrace(
        failure,
        failure.details.stackTrace ?? StackTrace.current,
      );
    }
  }
}