toasty_core/driver/operation.rs
1//! Database operations dispatched to drivers.
2//!
3//! An [`Operation`] is the unit of work sent to [`Connection::exec`](super::Connection::exec).
4//! The query engine compiles user queries into one or more `Operation` values.
5//! SQL drivers handle [`QuerySql`] and [`Insert`]; key-value drivers handle
6//! [`GetByKey`], [`QueryPk`], [`DeleteByKey`], [`FindPkByIndex`], [`UpdateByKey`],
7//! and (when [`Capability::scan`](super::Capability::scan) is `true`) [`Scan`].
8//! Both driver types handle [`Transaction`] operations.
9
10mod delete_by_key;
11pub use delete_by_key::DeleteByKey;
12
13mod find_pk_by_index;
14pub use find_pk_by_index::FindPkByIndex;
15
16mod get_by_key;
17pub use get_by_key::GetByKey;
18
19mod insert;
20pub use insert::Insert;
21
22mod pagination;
23pub use pagination::Pagination;
24
25mod query_pk;
26pub use query_pk::QueryPk;
27
28mod query_sql;
29pub use query_sql::QuerySql;
30
31mod scan;
32pub use scan::Scan;
33
34mod transaction;
35pub use transaction::{IsolationLevel, Transaction, TransactionMode};
36
37mod typed_value;
38pub use typed_value::TypedValue;
39
40mod update_by_key;
41pub use update_by_key::UpdateByKey;
42
43/// A single database operation to be executed by a driver.
44///
45/// Each variant maps to one logical database action. The query planner selects
46/// variants based on the driver's [`Capability`](super::Capability): SQL
47/// drivers receive [`QuerySql`](Self::QuerySql) and [`Insert`](Self::Insert),
48/// while key-value drivers receive [`GetByKey`](Self::GetByKey),
49/// [`QueryPk`](Self::QueryPk), etc.
50///
51/// All operation types implement `From<T> for Operation`, so they can be
52/// converted with `.into()`.
53///
54/// # Examples
55///
56/// ```
57/// use toasty_core::driver::operation::{Operation, Transaction};
58///
59/// let op: Operation = Transaction::start().into();
60/// assert!(!op.is_transaction_commit());
61/// ```
62#[derive(Debug, Clone)]
63pub enum Operation {
64 /// Insert a new record. Contains a lowered [`stmt::Insert`](crate::stmt::Insert) statement.
65 Insert(Insert),
66
67 /// Delete one or more records identified by primary key.
68 DeleteByKey(DeleteByKey),
69
70 /// Look up primary keys via a secondary index.
71 FindPkByIndex(FindPkByIndex),
72
73 /// Fetch one or more records by exact primary key match.
74 GetByKey(GetByKey),
75
76 /// Query a table with a primary key filter, optional secondary filtering,
77 /// ordering, and pagination.
78 QueryPk(QueryPk),
79
80 /// Execute a raw SQL statement. Only sent to SQL-capable drivers.
81 QuerySql(QuerySql),
82
83 /// A transaction lifecycle operation (begin, commit, rollback, savepoint).
84 Transaction(Transaction),
85
86 /// Update one or more records identified by primary key.
87 UpdateByKey(UpdateByKey),
88
89 /// Full-table scan with optional filter and pagination.
90 ///
91 /// Only sent to drivers with [`Capability::scan`](super::Capability::scan) set to `true`.
92 Scan(Scan),
93}
94
95impl Operation {
96 /// Returns the operation variant name for logging.
97 pub fn name(&self) -> &str {
98 match self {
99 Operation::Insert(_) => "insert",
100 Operation::DeleteByKey(_) => "delete_by_key",
101 Operation::FindPkByIndex(_) => "find_pk_by_index",
102 Operation::GetByKey(_) => "get_by_key",
103 Operation::QueryPk(_) => "query_pk",
104 Operation::QuerySql(_) => "query_sql",
105 Operation::Transaction(_) => "transaction",
106 Operation::UpdateByKey(_) => "update_by_key",
107 Operation::Scan(_) => "scan",
108 }
109 }
110}