| 1 | use bytes::{Buf, Bytes}; |
| 2 | |
| 3 | use crate::error::Error; |
| 4 | use crate::io::ProtocolDecode; |
| 5 | use crate::protocol::Capabilities; |
| 6 | |
| 7 | // https://dev.mysql.com/doc/internals/en/com-stmt-prepare-response.html#packet-COM_STMT_PREPARE_OK |
| 8 | |
| 9 | #[derive(Debug)] |
| 10 | pub(crate) struct PrepareOk { |
| 11 | pub(crate) statement_id: u32, |
| 12 | pub(crate) columns: u16, |
| 13 | pub(crate) params: u16, |
| 14 | #[allow(unused)] |
| 15 | pub(crate) warnings: u16, |
| 16 | } |
| 17 | |
| 18 | impl ProtocolDecode<'_, Capabilities> for PrepareOk { |
| 19 | fn decode_with(buf: Bytes, _: Capabilities) -> Result<Self, Error> { |
| 20 | const SIZE: usize = 12; |
| 21 | |
| 22 | let mut slice = buf.get(..SIZE).ok_or_else(|| { |
| 23 | err_protocol!("PrepareOk expected 12 bytes but got {} bytes", buf.len()) |
| 24 | })?; |
| 25 | |
| 26 | let status = slice.get_u8(); |
| 27 | if status != 0x00 { |
| 28 | return Err(err_protocol!( |
| 29 | "expected 0x00 (COM_STMT_PREPARE_OK) but found 0x{:02x}", |
| 30 | status |
| 31 | )); |
| 32 | } |
| 33 | |
| 34 | let statement_id = slice.get_u32_le(); |
| 35 | let columns = slice.get_u16_le(); |
| 36 | let params = slice.get_u16_le(); |
| 37 | |
| 38 | slice.advance(1); // reserved: string<1> |
| 39 | |
| 40 | let warnings = slice.get_u16_le(); |
| 41 | |
| 42 | Ok(Self { |
| 43 | statement_id, |
| 44 | columns, |
| 45 | params, |
| 46 | warnings, |
| 47 | }) |
| 48 | } |
| 49 | } |