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