1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#![forbid(unsafe_code, non_ascii_idents)]

//! # SQLite arquitecture
//! *Reference:* https://www.sqlite.org/arch.html

use crate::io::SqliteIo;
use crate::pager::SqlitePager;
use crate::result::SqliteResult;
use crate::runtime::SqliteRuntime;
use std::sync::OnceLock;

pub mod header;
pub mod io;
#[cfg(feature = "log")]
pub(crate) mod log;
#[macro_use]
pub(crate) mod log_macros;
pub mod pager;
pub mod result;
pub mod runtime;
pub mod traits;
#[macro_use]
pub mod macros;

#[cfg(test)]
mod tests;

#[derive(Debug)]
pub struct SqliteConnection {
  runtime: SqliteRuntime,
}
static VERSION_NUMBER: OnceLock<u32> = OnceLock::new();

impl SqliteConnection {
  pub fn open(conn_str: impl AsRef<str>) -> SqliteResult<Self> {
    crate::log::EnvLogger::init();

    VERSION_NUMBER.get_or_init(|| {
      let mut s = env!("CARGO_PKG_VERSION").split('.');
      let release = s.next().and_then(|x| x.parse().ok()).unwrap_or(0u32);
      let major = s.next().and_then(|x| x.parse().ok()).unwrap_or(0u32);
      let minor = s.next().and_then(|x| x.parse().ok()).unwrap_or(0u32);

      (10_000 * release) + (100 * major) + minor
    });

    trace!("Openning SQliteIo [{}]...", conn_str.as_ref());
    let io = SqliteIo::open(conn_str)?;
    trace!("SQliteIo started: [{io:?}].");
    trace!("Connecting SqlitePager...");
    let pager = SqlitePager::connect(io)?;
    trace!("SQliteIo started: [{pager:?}].");
    trace!("Starting SqliteRuntime...");
    let runtime = SqliteRuntime::start(pager)?;
    trace!("SqliteRuntime started: [{runtime:?}].");

    Ok(Self { runtime })
  }

  pub fn runtime(&self) -> &SqliteRuntime {
    &self.runtime
  }

  pub fn runtime_mut(&mut self) -> &mut SqliteRuntime {
    &mut self.runtime
  }
}