sqlx_core_oldapi/postgres/message/bind.rs
1use crate::io::Encode;
2use crate::postgres::io::PgBufMutExt;
3use crate::postgres::types::Oid;
4use crate::postgres::PgValueFormat;
5
6#[derive(Debug)]
7pub struct Bind<'a> {
8 /// The ID of the destination portal (`None` selects the unnamed portal).
9 pub portal: Option<Oid>,
10
11 /// The id of the source prepared statement.
12 pub statement: Oid,
13
14 /// The parameter format codes. Each must presently be zero (text) or one (binary).
15 ///
16 /// There can be zero to indicate that there are no parameters or that the parameters all use the
17 /// default format (text); or one, in which case the specified format code is applied to all
18 /// parameters; or it can equal the actual number of parameters.
19 pub formats: &'a [PgValueFormat],
20
21 /// The number of parameters.
22 pub num_params: i16,
23
24 /// The value of each parameter, in the indicated format.
25 pub params: &'a [u8],
26
27 /// The result-column format codes. Each must presently be zero (text) or one (binary).
28 ///
29 /// There can be zero to indicate that there are no result columns or that the
30 /// result columns should all use the default format (text); or one, in which
31 /// case the specified format code is applied to all result columns (if any);
32 /// or it can equal the actual number of result columns of the query.
33 pub result_formats: &'a [PgValueFormat],
34}
35
36impl Encode<'_> for Bind<'_> {
37 fn encode_with(&self, buf: &mut Vec<u8>, _: ()) {
38 buf.push(b'B');
39
40 buf.put_length_prefixed(|buf| {
41 buf.put_portal_name(self.portal);
42
43 buf.put_statement_name(self.statement);
44
45 buf.extend(&(self.formats.len() as i16).to_be_bytes());
46
47 for &format in self.formats {
48 buf.extend(&(format as i16).to_be_bytes());
49 }
50
51 buf.extend(&self.num_params.to_be_bytes());
52
53 buf.extend(self.params);
54
55 if let Ok(len) = i16::try_from(self.result_formats.len()) {
56 buf.extend(&len.to_be_bytes());
57 for &format in self.result_formats {
58 buf.extend(&(format as i16).to_be_bytes());
59 }
60 }
61 });
62 }
63}
64
65// TODO: Unit Test Bind
66// TODO: Benchmark Bind