Skip to main content

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