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//! Drivers handle [`Insert`]. SQL drivers also handle [`QuerySql`] and
6//! [`RawSql`]; key-value 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
47mod upsert;
48pub use upsert::Upsert;
49
50/// A single database operation to be executed by a driver.
51///
52/// Each variant maps to one logical database action. The query planner sends
53/// [`Insert`](Self::Insert) to every driver. SQL drivers also receive
54/// [`QuerySql`](Self::QuerySql), while key-value drivers receive
55/// [`GetByKey`](Self::GetByKey), [`QueryPk`](Self::QueryPk), etc., according to
56/// the driver's [`Capability`](super::Capability).
57///
58/// All operation types implement `From<T> for Operation`, so they can be
59/// converted with `.into()`.
60///
61/// # Examples
62///
63/// ```
64/// use toasty_core::driver::operation::{Operation, Transaction};
65///
66/// let op: Operation = Transaction::start().into();
67/// assert!(!op.is_transaction_commit());
68/// ```
69#[derive(Debug, Clone)]
70pub enum Operation {
71    /// Insert a new record. Contains a lowered [`stmt::Insert`](crate::stmt::Insert) statement.
72    Insert(Insert),
73
74    /// Delete one or more records identified by primary key.
75    DeleteByKey(DeleteByKey),
76
77    /// Look up primary keys via a secondary index.
78    FindPkByIndex(FindPkByIndex),
79
80    /// Fetch one or more records by exact primary key match.
81    GetByKey(GetByKey),
82
83    /// Query a table with a primary key filter, optional secondary filtering,
84    /// ordering, and pagination.
85    QueryPk(QueryPk),
86
87    /// Execute SQL generated from a lowered Toasty statement AST. Only sent to
88    /// SQL-capable drivers.
89    QuerySql(QuerySql),
90
91    /// Execute user-authored SQL text. Only sent to SQL-capable drivers.
92    RawSql(RawSql),
93
94    /// A transaction lifecycle operation (begin, commit, rollback, savepoint).
95    Transaction(Transaction),
96
97    /// Update one or more records identified by primary key.
98    UpdateByKey(UpdateByKey),
99
100    /// Atomically creates or updates one record by a unique key on a non-SQL driver.
101    Upsert(Upsert),
102
103    /// Full-table scan with optional filter and pagination.
104    ///
105    /// Only sent to drivers with [`Capability::scan`](super::Capability::scan) set to `true`.
106    Scan(Scan),
107}
108
109impl Operation {
110    /// Returns the operation variant name for logging.
111    pub fn name(&self) -> &'static str {
112        match self {
113            Operation::Insert(_) => "insert",
114            Operation::DeleteByKey(_) => "delete_by_key",
115            Operation::FindPkByIndex(_) => "find_pk_by_index",
116            Operation::GetByKey(_) => "get_by_key",
117            Operation::QueryPk(_) => "query_pk",
118            Operation::QuerySql(_) => "query_sql",
119            Operation::RawSql(_) => "raw_sql",
120            Operation::Transaction(_) => "transaction",
121            Operation::UpdateByKey(_) => "update_by_key",
122            Operation::Upsert(_) => "upsert",
123            Operation::Scan(_) => "scan",
124        }
125    }
126}