build method

Uint8List build()

Builds the binary data buffer for bulk insert.

Validates that table name, columns, and at least one row are present. Returns a Uint8List containing the serialized bulk insert data.

Throws StateError if table name is empty, no columns are defined, or no rows have been added.

Implementation

Uint8List build() {
  if (_table.isEmpty) {
    throw StateError('Table name required');
  }
  if (_columns.isEmpty) {
    throw StateError('At least one column required');
  }
  if (_rows.isEmpty) {
    throw StateError('At least one row required');
  }

  final out = <int>[];
  final tableBytes = utf8.encode(_table);
  out
    ..addAll(_u32Le(tableBytes.length))
    ..addAll(tableBytes)
    ..addAll(_u32Le(_columns.length));

  for (final spec in _columns) {
    final nameBytes = utf8.encode(spec.name);
    out
      ..addAll(_u32Le(nameBytes.length))
      ..addAll(nameBytes)
      ..add(spec.tag)
      ..add(spec.nullable ? 1 : 0)
      ..addAll(_u32Le(spec.maxLen));
  }

  final rowCount = _rows.length;
  out.addAll(_u32Le(rowCount));

  for (var c = 0; c < _columns.length; c++) {
    final spec = _columns[c];
    _serializeColumn(out, spec, c, rowCount);
  }

  return Uint8List.fromList(out);
}