MySQLColumnDefinitionPacket.decode constructor

MySQLColumnDefinitionPacket.decode(
  1. Uint8List buffer
)

Decodifica um Uint8List recebido do servidor MySQL e constrói um objeto MySQLColumnDefinitionPacket.

A sequência de leitura segue a especificação do protocolo MySQL:

  1. catalog (length-encoded string)
  2. schema (length-encoded string)
  3. table (length-encoded string)
  4. org_table (length-encoded string)
  5. name (length-encoded string)
  6. org_name (length-encoded string)
  7. lengthOfFixedLengthFields (length-encoded integer; geralmente 0x0c)
  8. charset (2 bytes, little-endian)
  9. column_length (4 bytes, little-endian)
  10. type (1 byte)
  11. flags (2 bytes, little-endian)
  12. decimals (1 byte)
  13. filler (2 bytes, geralmente 0x00 0x00)

Ao final, retorna uma instância de MySQLColumnDefinitionPacket.

Implementation

factory MySQLColumnDefinitionPacket.decode(Uint8List buffer) {
  final byteData = ByteData.sublistView(buffer);
  int offset = 0;

  // 1) Lê catalog (length-encoded string)
  final catalogLE = _readMetadataString(buffer, offset);
  offset += catalogLE.$2;

  // 2) Lê schema (length-encoded string)
  final schemaLE = _readMetadataString(buffer, offset);
  offset += schemaLE.$2;

  // 3) Lê table (length-encoded string)
  final tableLE = _readMetadataString(buffer, offset);
  offset += tableLE.$2;

  // 4) Lê org_table (length-encoded string)
  final orgTableLE = _readMetadataString(buffer, offset);
  offset += orgTableLE.$2;

  // 5) Lê name (length-encoded string)
  final nameLE = _readMetadataString(buffer, offset);
  offset += nameLE.$2;

  // 6) Lê org_name (length-encoded string)
  final orgNameLE = _readMetadataString(buffer, offset);
  offset += orgNameLE.$2;

  // 7) Lê lengthOfFixedLengthFields (geralmente 0x0c)
  final lengthOfFixedLengthFields = buffer.getVariableEncIntAt(offset);
  if (lengthOfFixedLengthFields.$1 < BigInt.from(0x0c)) {
    throw MySQLProtocolException(
      'Invalid fixed-fields length in MySQLColumnDefinitionPacket: '
      '${lengthOfFixedLengthFields.$1}',
    );
  }
  offset += lengthOfFixedLengthFields.$2;

  if (BigInt.from(buffer.length - offset) < lengthOfFixedLengthFields.$1) {
    throw MySQLProtocolException(
      'Truncated fixed-length fields in MySQLColumnDefinitionPacket: '
      'declared ${lengthOfFixedLengthFields.$1} bytes, '
      'available ${buffer.length - offset}',
    );
  }

  // 8) Lê charset (2 bytes, little-endian)
  final charset = byteData.getUint16(offset, Endian.little);
  offset += 2;

  // 9) Lê column_length (4 bytes, little-endian)
  final columnLength = byteData.getUint32(offset, Endian.little);
  offset += 4;

  // 10) Lê type (1 byte)
  final colType = byteData.getUint8(offset);
  offset += 1;

  // 11) Lê flags (2 bytes, little-endian)
  final flags = byteData.getUint16(offset, Endian.little);
  offset += 2;

  // 12) Lê o número de decimais (1 byte)
  final decimals = byteData.getUint8(offset);
  offset += 1;

  // 13) Pula 2 bytes de filler (geralmente 0x00 0x00)
  offset += 2;

  // Retorna a instância
  return MySQLColumnDefinitionPacket(
    catalog: catalogLE.$1,
    schema: schemaLE.$1,
    table: tableLE.$1,
    orgTable: orgTableLE.$1,
    name: nameLE.$1,
    orgName: orgNameLE.$1,
    charset: charset,
    columnLength: columnLength,
    type: MySQLColumnType.create(colType),
    flags: flags,
    decimals: decimals,
  );
}