classifier
ModuleStatement classifiers: whether a wire protocol message reads, writes or controls a transaction, so a proxy can route it to a replica or the primary.
A classifier does not parse: the SQL classifiers read the leading keyword
(skipping whitespace, comments and quoted literals), and the MongoDB
classifier reads the first key of an OP_MSG command document. For a
structural parse of SQL, see crate::ast.
Every classifier implements Classifier; the SQL ones also implement
Sql, which classifies bare SQL text and holds the shared keyword logic.
Messages are bytes (&[u8]), as they arrive on the wire.
PHP: Utopia\Query\Classifier, Utopia\Query\Classifier\*.
| PHP | Rust |
|---|---|
Classifier (interface), classify() | Classifier, Classifier::classify |
Classifier\SQL (abstract), classifySQL(), extractKeyword() | Sql, Sql::classify_sql, Sql::extract_keyword |
Classifier\MySQL | MySql |
Classifier\PostgreSQL | PostgreSql |
Classifier\MongoDB | MongoDb |
Type | crate::Type |
use utopia_query::Type;
use utopia_query::classifier::{Classifier, PostgreSql, Sql};
let classifier = PostgreSql;
assert_eq!(classifier.classify(b"Q\x00\x00\x00\x0dSELECT 1\x00"), Type::Read);
assert_eq!(classifier.classify_sql(b"/* hint */ insert into t values (1)"), Type::Write);
assert_eq!(classifier.extract_keyword(b" begin;"), b"BEGIN");use Utopia\Query\Classifier\PostgreSQL;
use Utopia\Query\Type;
$classifier = new PostgreSQL();
var_dump($classifier->classify("Q\x00\x00\x00\x0dSELECT 1\x00") === Type::Read); // bool(true)
var_dump($classifier->classifySQL('/* hint */ insert into t values (1)') === Type::Write); // bool(true)
echo $classifier->extractKeyword(' begin;'), "\n"; // BEGINContents
use utopia_query::classifier::MongoDb;pub struct MongoDb;Classifies MongoDB OP_MSG (opcode 2013) messages by their command name,
the first key of the body section's BSON document.
Reads (find, aggregate, count, ping, hello, ...) and writes
(insert, update, delete, create, drop, ...) are looked up;
commitTransaction / abortTransaction end a transaction, and a CRUD
command carrying a top-level startTransaction key begins one. Anything
else, or a malformed message, is unknown.
PHP: Utopia\Query\Classifier\MongoDB.
use utopia_query::classifier::MySql;pub struct MySql;Classifies MySQL client protocol packets: 3-byte payload length, sequence id, command byte, payload.
COM_QUERY (0x03) is classified by its SQL text; the prepared statement
commands (COM_STMT_PREPARE, _EXECUTE, _SEND_LONG_DATA, _CLOSE,
_RESET) are writes, so they reach the primary; anything else is unknown.
PHP: Utopia\Query\Classifier\MySQL.
use utopia_query::classifier::PostgreSql;pub struct PostgreSql;Classifies PostgreSQL frontend messages: type byte, big-endian length, body.
A simple query (Q) is classified by its SQL text (up to the first NUL);
the extended query messages Parse, Bind and Execute are writes, so
they reach the primary; anything else is unknown.
PHP: Utopia\Query\Classifier\PostgreSQL.
use utopia_query::classifier::Classifier;pub trait ClassifierClassifies a raw wire protocol message as a read, a write, a transaction statement or unknown. Implement it to route another protocol.
PHP: Utopia\Query\Classifier.
use utopia_query::classifier::Sql;pub trait Sql: ClassifierClassifies SQL text by its leading keyword: the logic shared by the SQL
wire protocol classifiers (super::MySql, super::PostgreSql),
which implement Classifier::classify for their packet format and
hand the SQL text to Sql::classify_sql.
Handles leading whitespace, -- and /* */ comments, quoted literals and
identifiers ('..', "..", `..`, $tag$..$tag$), mixed case,
COPY ... TO (read) against COPY ... FROM (write), and CTEs
(WITH ... SELECT reads, WITH ... INSERT writes).
PHP: Utopia\Query\Classifier\SQL.