use crate::io::{BufMutExt, Encode};
use crate::postgres::io::PgBufMutExt;
use crate::postgres::types::Oid;
#[derive(Debug)]
pub struct Parse<'a> {
pub statement: Oid,
pub query: &'a str,
pub param_types: &'a [Oid],
}
impl Encode<'_> for Parse<'_> {
fn encode_with(&self, buf: &mut Vec<u8>, _: ()) {
buf.push(b'P');
buf.put_length_prefixed(|buf| {
buf.put_statement_name(self.statement);
buf.put_str_nul(self.query);
let param_len = i16::try_from(self.param_types.len()).expect("too many params");
buf.extend_from_slice(¶m_len.to_be_bytes());
for &oid in self.param_types {
buf.extend_from_slice(&oid.0.to_be_bytes());
}
})
}
}
#[test]
fn test_encode_parse() {
const EXPECTED: &[u8] = b"P\0\0\0\x1dsqlx_s_1\0SELECT $1\0\0\x01\0\0\0\x19";
let mut buf = Vec::new();
let m = Parse {
statement: Oid(1),
query: "SELECT $1",
param_types: &[Oid(25)],
};
m.encode(&mut buf);
assert_eq!(buf, EXPECTED);
}