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