streamQuery method
Executes a SQL query and returns results as a stream.
The connectionId must be a valid active connection.
The sql should be a valid SQL SELECT statement.
The chunkSize specifies how many rows to fetch per chunk
(default: 1000). Results are streamed as ParsedRowBuffer instances,
allowing efficient processing of large result sets without loading
everything into memory.
Example:
await for (final chunk in native.streamQuery(
connId,
'SELECT * FROM users',
)) {
// Process chunk
}
Implementation
Stream<ParsedRowBuffer> streamQuery(
int connectionId,
String sql, {
int chunkSize = 1000,
}) 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);
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;
}
buffer.add(data);
if (!result.hasMore) {
break;
}
}
if (buffer.length > 0) {
final parsed = BinaryProtocolParser.parse(buffer.toBytes());
yield parsed;
}
} finally {
_native.streamClose(streamId);
}
}