toasty_core/driver/operation/
transaction.rs

1use super::Operation;
2
3/// Isolation levels supported across SQL backends.
4///
5/// Not all backends support all levels — the driver will return an error
6/// if an unsupported level is requested.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum IsolationLevel {
9    ReadUncommitted,
10    ReadCommitted,
11    RepeatableRead,
12    Serializable,
13}
14
15#[derive(Debug, Clone)]
16pub enum Transaction {
17    /// Start a transaction with optional configuration.
18    ///
19    /// When `isolation` is `None` and `read_only` is `false`, the database's
20    /// default isolation level and read-write mode are used.
21    Start {
22        isolation: Option<IsolationLevel>,
23        read_only: bool,
24    },
25
26    /// Commit a transaction
27    Commit,
28
29    /// Rollback a transaction
30    Rollback,
31
32    /// Create a savepoint with the given identifier
33    Savepoint(String),
34
35    /// Release (commit) a savepoint
36    ReleaseSavepoint(String),
37
38    /// Rollback to a savepoint, undoing work since it was created
39    RollbackToSavepoint(String),
40}
41
42impl Transaction {
43    /// Start a transaction with database defaults.
44    pub fn start() -> Self {
45        Self::Start {
46            isolation: None,
47            read_only: false,
48        }
49    }
50}
51
52impl Operation {
53    /// Returns `true` if this is a [`Transaction::Commit`] operation.
54    pub fn is_transaction_commit(&self) -> bool {
55        matches!(self, Operation::Transaction(Transaction::Commit))
56    }
57}
58
59impl From<Transaction> for Operation {
60    fn from(value: Transaction) -> Self {
61        Self::Transaction(value)
62    }
63}