1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//! Firebird transaction types
//!
//! More info about transactions in firebird:
//! <https://firebirdsql.org/file/documentation/html/en/refdocs/fblangref30/firebird-30-language-reference.html#fblangref30-transacs>

use crate::*;

/// Transaction isolation level
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum TrIsolationLevel {
    /// Transactions can't see alterations commited after they started
    Concurrency,
    /// Table locking
    Consistency,
    /// Transactions can see alterations commited after they started
    ReadCommited(TrRecordVersion),
}

impl Default for TrIsolationLevel {
    fn default() -> Self {
        Self::ReadCommited(TrRecordVersion::default())
    }
}

impl From<TrIsolationLevel> for u8 {
    fn from(tp: TrIsolationLevel) -> Self {
        match tp {
            TrIsolationLevel::Concurrency => ibase::isc_tpb_concurrency as u8,
            TrIsolationLevel::Consistency => ibase::isc_tpb_consistency as u8,
            TrIsolationLevel::ReadCommited(_) => ibase::isc_tpb_read_committed as u8,
        }
    }
}

#[derive(Debug, Eq, PartialEq, Copy, Clone)]
/// Commit / Rollback operations
pub enum TrOp {
    Commit,
    CommitRetaining,
    Rollback,
    RollbackRetaining,
}

/// Lock resolution modes
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum TrLockResolution {
    /// In the NO WAIT mode, a transaction will immediately throw a database exception if a conflict occurs
    NoWait,
    /// In the WAIT model, transaction will wait till the other transaction has finished.
    ///
    /// If a TIMEOUT is specified for the WAIT transaction, waiting will continue only for the number of seconds specified
    Wait(Option<u32>),
}

impl Default for TrLockResolution {
    fn default() -> Self {
        Self::Wait(None)
    }
}

impl From<TrLockResolution> for u8 {
    fn from(tp: TrLockResolution) -> Self {
        match tp {
            TrLockResolution::NoWait => ibase::isc_tpb_nowait as u8,
            TrLockResolution::Wait(_) => ibase::isc_tpb_wait as u8,
        }
    }
}

/// Data access mode
#[repr(u8)]
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum TrDataAccessMode {
    /// Operations in the context of this transaction can be both read operations and data update operations
    ReadWrite = ibase::isc_tpb_write as u8,
    /// Only SELECT operations can be executed in the context of this transaction
    ReadOnly = ibase::isc_tpb_read as u8,
}

impl Default for TrDataAccessMode {
    fn default() -> Self {
        Self::ReadWrite
    }
}

/// Record version isolation
#[repr(u8)]
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum TrRecordVersion {
    /// 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
    RecordVersion = ibase::isc_tpb_rec_version as u8,
    /// The transaction reads the latest committed version of the row, regardless of other pending versions of the row.
    NoRecordVersion = ibase::isc_tpb_no_rec_version as u8,
}

impl Default for TrRecordVersion {
    fn default() -> Self {
        Self::NoRecordVersion
    }
}

/// Parameters of a new transaction
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub struct TransactionConfiguration {
    pub data_access: TrDataAccessMode,
    pub isolation: TrIsolationLevel,
    pub lock_resolution: TrLockResolution,
}

impl Default for TransactionConfiguration {
    fn default() -> Self {
        Self {
            data_access: TrDataAccessMode::default(),
            isolation: TrIsolationLevel::default(),
            lock_resolution: TrLockResolution::default(),
        }
    }
}