Skip to main content

a3s_orm/drivers/sqlite/
options.rs

1use std::time::Duration;
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4pub enum SqliteJournalMode {
5    Delete,
6    Truncate,
7    Persist,
8    Memory,
9    Wal,
10    Off,
11}
12
13impl SqliteJournalMode {
14    pub(crate) const fn as_sql(self) -> &'static str {
15        match self {
16            Self::Delete => "DELETE",
17            Self::Truncate => "TRUNCATE",
18            Self::Persist => "PERSIST",
19            Self::Memory => "MEMORY",
20            Self::Wal => "WAL",
21            Self::Off => "OFF",
22        }
23    }
24}
25
26#[derive(Clone, Copy, Debug, PartialEq, Eq)]
27pub struct SqliteOptions {
28    pub busy_timeout: Duration,
29    pub foreign_keys: bool,
30    pub journal_mode: SqliteJournalMode,
31}
32
33impl Default for SqliteOptions {
34    fn default() -> Self {
35        Self {
36            busy_timeout: Duration::from_secs(5),
37            foreign_keys: true,
38            journal_mode: SqliteJournalMode::Wal,
39        }
40    }
41}
42
43impl SqliteOptions {
44    pub(crate) fn in_memory() -> Self {
45        Self {
46            journal_mode: SqliteJournalMode::Memory,
47            ..Self::default()
48        }
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn renders_every_journal_mode() {
58        assert_eq!(SqliteJournalMode::Delete.as_sql(), "DELETE");
59        assert_eq!(SqliteJournalMode::Truncate.as_sql(), "TRUNCATE");
60        assert_eq!(SqliteJournalMode::Persist.as_sql(), "PERSIST");
61        assert_eq!(SqliteJournalMode::Memory.as_sql(), "MEMORY");
62        assert_eq!(SqliteJournalMode::Wal.as_sql(), "WAL");
63        assert_eq!(SqliteJournalMode::Off.as_sql(), "OFF");
64    }
65}