rsfbclient_core/
transaction.rs

1//! Firebird transaction types
2//!
3//! More info about transactions in firebird:
4//! <https://firebirdsql.org/file/documentation/html/en/refdocs/fblangref30/firebird-30-language-reference.html#fblangref30-transacs>
5
6use crate::*;
7
8/// Transaction isolation level
9#[derive(Debug, Eq, PartialEq, Copy, Clone)]
10pub enum TrIsolationLevel {
11    /// Transactions can't see alterations commited after they started
12    Concurrency,
13    /// Table locking
14    Consistency,
15    /// Transactions can see alterations commited after they started
16    ReadCommited(TrRecordVersion),
17}
18
19impl Default for TrIsolationLevel {
20    fn default() -> Self {
21        Self::ReadCommited(TrRecordVersion::default())
22    }
23}
24
25impl From<TrIsolationLevel> for u8 {
26    fn from(tp: TrIsolationLevel) -> Self {
27        match tp {
28            TrIsolationLevel::Concurrency => ibase::isc_tpb_concurrency as u8,
29            TrIsolationLevel::Consistency => ibase::isc_tpb_consistency as u8,
30            TrIsolationLevel::ReadCommited(_) => ibase::isc_tpb_read_committed as u8,
31        }
32    }
33}
34
35#[derive(Debug, Eq, PartialEq, Copy, Clone)]
36/// Commit / Rollback operations
37pub enum TrOp {
38    Commit,
39    CommitRetaining,
40    Rollback,
41    RollbackRetaining,
42}
43
44/// Lock resolution modes
45#[derive(Debug, Eq, PartialEq, Copy, Clone)]
46pub enum TrLockResolution {
47    /// In the NO WAIT mode, a transaction will immediately throw a database exception if a conflict occurs
48    NoWait,
49    /// In the WAIT model, transaction will wait till the other transaction has finished.
50    ///
51    /// If a TIMEOUT is specified for the WAIT transaction, waiting will continue only for the number of seconds specified
52    Wait(Option<u32>),
53}
54
55impl Default for TrLockResolution {
56    fn default() -> Self {
57        Self::Wait(None)
58    }
59}
60
61impl From<TrLockResolution> for u8 {
62    fn from(tp: TrLockResolution) -> Self {
63        match tp {
64            TrLockResolution::NoWait => ibase::isc_tpb_nowait as u8,
65            TrLockResolution::Wait(_) => ibase::isc_tpb_wait as u8,
66        }
67    }
68}
69
70/// Data access mode
71#[repr(u8)]
72#[derive(Debug, Eq, PartialEq, Copy, Clone)]
73pub enum TrDataAccessMode {
74    /// Operations in the context of this transaction can be both read operations and data update operations
75    ReadWrite = ibase::isc_tpb_write as u8,
76    /// Only SELECT operations can be executed in the context of this transaction
77    ReadOnly = ibase::isc_tpb_read as u8,
78}
79
80impl Default for TrDataAccessMode {
81    fn default() -> Self {
82        Self::ReadWrite
83    }
84}
85
86/// Record version isolation
87#[repr(u8)]
88#[derive(Debug, Eq, PartialEq, Copy, Clone)]
89pub enum TrRecordVersion {
90    /// Is a kind of two-phase locking mechanism: it will make the transaction unable to write to any row that has an update pending from another transaction
91    RecordVersion = ibase::isc_tpb_rec_version as u8,
92    /// The transaction reads the latest committed version of the row, regardless of other pending versions of the row.
93    NoRecordVersion = ibase::isc_tpb_no_rec_version as u8,
94}
95
96impl Default for TrRecordVersion {
97    fn default() -> Self {
98        Self::NoRecordVersion
99    }
100}
101
102/// Parameters of a new transaction
103#[derive(Debug, Eq, PartialEq, Copy, Clone)]
104pub struct TransactionConfiguration {
105    pub data_access: TrDataAccessMode,
106    pub isolation: TrIsolationLevel,
107    pub lock_resolution: TrLockResolution,
108}
109
110impl Default for TransactionConfiguration {
111    fn default() -> Self {
112        Self {
113            data_access: TrDataAccessMode::default(),
114            isolation: TrIsolationLevel::default(),
115            lock_resolution: TrLockResolution::default(),
116        }
117    }
118}