sqlx_postgres/message/
execute.rs

1use std::num::Saturating;
2
3use sqlx_core::Error;
4
5use crate::io::{PgBufMutExt, PortalId};
6use crate::message::{FrontendMessage, FrontendMessageFormat};
7
8pub struct Execute {
9    /// The id of the portal to execute.
10    pub portal: PortalId,
11
12    /// Maximum number of rows to return, if portal contains a query
13    /// that returns rows (ignored otherwise). Zero denotes “no limit”.
14    pub limit: u32,
15}
16
17impl FrontendMessage for Execute {
18    const FORMAT: FrontendMessageFormat = FrontendMessageFormat::Execute;
19
20    fn body_size_hint(&self) -> Saturating<usize> {
21        let mut size = Saturating(0);
22
23        size += self.portal.name_len();
24        size += 2; // limit
25
26        size
27    }
28
29    fn encode_body(&self, buf: &mut Vec<u8>) -> Result<(), Error> {
30        buf.put_portal_name(self.portal);
31        buf.extend(&self.limit.to_be_bytes());
32
33        Ok(())
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use crate::io::PortalId;
40    use crate::message::FrontendMessage;
41
42    use super::Execute;
43
44    #[test]
45    fn test_encode_execute_named_portal() {
46        const EXPECTED: &[u8] = b"E\0\0\0\x1Asqlx_p_1234567890\0\0\0\0\x02";
47
48        let mut buf = Vec::new();
49        let m = Execute {
50            portal: PortalId::TEST_VAL,
51            limit: 2,
52        };
53
54        m.encode_msg(&mut buf).unwrap();
55
56        assert_eq!(buf, EXPECTED);
57    }
58
59    #[test]
60    fn test_encode_execute_unnamed_portal() {
61        const EXPECTED: &[u8] = b"E\0\0\0\x09\0\x49\x96\x02\xD2";
62
63        let mut buf = Vec::new();
64        let m = Execute {
65            portal: PortalId::UNNAMED,
66            limit: 1234567890,
67        };
68
69        m.encode_msg(&mut buf).unwrap();
70
71        assert_eq!(buf, EXPECTED);
72    }
73}