utopia_query/classifier/postgresql.rs
1//! The PostgreSQL frontend protocol classifier.
2
3use super::{Classifier, Sql};
4use crate::Type;
5
6/// Classifies PostgreSQL frontend messages: type byte, big-endian length,
7/// body.
8///
9/// A simple query (`Q`) is classified by its SQL text (up to the first NUL);
10/// the extended query messages `Parse`, `Bind` and `Execute` are writes, so
11/// they reach the primary; anything else is unknown.
12///
13/// PHP: `Utopia\Query\Classifier\PostgreSQL`.
14#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
15pub struct PostgreSql;
16
17impl Classifier for PostgreSql {
18 /// PHP: `PostgreSQL::classify()`.
19 fn classify(&self, data: &[u8]) -> Type {
20 if data.len() < 6 {
21 return Type::Unknown;
22 }
23 match data[0] {
24 b'Q' => {
25 let query = &data[5..];
26 let end = query.iter().position(|&b| b == 0).unwrap_or(query.len());
27 self.classify_sql(&query[..end])
28 }
29 b'P' | b'B' | b'E' => Type::Write,
30 _ => Type::Unknown,
31 }
32 }
33}
34
35impl Sql for PostgreSql {}