MySQLPacketOK.decode constructor
MySQLPacketOK.decode(
- Uint8List buffer
Decodifica um buffer Uint8List recebido do servidor e retorna uma instância de MySQLPacketOK.
A decodificação segue a seguinte estrutura:
- Header: 1 byte, que deve ser 0x00.
- Affected Rows: Valor length-encoded, representando o número de linhas afetadas.
- Last Insert ID: Valor length-encoded, representando o último ID gerado.
O método utiliza a função getVariableEncInt para decodificar os valores length-encoded.
Implementation
factory MySQLPacketOK.decode(Uint8List buffer) {
if (buffer.isEmpty) {
throw const MySQLProtocolException(
'Truncated MySQLPacketOK: empty buffer',
);
}
int offset = 0;
// 1) Leitura do header (1 byte; 0x00 or 0xfe when CLIENT_DEPRECATE_EOF is set)
final header = buffer[offset];
if (header != 0x00 && header != 0xfe) {
throw MySQLProtocolException(
'Invalid MySQLPacketOK header: 0x${header.toRadixString(16)}',
);
}
offset += 1;
// 2) Leitura do número de linhas afetadas (length-encoded integer)
final affectedRows = buffer.getVariableEncIntAt(offset);
offset += affectedRows.$2;
// 3) Leitura do último ID inserido (length-encoded integer)
final lastInsertID = buffer.getVariableEncIntAt(offset);
offset += lastInsertID.$2;
// 4) Status flags (2 bytes, little-endian; CLIENT_PROTOCOL_41)
final statusFlags = buffer.length >= offset + 2
? buffer[offset] | (buffer[offset + 1] << 8)
: 0;
return MySQLPacketOK(
header: header,
affectedRows: affectedRows.$1,
lastInsertID: lastInsertID.$1,
statusFlags: statusFlags,
);
}