parse static method

List<MultiResultItem> parse(
  1. Uint8List data
)

Parses multi-result binary data into a list of items.

The data parameter must contain valid multi-result binary data. Returns a list of MultiResultItem in the order they appear.

Throws FormatException if the buffer is malformed or contains invalid data.

Implementation

static List<MultiResultItem> parse(Uint8List data) {
  if (data.length < headerSize) {
    throw const FormatException(
      'Buffer too small for multi-result header',
    );
  }

  final byteData = ByteData.sublistView(data);
  final itemCount = byteData.getUint32(0, Endian.little);

  final items = <MultiResultItem>[];
  var offset = headerSize;

  for (var i = 0; i < itemCount; i++) {
    if (offset + itemHeaderSize > data.length) {
      throw const FormatException(
        'Multi-result buffer truncated at item header',
      );
    }

    final tag = data[offset];
    offset += 1;

    if (tag != tagResultSet && tag != tagRowCount) {
      throw FormatException('Unknown multi-result item tag: $tag');
    }

    final length = byteData.getUint32(offset, Endian.little);
    offset += 4;

    if (offset + length > data.length) {
      throw const FormatException(
        'Multi-result buffer truncated at item payload',
      );
    }

    final payload = data.sublist(offset, offset + length);

    switch (tag) {
      case tagResultSet:
        final resultSet = BinaryProtocolParser.parse(payload);
        items.add(
          MultiResultItem(resultSet: resultSet, rowCount: null),
        );

      case tagRowCount:
        if (length != 8) {
          throw const FormatException(
            'RowCount item expected 8-byte payload',
          );
        }

        final rowCount = byteData.getInt64(offset, Endian.little);
        items.add(
          MultiResultItem(resultSet: null, rowCount: rowCount),
        );

      default:
        throw FormatException('Unknown multi-result item tag: $tag');
    }

    offset += length;
  }

  return items;
}