| 1 | use bytes::{Buf, Bytes}; |
| 2 | |
| 3 | use crate::error::Error; |
| 4 | use crate::io::MySqlBufExt; |
| 5 | use crate::io::ProtocolDecode; |
| 6 | use crate::protocol::response::Status; |
| 7 | |
| 8 | /// Indicates successful completion of a previous command sent by the client. |
| 9 | #[derive(Debug)] |
| 10 | pub struct OkPacket { |
| 11 | pub affected_rows: u64, |
| 12 | pub last_insert_id: u64, |
| 13 | pub status: Status, |
| 14 | pub warnings: u16, |
| 15 | } |
| 16 | |
| 17 | impl ProtocolDecode<'_> for OkPacket { |
| 18 | fn decode_with(mut buf: Bytes, _: ()) -> Result<Self, Error> { |
| 19 | let header = buf.get_u8(); |
| 20 | if header != 0 && header != 0xfe { |
| 21 | return Err(err_protocol!( |
| 22 | "expected 0x00 or 0xfe (OK_Packet) but found 0x{:02x}", |
| 23 | header |
| 24 | )); |
| 25 | } |
| 26 | |
| 27 | let affected_rows = buf.get_uint_lenenc(); |
| 28 | let last_insert_id = buf.get_uint_lenenc(); |
| 29 | let status = Status::from_bits_truncate(buf.get_u16_le()); |
| 30 | let warnings = buf.get_u16_le(); |
| 31 | |
| 32 | Ok(Self { |
| 33 | affected_rows, |
| 34 | last_insert_id, |
| 35 | status, |
| 36 | warnings, |
| 37 | }) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | #[test] |
| 42 | fn test_decode_ok_packet() { |
| 43 | const DATA: &[u8] = b"\x00\x00\x00\x02@\x00\x00"; |
| 44 | |
| 45 | let p = OkPacket::decode(DATA.into()).unwrap(); |
| 46 | |
| 47 | assert_eq!(p.affected_rows, 0); |
| 48 | assert_eq!(p.last_insert_id, 0); |
| 49 | assert_eq!(p.warnings, 0); |
| 50 | assert!(p.status.contains(Status::SERVER_STATUS_AUTOCOMMIT)); |
| 51 | assert!(p.status.contains(Status::SERVER_SESSION_STATE_CHANGED)); |
| 52 | } |