openBlob method

Future<Stream<ByteBuffer>> openBlob({
  1. required FbBlobId id,
  2. int segmentSize = 4096,
  3. FbTransaction? inTransaction,
})

Opens an existing blob for reading.

For a given FbBlobId, opens the blob in the database and returns a stream of byte buffers, which allows to read the binary data from the blob. The segmentSize determines the maximum size of the buffer a single read from the stream can yield.

Example:

final db = await FbDb.attach(host: "localhost", database: "employee");
await db.startTransaction(); // remember to start an explicit transaction
final q = db.query();
// suppose TEST_TABLE.BLOB_COL contains blobs
await q.openCursor(
  sql: "SELECT BLOB_COL from TEST_TABLE",
  inlineBlobls: false, // get blob ID instead of a buffer
);
final row = await q.fetchOneAsMap();
final blobId = row["BLOB_COL"];
final blobStream = await db.openBlob(id: blobId);
await for (var segment in blobStream) {
  // process the BLOB data segment
}
await db.commit(); // commit the started transaction
await db.detach();

Implementation

Future<Stream<ByteBuffer>> openBlob({
  required FbBlobId id,
  int segmentSize = 4096,
  FbTransaction? inTransaction,
}) async {
  if (segmentSize <= 0) {
    throw FbClientException("Invalid blob segment size: $segmentSize");
  }
  await _askWorker(FbDbControlOp.openBlob, [
    id,
    if (inTransaction != null) inTransaction.handle,
  ]);
  return () async* {
    for (;;) {
      final r = await _askWorker(FbDbControlOp.getBlobSegment, [
        id,
        segmentSize,
      ]);
      if (r.data.isNotEmpty && r.data[0] != null) {
        ByteBuffer buf = r.data[0];
        yield buf;
      } else {
        break;
      }
    }
  }();
}