Skip to main content

cartel_sqlite/settings/options/
mod.rs

1use crate::{database, runtime, sys};
2use std::{mem, num, path};
3
4mod opening;
5
6#[derive(Clone, Copy, PartialEq, Eq)]
7pub enum Journal {
8    Rollback,
9    Wal(Wal),
10}
11
12#[derive(Clone, Copy, PartialEq, Eq)]
13pub struct Wal {
14    checkpoint: Checkpoint,
15    retained_bytes: Option<u64>,
16}
17
18impl Wal {
19    pub const fn new() -> Self {
20        Self {
21            checkpoint: Checkpoint::Auto,
22            retained_bytes: None,
23        }
24    }
25
26    pub const fn checkpoint(&mut self, mode: Checkpoint) -> &mut Self {
27        self.checkpoint = mode;
28        self
29    }
30
31    pub const fn retain(&mut self, bytes: Option<u64>) -> &mut Self {
32        self.retained_bytes = bytes;
33        self
34    }
35}
36
37impl Default for Wal {
38    fn default() -> Self {
39        Self::new()
40    }
41}
42
43#[derive(Clone, Copy, PartialEq, Eq)]
44pub enum Checkpoint {
45    Auto,
46    Pages(num::NonZero<u32>),
47    Manual,
48}
49
50#[derive(Clone, Copy, PartialEq, Eq)]
51pub enum Synchronous {
52    Off,
53    Normal,
54    Full,
55    Extra,
56}
57
58impl Synchronous {
59    const fn value(self) -> i32 {
60        match self {
61            Self::Off => 0,
62            Self::Normal => 1,
63            Self::Full => 2,
64            Self::Extra => 3,
65        }
66    }
67}
68
69#[derive(Clone, Copy, PartialEq, Eq)]
70pub enum Access {
71    Read,
72    Write,
73    Create,
74}
75
76#[derive(Clone, Copy, PartialEq, Eq)]
77pub struct OpenOptions {
78    access: Access,
79    journal: Journal,
80    synchronous: Synchronous,
81    mapped_bytes: u64,
82    path_bytes: usize,
83}
84
85impl OpenOptions {
86    const PATH_BYTES: usize = 4096;
87
88    pub const fn new() -> Self {
89        Self {
90            access: Access::Create,
91            journal: Journal::Rollback,
92            synchronous: Synchronous::Full,
93            mapped_bytes: 0,
94            path_bytes: Self::PATH_BYTES,
95        }
96    }
97
98    pub const fn access(&mut self, access: Access) -> &mut Self {
99        self.access = access;
100        self
101    }
102
103    pub const fn journal(&mut self, journal: Journal) -> &mut Self {
104        self.journal = journal;
105        self
106    }
107
108    pub const fn synchronous(&mut self, mode: Synchronous) -> &mut Self {
109        self.synchronous = mode;
110        self
111    }
112
113    pub const fn mapped_bytes(&mut self, bytes: u64) -> &mut Self {
114        self.mapped_bytes = bytes;
115        self
116    }
117
118    pub const fn path_bytes(&mut self, bytes: usize) -> &mut Self {
119        self.path_bytes = bytes;
120        self
121    }
122
123    pub fn open<'sql>(
124        &self,
125        runtime: runtime::Runtime,
126        path: impl AsRef<path::Path>,
127    ) -> Result<database::Target<'sql>, crate::Error> {
128        let path = path.as_ref();
129        let path = sys::Filename::new(path, self.path_bytes)?;
130        Ok(database::Target::new(
131            runtime,
132            database::Location::File(path),
133            *self,
134        ))
135    }
136
137    pub fn open_memory<'sql>(
138        &self,
139        runtime: runtime::Runtime,
140    ) -> Result<database::Target<'sql>, crate::Error> {
141        match self.journal {
142            Journal::Rollback => {}
143            Journal::Wal(_) => return Err(crate::Error::MemoryJournal),
144        }
145        Ok(database::Target::new(
146            runtime,
147            database::Location::Memory,
148            *self,
149        ))
150    }
151
152    pub(crate) const fn flags(self) -> i32 {
153        match self.access {
154            Access::Read => libsqlite3_sys::SQLITE_OPEN_READONLY,
155            Access::Write => libsqlite3_sys::SQLITE_OPEN_READWRITE,
156            Access::Create => {
157                libsqlite3_sys::SQLITE_OPEN_READWRITE | libsqlite3_sys::SQLITE_OPEN_CREATE
158            }
159        }
160    }
161}
162
163impl Default for OpenOptions {
164    fn default() -> Self {
165        Self::new()
166    }
167}
168
169const _: () = {
170    assert!(mem::size_of::<Access>() == 1);
171    assert!(mem::size_of::<OpenOptions>() == 6 * mem::size_of::<usize>());
172};