MySQLPacketStmtPrepareOK.decode constructor

MySQLPacketStmtPrepareOK.decode(
  1. Uint8List buffer
)

Decodifica um buffer Uint8List recebido do servidor e retorna uma instância de MySQLPacketStmtPrepareOK.

A estrutura do pacote segue a especificação do protocolo MySQL para COM_STMT_PREPARE:

  1. Header: 1 byte (geralmente 0x00).
  2. Statement ID (stmtID): 4 bytes (little-endian).
  3. Number of columns: 2 bytes (little-endian).
  4. Number of parameters: 2 bytes (little-endian).
  5. Filler: 1 byte (valor ignorado).
  6. Number of warnings: 2 bytes (little-endian).

Implementation

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

  // 1) Leitura do header (1 byte)
  final header = byteData.getUint8(offset);
  offset += 1;

  // 2) Leitura do Statement ID (4 bytes, little-endian)
  final statementID = byteData.getUint32(offset, Endian.little);
  offset += 4;

  // 3) Leitura do número de colunas (2 bytes, little-endian)
  final numColumns = byteData.getUint16(offset, Endian.little);
  offset += 2;

  // 4) Leitura do número de parâmetros (2 bytes, little-endian)
  final numParams = byteData.getUint16(offset, Endian.little);
  offset += 2;

  // 5) Pula 1 byte de filler (não utilizado)
  offset += 1;

  // 6) Leitura do número de warnings (2 bytes, little-endian)
  final numWarnings = byteData.getUint16(offset, Endian.little);
  offset += 2;

  return MySQLPacketStmtPrepareOK(
    header: header,
    stmtID: statementID,
    numOfCols: numColumns,
    numOfParams: numParams,
    numOfWarnings: numWarnings,
  );
}