Skip to main content

elif_orm/transactions/
isolation.rs

1//! Transaction Isolation Levels
2//!
3//! Provides isolation level management for different database systems.
4
5/// Transaction isolation levels supported by PostgreSQL
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum IsolationLevel {
8    /// Read Uncommitted - lowest isolation level
9    ReadUncommitted,
10    /// Read Committed - default PostgreSQL isolation level
11    ReadCommitted,
12    /// Repeatable Read - stronger consistency guarantees
13    RepeatableRead,
14    /// Serializable - highest isolation level
15    Serializable,
16}
17
18impl IsolationLevel {
19    /// Convert to SQL string for SET TRANSACTION ISOLATION LEVEL command
20    pub fn as_sql(&self) -> &'static str {
21        match self {
22            IsolationLevel::ReadUncommitted => "READ UNCOMMITTED",
23            IsolationLevel::ReadCommitted => "READ COMMITTED",
24            IsolationLevel::RepeatableRead => "REPEATABLE READ",
25            IsolationLevel::Serializable => "SERIALIZABLE",
26        }
27    }
28
29    /// Get the default isolation level for a database dialect
30    pub fn default_for_dialect(dialect: &crate::backends::SqlDialect) -> Self {
31        match dialect {
32            crate::backends::SqlDialect::PostgreSQL => IsolationLevel::ReadCommitted,
33            crate::backends::SqlDialect::MySQL => IsolationLevel::RepeatableRead,
34            crate::backends::SqlDialect::SQLite => IsolationLevel::Serializable,
35        }
36    }
37
38    /// Check if this isolation level is supported by the given dialect
39    pub fn is_supported_by(&self, dialect: &crate::backends::SqlDialect) -> bool {
40        match dialect {
41            crate::backends::SqlDialect::PostgreSQL => true, // All levels supported
42            crate::backends::SqlDialect::MySQL => {
43                matches!(
44                    self,
45                    IsolationLevel::ReadUncommitted
46                        | IsolationLevel::ReadCommitted
47                        | IsolationLevel::RepeatableRead
48                        | IsolationLevel::Serializable
49                )
50            }
51            crate::backends::SqlDialect::SQLite => {
52                // SQLite only effectively supports Serializable
53                matches!(self, IsolationLevel::Serializable)
54            }
55        }
56    }
57}