1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//! Statement 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");
//! ```
//!
//! ```php
//! 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"; // BEGIN
//! ```
pub use MongoDb;
pub use MySql;
pub use PostgreSql;
pub use Sql;
use crateType;
/// Classifies 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`.