|
| 1 | +use std::{future::Future, pin::Pin}; |
| 2 | + |
| 3 | +use futures_util::Stream; |
| 4 | + |
1 | 5 | use crate::{ |
2 | 6 | DbBackend, DbErr, ExecResult, QueryResult, Statement, StatementBuilder, TransactionError, |
3 | 7 | }; |
4 | | -use futures_util::Stream; |
5 | | -use std::{future::Future, pin::Pin}; |
6 | 8 |
|
7 | 9 | /// The generic API for a database connection that can perform query or execute statements. |
8 | 10 | /// It abstracts database connection and transaction |
@@ -125,6 +127,45 @@ impl std::fmt::Display for AccessMode { |
125 | 127 | } |
126 | 128 | } |
127 | 129 |
|
| 130 | +#[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 131 | +/// Which kind of transaction to start. Only supported by SQLite. |
| 132 | +/// <https://www.sqlite.org/lang_transaction.html> |
| 133 | +#[cfg(feature = "sqlx-sqlite")] |
| 134 | +pub enum SqliteTransactionMode { |
| 135 | + /// The default. Transaction starts when the next statement is executed, and |
| 136 | + /// will be a read or write transaction depending on that statement. |
| 137 | + Deferred, |
| 138 | + /// Start a write transaction as soon as the BEGIN statement is received. |
| 139 | + Immediate, |
| 140 | + /// Start a write transaction as soon as the BEGIN statement is received. |
| 141 | + /// When in non-WAL mode, also block all other transactions from reading the |
| 142 | + /// database. |
| 143 | + Exclusive, |
| 144 | +} |
| 145 | + |
| 146 | +#[cfg(feature = "sqlx-sqlite")] |
| 147 | +impl SqliteTransactionMode { |
| 148 | + /// The keyword used to start a transaction in this mode (the word coming after "BEGIN"). |
| 149 | + pub fn sqlite_keyword(&self) -> &'static str { |
| 150 | + match self { |
| 151 | + SqliteTransactionMode::Deferred => "DEFERRED", |
| 152 | + SqliteTransactionMode::Immediate => "IMMEDIATE", |
| 153 | + SqliteTransactionMode::Exclusive => "EXCLUSIVE", |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +/// Configuration for starting a transaction |
| 159 | +#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)] |
| 160 | +pub struct TransactionConfig { |
| 161 | + #[cfg(any(feature = "sqlx-postgres", feature = "sqlx-mysql"))] |
| 162 | + pub isolation_level: Option<IsolationLevel>, |
| 163 | + #[cfg(any(feature = "sqlx-postgres", feature = "sqlx-mysql"))] |
| 164 | + pub access_mode: Option<AccessMode>, |
| 165 | + #[cfg(feature = "sqlx-sqlite")] |
| 166 | + pub sqlite_transaction_mode: Option<SqliteTransactionMode>, |
| 167 | +} |
| 168 | + |
128 | 169 | /// Spawn database transaction |
129 | 170 | #[async_trait::async_trait] |
130 | 171 | pub trait TransactionTrait { |
|
0 commit comments