1use super::custom::CustomTransaction;
2
3#[derive(Debug, Clone)]
5pub enum TransactionStmt {
6 Begin(BeginStmt),
7 Commit(CommitStmt),
8 Rollback(RollbackStmt),
9 Savepoint(SavepointStmt),
10 ReleaseSavepoint(ReleaseSavepointStmt),
11 SetTransaction(SetTransactionStmt),
12 LockTable(LockTableStmt),
13 PrepareTransaction(PrepareTransactionStmt),
15 CommitPrepared(CommitPreparedStmt),
17 RollbackPrepared(RollbackPreparedStmt),
19 Custom(Box<dyn CustomTransaction>),
20}
21
22impl TransactionStmt {
23 pub fn begin() -> Self {
24 Self::Begin(BeginStmt::default())
25 }
26
27 pub fn commit() -> Self {
28 Self::Commit(CommitStmt::default())
29 }
30
31 pub fn rollback() -> Self {
32 Self::Rollback(RollbackStmt::default())
33 }
34
35 pub fn savepoint(name: impl Into<String>) -> Self {
36 Self::Savepoint(SavepointStmt { name: name.into() })
37 }
38
39 pub fn release(name: impl Into<String>) -> Self {
40 Self::ReleaseSavepoint(ReleaseSavepointStmt { name: name.into() })
41 }
42
43 pub fn rollback_to(name: impl Into<String>) -> Self {
44 Self::Rollback(RollbackStmt {
45 to_savepoint: Some(name.into()),
46 ..Default::default()
47 })
48 }
49}
50
51#[derive(Debug, Clone, Default)]
56pub struct BeginStmt {
57 pub modes: Option<Vec<TransactionMode>>,
59 pub lock_type: Option<SqliteLockType>,
61 pub name: Option<String>,
63 pub with_mark: Option<String>,
65}
66
67impl BeginStmt {
68 pub fn with_isolation(level: IsolationLevel) -> Self {
69 Self {
70 modes: Some(vec![TransactionMode::IsolationLevel(level)]),
71 ..Default::default()
72 }
73 }
74
75 pub fn read_only() -> Self {
76 Self {
77 modes: Some(vec![TransactionMode::ReadOnly]),
78 ..Default::default()
79 }
80 }
81
82 pub fn sqlite_deferred() -> Self {
83 Self {
84 lock_type: Some(SqliteLockType::Deferred),
85 ..Default::default()
86 }
87 }
88
89 pub fn sqlite_immediate() -> Self {
90 Self {
91 lock_type: Some(SqliteLockType::Immediate),
92 ..Default::default()
93 }
94 }
95
96 pub fn sqlite_exclusive() -> Self {
97 Self {
98 lock_type: Some(SqliteLockType::Exclusive),
99 ..Default::default()
100 }
101 }
102}
103
104#[derive(Debug, Clone, Copy, PartialEq, Eq)]
106pub enum SqliteLockType {
107 Deferred,
108 Immediate,
109 Exclusive,
110}
111
112#[derive(Debug, Clone, Default)]
117pub struct CommitStmt {
118 pub and_chain: bool,
120 pub release: bool,
122 pub name: Option<String>,
124 pub comment: Option<String>,
126 pub write_mode: Option<OracleWriteMode>,
128 pub force: Option<String>,
130}
131
132#[derive(Debug, Clone, Copy, PartialEq, Eq)]
134pub struct OracleWriteMode {
135 pub wait: OracleWriteWait,
136 pub flush: OracleWriteFlush,
137}
138
139#[derive(Debug, Clone, Copy, PartialEq, Eq)]
140pub enum OracleWriteWait {
141 Wait,
142 NoWait,
143}
144
145#[derive(Debug, Clone, Copy, PartialEq, Eq)]
146pub enum OracleWriteFlush {
147 Immediate,
148 Batch,
149}
150
151#[derive(Debug, Clone, Default)]
156pub struct RollbackStmt {
157 pub to_savepoint: Option<String>,
159 pub and_chain: bool,
161 pub release: bool,
163 pub name: Option<String>,
165 pub force: Option<String>,
167}
168
169#[derive(Debug, Clone)]
174pub struct SavepointStmt {
175 pub name: String,
176}
177
178#[derive(Debug, Clone)]
184pub struct ReleaseSavepointStmt {
185 pub name: String,
186}
187
188#[derive(Debug, Clone)]
193pub struct SetTransactionStmt {
194 pub modes: Vec<TransactionMode>,
196 pub scope: Option<TransactionScope>,
198 pub snapshot_id: Option<String>,
200 pub name: Option<String>,
202}
203
204#[derive(Debug, Clone, PartialEq, Eq)]
206pub enum TransactionMode {
207 IsolationLevel(IsolationLevel),
208 ReadOnly,
209 ReadWrite,
210 Deferrable,
212 NotDeferrable,
214 WithConsistentSnapshot,
216}
217
218#[derive(Debug, Clone, Copy, PartialEq, Eq)]
219pub enum IsolationLevel {
220 ReadUncommitted,
221 ReadCommitted,
222 RepeatableRead,
223 Serializable,
224 Snapshot,
226}
227
228#[derive(Debug, Clone, Copy, PartialEq, Eq)]
230pub enum TransactionScope {
231 Session,
234 Global,
236}
237
238#[derive(Debug, Clone)]
243pub struct LockTableStmt {
244 pub tables: Vec<LockTableDef>,
245 pub nowait: bool,
247}
248
249#[derive(Debug, Clone)]
250pub struct LockTableDef {
251 pub table: String,
252 pub schema: Option<String>,
254 pub mode: LockMode,
255 pub only: bool,
257 pub alias: Option<String>,
259 pub wait: Option<u64>,
261 pub partition: Option<String>,
263}
264
265#[derive(Debug, Clone, Copy, PartialEq, Eq)]
267pub enum LockMode {
268 AccessShare,
270 RowShare,
271 RowExclusive,
272 ShareUpdateExclusive,
273 Share,
274 ShareRowExclusive,
275 Exclusive,
276 AccessExclusive,
277 Read,
279 ReadLocal,
280 Write,
281 LowPriorityWrite,
282}
283
284#[derive(Debug, Clone)]
290pub struct PrepareTransactionStmt {
291 pub transaction_id: String,
292}
293
294#[derive(Debug, Clone)]
296pub struct CommitPreparedStmt {
297 pub transaction_id: String,
298}
299
300#[derive(Debug, Clone)]
302pub struct RollbackPreparedStmt {
303 pub transaction_id: String,
304}