Skip to main content

drizzle_sqlite/
connection.rs

1//! Transaction-type marker and per-driver behavior conversions for `SQLite` drivers.
2
3/// `SQLite` transaction types
4#[derive(Default, Debug, Clone, Copy)]
5pub enum SQLiteTransactionType {
6    #[default]
7    /// A deferred transaction is the default - it does not acquire locks until needed
8    Deferred,
9    /// An immediate transaction acquires a RESERVED lock immediately
10    Immediate,
11    /// An exclusive transaction acquires an EXCLUSIVE lock immediately
12    Exclusive,
13}
14
15#[cfg(feature = "rusqlite")]
16impl From<SQLiteTransactionType> for ::rusqlite::TransactionBehavior {
17    fn from(tx_type: SQLiteTransactionType) -> Self {
18        match tx_type {
19            SQLiteTransactionType::Deferred => Self::Deferred,
20            SQLiteTransactionType::Immediate => Self::Immediate,
21            SQLiteTransactionType::Exclusive => Self::Exclusive,
22        }
23    }
24}
25
26#[cfg(feature = "rusqlite")]
27impl From<::rusqlite::TransactionBehavior> for SQLiteTransactionType {
28    fn from(behavior: ::rusqlite::TransactionBehavior) -> Self {
29        match behavior {
30            ::rusqlite::TransactionBehavior::Immediate => Self::Immediate,
31            ::rusqlite::TransactionBehavior::Exclusive => Self::Exclusive,
32            // Deferred and any future variants default to Deferred.
33            _ => Self::Deferred,
34        }
35    }
36}
37
38// Convert to libsql::TransactionBehavior
39#[cfg(feature = "libsql")]
40impl From<SQLiteTransactionType> for libsql::TransactionBehavior {
41    fn from(tx_type: SQLiteTransactionType) -> Self {
42        match tx_type {
43            SQLiteTransactionType::Deferred => Self::Deferred,
44            SQLiteTransactionType::Immediate => Self::Immediate,
45            SQLiteTransactionType::Exclusive => Self::Exclusive,
46        }
47    }
48}
49
50// Convert from libsql::TransactionBehavior
51#[cfg(feature = "libsql")]
52impl From<libsql::TransactionBehavior> for SQLiteTransactionType {
53    fn from(behavior: libsql::TransactionBehavior) -> Self {
54        match behavior {
55            libsql::TransactionBehavior::Immediate => Self::Immediate,
56            libsql::TransactionBehavior::Exclusive => Self::Exclusive,
57            // Deferred and ReadOnly (mapped as closest equivalent) both fall through to Deferred.
58            libsql::TransactionBehavior::Deferred | libsql::TransactionBehavior::ReadOnly => {
59                Self::Deferred
60            }
61        }
62    }
63}
64
65// Convert to turso::TransactionBehavior
66#[cfg(feature = "turso")]
67impl From<SQLiteTransactionType> for turso::transaction::TransactionBehavior {
68    fn from(tx_type: SQLiteTransactionType) -> Self {
69        match tx_type {
70            SQLiteTransactionType::Deferred => Self::Deferred,
71            SQLiteTransactionType::Immediate => Self::Immediate,
72            SQLiteTransactionType::Exclusive => Self::Exclusive,
73        }
74    }
75}
76
77// Convert from turso::TransactionBehavior
78#[cfg(feature = "turso")]
79impl From<turso::transaction::TransactionBehavior> for SQLiteTransactionType {
80    fn from(behavior: turso::transaction::TransactionBehavior) -> Self {
81        match behavior {
82            turso::transaction::TransactionBehavior::Immediate => Self::Immediate,
83            turso::transaction::TransactionBehavior::Exclusive => Self::Exclusive,
84            // Deferred and any future variants default to Deferred.
85            _ => Self::Deferred,
86        }
87    }
88}