streamFetch method

StreamFetchResult streamFetch(
  1. int streamId
)

Fetches the next chunk of data from a streaming query.

The streamId must be a valid stream identifier from streamStart.

Returns a StreamFetchResult with success status, data, and hasMore flag.

Implementation

StreamFetchResult streamFetch(int streamId) {
  var size = initialBufferSize;
  const maxSize = maxBufferSize;
  while (size <= maxSize) {
    final buf = malloc<ffi.Uint8>(size);
    final outWritten = malloc<ffi.Uint32>();
    final hasMore = malloc<ffi.Uint8>();
    outWritten.value = 0;
    hasMore.value = 0;
    try {
      final code = _bindings.odbc_stream_fetch(
        streamId,
        buf,
        size,
        outWritten,
        hasMore,
      );
      if (code == 0) {
        final n = outWritten.value;
        final data = n > 0 ? Uint8List.fromList(buf.asTypedList(n)) : null;
        final more = hasMore.value != 0;
        return StreamFetchResult(
          success: true,
          data: data?.toList(),
          hasMore: more,
        );
      }
      if (code == -2) {
        size *= 2;
        continue;
      }
      return StreamFetchResult(
        success: false,
        data: null,
        hasMore: false,
      );
    } finally {
      malloc
        ..free(buf)
        ..free(outWritten)
        ..free(hasMore);
    }
  }
  return StreamFetchResult(
    success: false,
    data: null,
    hasMore: false,
  );
}