zero_postgres/protocol/frontend/
simple.rs

1//! Simple query protocol messages.
2
3use crate::protocol::codec::MessageBuilder;
4
5/// Write a Query message.
6///
7/// The query string may contain multiple SQL statements separated by semicolons.
8pub fn write_query(buf: &mut Vec<u8>, query: &str) {
9    log::debug!("QUERY {query}");
10    let mut msg = MessageBuilder::new(buf, super::msg_type::QUERY);
11    msg.write_cstr(query);
12    msg.finish();
13}
14
15#[cfg(test)]
16mod tests {
17    use super::*;
18
19    #[test]
20    fn test_query() {
21        let mut buf = Vec::new();
22        write_query(&mut buf, "SELECT 1");
23
24        assert_eq!(buf[0], b'Q');
25
26        // Length should be 4 (length field) + 9 (query + null terminator)
27        let len = i32::from_be_bytes([buf[1], buf[2], buf[3], buf[4]]);
28        assert_eq!(len, 13);
29
30        // Query string should be "SELECT 1\0"
31        assert_eq!(&buf[5..14], b"SELECT 1\0");
32    }
33}