MySQLResultSetRowPacket.decode constructor

MySQLResultSetRowPacket.decode(
  1. Uint8List buffer,
  2. List<MySQLColumnDefinitionPacket> columns, {
  3. List<bool>? binaryColumns,
})

Decodifica um Uint8List recebido do servidor como uma linha de resultado, considerando que estamos no protocolo textual. Para cada coluna:

  • Se for 0xFB, o valor é NULL;
  • Caso contrário, lê-se o campo como length-encoded data.
    • Se a coluna for BLOB/BINÁRIO, mantém como Uint8List;
    • Senão, converte para String.

Implementation

factory MySQLResultSetRowPacket.decode(
    Uint8List buffer, List<MySQLColumnDefinitionPacket> columns,
    {List<bool>? binaryColumns}) {
  var offset = 0;
  final limit = buffer.length;
  final values = List<dynamic>.filled(columns.length, null, growable: false);

  for (var x = 0; x < columns.length; x++) {
    final slice = readLengthEncodedSlice(
      buffer,
      offset,
      limit,
      allowNullMarker: true,
    );
    offset = slice.nextOffset;
    final fieldBytes = slice.bytes;
    if (fieldBytes == null) {
      continue;
    }

    final isBinary = (binaryColumns != null && x < binaryColumns.length)
        ? binaryColumns[x]
        : columnShouldBeBinary(columns[x]);
    if (isBinary) {
      // Se for BLOB/binário, guardamos como bytes; caso contrário, convertemos p/ String
      values[x] = fieldBytes; // Uint8List
    } else {
      // MySQL always sends textual protocol data using the negotiated connection charset (utf8mb4 by default),
      // so decode as UTF-8 to keep accents and emojis intact.
      values[x] = utf8.decode(fieldBytes, allowMalformed: true);
    }
  }

  return MySQLResultSetRowPacket(values: values);
}