sqlite_rs/
lib.rs

1#![forbid(unsafe_code, non_ascii_idents)]
2
3//! # SQLite arquitecture
4//! *Reference:* https://www.sqlite.org/arch.html
5
6use crate::io::SqliteIo;
7use crate::pager::SqlitePager;
8use crate::result::SqliteResult;
9use crate::runtime::SqliteRuntime;
10use std::sync::OnceLock;
11
12pub mod header;
13pub mod io;
14#[cfg(feature = "log")]
15pub(crate) mod log;
16#[macro_use]
17pub(crate) mod log_macros;
18pub mod pager;
19pub mod result;
20pub mod runtime;
21pub mod traits;
22#[macro_use]
23pub mod macros;
24
25#[cfg(test)]
26mod tests;
27
28#[derive(Debug)]
29pub struct SqliteConnection {
30  runtime: SqliteRuntime,
31}
32static VERSION_NUMBER: OnceLock<u32> = OnceLock::new();
33
34impl SqliteConnection {
35  pub fn open(conn_str: impl AsRef<str>) -> SqliteResult<Self> {
36    crate::log::EnvLogger::init();
37
38    VERSION_NUMBER.get_or_init(|| {
39      let mut s = env!("CARGO_PKG_VERSION").split('.');
40      let release = s.next().and_then(|x| x.parse().ok()).unwrap_or(0u32);
41      let major = s.next().and_then(|x| x.parse().ok()).unwrap_or(0u32);
42      let minor = s.next().and_then(|x| x.parse().ok()).unwrap_or(0u32);
43
44      (10_000 * release) + (100 * major) + minor
45    });
46
47    trace!("Openning SQliteIo [{}]...", conn_str.as_ref());
48    let io = SqliteIo::open(conn_str)?;
49    trace!("SQliteIo started: [{io:?}].");
50    trace!("Connecting SqlitePager...");
51    let pager = SqlitePager::connect(io)?;
52    trace!("SQliteIo started: [{pager:?}].");
53    trace!("Starting SqliteRuntime...");
54    let runtime = SqliteRuntime::start(pager)?;
55    trace!("SqliteRuntime started: [{runtime:?}].");
56
57    Ok(Self { runtime })
58  }
59
60  pub fn runtime(&self) -> &SqliteRuntime {
61    &self.runtime
62  }
63
64  pub fn runtime_mut(&mut self) -> &mut SqliteRuntime {
65    &mut self.runtime
66  }
67}