streamQueryBatched method
Executes a SQL query and returns results as a batched stream.
Uses cursor-based batching; each batch is a complete protocol message.
fetchSize rows per batch, chunkSize buffer size in bytes.
Implementation
Stream<ParsedRowBuffer> streamQueryBatched(
int connectionId,
String sql, {
int fetchSize = 1000,
int chunkSize = 64 * 1024,
}) async* {
final streamId = _native.streamStartBatched(
connectionId,
sql,
fetchSize: fetchSize,
chunkSize: chunkSize,
);
if (streamId == 0) {
throw Exception('Failed to start batched stream: ${_native.getError()}');
}
final pending = BinaryFrameAccumulator();
try {
while (true) {
final result = _native.streamFetch(streamId);
if (!result.success) {
throw Exception('Stream fetch failed: ${_native.getError()}');
}
final data = result.data;
if (data == null || data.isEmpty) {
break;
}
pending.add(data);
for (final msg in pending.drainFrames()) {
yield BinaryProtocolParser.parse(msg);
}
if (!result.hasMore) break;
}
if (pending.length > 0) {
throw const FormatException(
'Leftover bytes after stream; expected complete protocol messages',
);
}
} finally {
_native.streamClose(streamId);
}
}