1use rusqlite::Params;
2
3use crate::data_storage::DataStorage;
4use crate::repositories::SQLiteConfig;
5use std::borrow::Borrow;
6use std::sync::{Arc, Mutex};
7
8pub mod build;
9pub mod bundle;
10mod explore;
11mod fetch;
12pub use said::{derivation::HashFunctionCode, sad::SerializationFormats, version::Encode};
13
14#[derive(Clone)]
15pub struct Connection {
16 pub connection: Arc<Mutex<rusqlite::Connection>>,
17}
18
19impl Connection {
20 pub fn new(path: &str) -> Self {
21 let conn = rusqlite::Connection::open(path).unwrap();
22 Self {
23 connection: Arc::new(Mutex::new(conn)),
24 }
25 }
26
27 pub fn execute<P>(&self, sql: &str, params: P) -> rusqlite::Result<usize>
28 where
29 P: Params,
30 {
31 let connection = self.connection.lock().unwrap();
32 connection.execute(sql, params)
33 }
34}
35
36pub struct Facade {
37 db: Box<dyn DataStorage>,
38 db_cache: Box<dyn DataStorage>,
39 connection: Connection,
40}
41
42impl Facade {
43 pub fn new(
44 db: Box<dyn DataStorage>,
45 db_cache: Box<dyn DataStorage>,
46 cache_storage_config: SQLiteConfig,
47 ) -> Self {
48 let cache_path: String = match cache_storage_config.path {
49 Some(path) => {
50 if !path.try_exists().unwrap() {
51 std::fs::create_dir_all(path.clone()).unwrap();
52 }
53 path.join("search_data.db")
54 .clone()
55 .into_os_string()
56 .into_string()
57 .unwrap()
58 }
59 None => ":memory:".to_string(),
60 };
61
62 Self {
63 db,
64 db_cache,
65 connection: Connection::new(&cache_path),
66 }
67 }
68
69 pub(crate) fn connection(&self) -> Connection {
70 self.connection.clone()
71 }
72
73 pub fn storage(&self) -> &dyn DataStorage {
74 self.db_cache.borrow()
75 }
76}