1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use std::i16;
use crate::io::{BufMutExt, Encode};
use crate::postgres::io::PgBufMutExt;
#[derive(Debug)]
pub struct Parse<'a> {
pub statement: u32,
pub query: &'a str,
pub param_types: &'a [u32],
}
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);
assert!(self.param_types.len() <= (u16::MAX as usize));
buf.extend(&(self.param_types.len() as i16).to_be_bytes());
for &oid in self.param_types {
buf.extend(&oid.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: 1,
query: "SELECT $1",
param_types: &[25],
};
m.encode(&mut buf);
assert_eq!(buf, EXPECTED);
}