| 1 | use smallvec::SmallVec; |
| 2 | use sqlx_core::bytes::{Buf, Bytes}; |
| 3 | |
| 4 | use crate::error::Error; |
| 5 | use crate::message::{BackendMessage, BackendMessageFormat}; |
| 6 | use crate::types::Oid; |
| 7 | |
| 8 | #[derive(Debug)] |
| 9 | pub struct ParameterDescription { |
| 10 | pub types: SmallVec<[Oid; 6]>, |
| 11 | } |
| 12 | |
| 13 | impl BackendMessage for ParameterDescription { |
| 14 | const FORMAT: BackendMessageFormat = BackendMessageFormat::ParameterDescription; |
| 15 | |
| 16 | fn decode_body(mut buf: Bytes) -> Result<Self, Error> { |
| 17 | // Note: this is correct, max parameters is 65535, not 32767 |
| 18 | // https://github.com/launchbadge/sqlx/issues/3464 |
| 19 | let cnt = buf.get_u16(); |
| 20 | let mut types = SmallVec::with_capacity(cnt as usize); |
| 21 | |
| 22 | for _ in 0..cnt { |
| 23 | types.push(Oid(buf.get_u32())); |
| 24 | } |
| 25 | |
| 26 | Ok(Self { types }) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | #[test] |
| 31 | fn test_decode_parameter_description() { |
| 32 | const DATA: &[u8] = b"\x00\x02\x00\x00\x00\x00\x00\x00\x05\x00"; |
| 33 | |
| 34 | let m = ParameterDescription::decode_body(DATA.into()).unwrap(); |
| 35 | |
| 36 | assert_eq!(m.types.len(), 2); |
| 37 | assert_eq!(m.types[0], Oid(0x0000_0000)); |
| 38 | assert_eq!(m.types[1], Oid(0x0000_0500)); |
| 39 | } |
| 40 | |
| 41 | #[test] |
| 42 | fn test_decode_empty_parameter_description() { |
| 43 | const DATA: &[u8] = b"\x00\x00"; |
| 44 | |
| 45 | let m = ParameterDescription::decode_body(DATA.into()).unwrap(); |
| 46 | |
| 47 | assert!(m.types.is_empty()); |
| 48 | } |
| 49 | |
| 50 | #[cfg(all(test, not(debug_assertions)))] |
| 51 | #[bench] |
| 52 | fn bench_decode_parameter_description(b: &mut test::Bencher) { |
| 53 | const DATA: &[u8] = b"\x00\x02\x00\x00\x00\x00\x00\x00\x05\x00"; |
| 54 | |
| 55 | b.iter(|| { |
| 56 | ParameterDescription::decode_body(test::black_box(Bytes::from_static(DATA))).unwrap(); |
| 57 | }); |
| 58 | } |