Skip to main content

utopia_query/classifier/
mysql.rs

1//! The MySQL client protocol classifier.
2
3use super::{Classifier, Sql};
4use crate::Type;
5
6/// Classifies MySQL client protocol packets: 3-byte payload length, sequence
7/// id, command byte, payload.
8///
9/// `COM_QUERY` (`0x03`) is classified by its SQL text; the prepared statement
10/// commands (`COM_STMT_PREPARE`, `_EXECUTE`, `_SEND_LONG_DATA`, `_CLOSE`,
11/// `_RESET`) are writes, so they reach the primary; anything else is unknown.
12///
13/// PHP: `Utopia\Query\Classifier\MySQL`.
14#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
15pub struct MySql;
16
17const COM_QUERY: u8 = 0x03;
18const COM_STMT_PREPARE: u8 = 0x16;
19const COM_STMT_EXECUTE: u8 = 0x17;
20const COM_STMT_SEND_LONG_DATA: u8 = 0x18;
21const COM_STMT_CLOSE: u8 = 0x19;
22const COM_STMT_RESET: u8 = 0x1A;
23
24impl Classifier for MySql {
25    /// PHP: `MySQL::classify()`.
26    fn classify(&self, data: &[u8]) -> Type {
27        if data.len() < 5 {
28            return Type::Unknown;
29        }
30        match data[4] {
31            COM_QUERY => self.classify_sql(&data[5..]),
32            COM_STMT_PREPARE | COM_STMT_EXECUTE | COM_STMT_SEND_LONG_DATA | COM_STMT_CLOSE | COM_STMT_RESET => {
33                Type::Write
34            }
35            _ => Type::Unknown,
36        }
37    }
38}
39
40impl Sql for MySql {}