sqlite_plugin/
flags.rs

1use core::fmt::{self, Debug, Formatter};
2
3use crate::vars;
4
5#[derive(Debug, PartialEq, Eq)]
6pub enum OpenKind {
7    Unknown,
8    MainDb,
9    MainJournal,
10    TempDb,
11    TempJournal,
12    TransientDb,
13    SubJournal,
14    SuperJournal,
15    Wal,
16}
17
18impl OpenKind {
19    pub fn is_temp(&self) -> bool {
20        matches!(self, Self::TempDb | Self::TempJournal | Self::TransientDb)
21    }
22}
23
24impl From<i32> for OpenKind {
25    fn from(flags: i32) -> Self {
26        match flags {
27            flags if flags & vars::SQLITE_OPEN_MAIN_DB > 0 => Self::MainDb,
28            flags if flags & vars::SQLITE_OPEN_MAIN_JOURNAL > 0 => Self::MainJournal,
29            flags if flags & vars::SQLITE_OPEN_TEMP_DB > 0 => Self::TempDb,
30            flags if flags & vars::SQLITE_OPEN_TEMP_JOURNAL > 0 => Self::TempJournal,
31            flags if flags & vars::SQLITE_OPEN_TRANSIENT_DB > 0 => Self::TransientDb,
32            flags if flags & vars::SQLITE_OPEN_SUBJOURNAL > 0 => Self::SubJournal,
33            flags if flags & vars::SQLITE_OPEN_SUPER_JOURNAL > 0 => Self::SuperJournal,
34            flags if flags & vars::SQLITE_OPEN_WAL > 0 => Self::Wal,
35            _ => Self::Unknown,
36        }
37    }
38}
39
40#[derive(Debug, PartialEq, Eq)]
41pub enum CreateMode {
42    None,
43    Create,
44    MustCreate,
45}
46
47#[derive(Debug, PartialEq, Eq)]
48pub enum OpenMode {
49    ReadOnly,
50    ReadWrite { create: CreateMode },
51}
52
53impl From<i32> for OpenMode {
54    fn from(flags: i32) -> Self {
55        const MUST_CREATE: i32 = vars::SQLITE_OPEN_CREATE | vars::SQLITE_OPEN_EXCLUSIVE;
56        match flags {
57            flags if flags & vars::SQLITE_OPEN_READONLY > 0 => Self::ReadOnly,
58            flags if flags & vars::SQLITE_OPEN_READWRITE > 0 => Self::ReadWrite {
59                create: match flags {
60                    flags if flags & MUST_CREATE == MUST_CREATE => CreateMode::MustCreate,
61                    flags if flags & vars::SQLITE_OPEN_CREATE > 0 => CreateMode::Create,
62                    _ => CreateMode::None,
63                },
64            },
65            _ => Self::ReadOnly,
66        }
67    }
68}
69
70impl OpenMode {
71    pub fn must_create(&self) -> bool {
72        matches!(self, Self::ReadWrite { create: CreateMode::MustCreate })
73    }
74    pub fn is_readonly(&self) -> bool {
75        matches!(self, Self::ReadOnly)
76    }
77}
78
79#[derive(Clone, Copy)]
80pub struct OpenOpts {
81    flags: i32,
82}
83
84impl OpenOpts {
85    pub fn new(flags: i32) -> Self {
86        Self { flags }
87    }
88
89    pub fn flags(&self) -> i32 {
90        self.flags
91    }
92
93    pub fn kind(&self) -> OpenKind {
94        self.flags.into()
95    }
96
97    pub fn mode(&self) -> OpenMode {
98        self.flags.into()
99    }
100
101    pub fn delete_on_close(&self) -> bool {
102        self.flags & vars::SQLITE_OPEN_DELETEONCLOSE > 0
103    }
104
105    pub fn set_readonly(&mut self) {
106        self.flags &= !vars::SQLITE_OPEN_READWRITE;
107        self.flags |= vars::SQLITE_OPEN_READONLY;
108    }
109}
110
111impl From<i32> for OpenOpts {
112    fn from(flags: i32) -> Self {
113        Self::new(flags)
114    }
115}
116
117impl Debug for OpenOpts {
118    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
119        f.debug_struct("OpenOpts")
120            .field("flags", &self.flags)
121            .field("kind", &self.kind())
122            .field("mode", &self.mode())
123            .field("delete_on_close", &self.delete_on_close())
124            .finish()
125    }
126}
127
128#[derive(Debug, PartialEq, Eq)]
129pub enum AccessFlags {
130    Exists,
131    Read,
132    ReadWrite,
133}
134
135impl From<i32> for AccessFlags {
136    fn from(flags: i32) -> Self {
137        match flags {
138            flags if flags == vars::SQLITE_ACCESS_EXISTS => Self::Exists,
139            flags if flags & vars::SQLITE_ACCESS_READ > 0 => Self::Read,
140            flags if flags & vars::SQLITE_ACCESS_READWRITE > 0 => Self::ReadWrite,
141            _ => Self::Exists,
142        }
143    }
144}
145
146/// Represents one of the 5 `SQLite` locking levels.
147/// See [SQLite documentation](https://www.sqlite.org/lockingv3.html) for more information.
148#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
149pub enum LockLevel {
150    /// No locks are held; the database may be neither read nor written.
151    Unlocked,
152
153    /// The database may be read but not written. Multiple Shared locks can
154    /// coexist at once.
155    Shared,
156
157    /// A shared lock with the intention to upgrade to an exclusive lock. Only
158    /// one Reserved lock can exist at once.
159    Reserved,
160
161    /// A lock in the process of upgrading to a reserved lock. Can coexist with
162    /// Shared locks, but no new shared locks can be taken.
163    Pending,
164
165    /// The database may be read or written, but no other locks can be held.
166    Exclusive,
167}
168
169impl From<i32> for LockLevel {
170    fn from(lock: i32) -> Self {
171        match lock {
172            vars::SQLITE_LOCK_NONE => Self::Unlocked,
173            vars::SQLITE_LOCK_SHARED => Self::Shared,
174            vars::SQLITE_LOCK_RESERVED => Self::Reserved,
175            vars::SQLITE_LOCK_PENDING => Self::Pending,
176            vars::SQLITE_LOCK_EXCLUSIVE => Self::Exclusive,
177            _ => panic!("invalid lock level: {}", lock),
178        }
179    }
180}