2022-10-27 20:58:54 +00:00
|
|
|
pub mod kvp;
|
2022-10-13 23:31:26 +00:00
|
|
|
|
2022-10-18 22:58:05 +00:00
|
|
|
use std::fs;
|
2022-11-01 20:15:58 +00:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::path::Path;
|
2022-10-18 18:43:18 +00:00
|
|
|
|
2022-06-03 00:28:28 +00:00
|
|
|
use anyhow::Result;
|
2022-11-01 20:15:58 +00:00
|
|
|
use indoc::indoc;
|
|
|
|
use sqlez::connection::Connection;
|
2022-11-10 23:29:29 +00:00
|
|
|
use sqlez::domain::Domain;
|
2022-11-01 20:15:58 +00:00
|
|
|
use sqlez::thread_safe_connection::ThreadSafeConnection;
|
2022-11-04 20:22:35 +00:00
|
|
|
|
2022-11-10 23:29:29 +00:00
|
|
|
const INITIALIZE_QUERY: &'static str = indoc! {"
|
|
|
|
PRAGMA journal_mode=WAL;
|
|
|
|
PRAGMA synchronous=NORMAL;
|
|
|
|
PRAGMA foreign_keys=TRUE;
|
|
|
|
PRAGMA case_sensitive_like=TRUE;
|
|
|
|
"};
|
|
|
|
|
2022-10-18 22:58:05 +00:00
|
|
|
#[derive(Clone)]
|
2022-11-10 23:29:29 +00:00
|
|
|
pub struct Db<D: Domain>(ThreadSafeConnection<D>);
|
2022-11-01 20:15:58 +00:00
|
|
|
|
2022-11-10 23:29:29 +00:00
|
|
|
impl<D: Domain> Deref for Db<D> {
|
2022-11-01 20:15:58 +00:00
|
|
|
type Target = sqlez::connection::Connection;
|
2022-06-03 00:28:28 +00:00
|
|
|
|
2022-11-01 20:15:58 +00:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0.deref()
|
|
|
|
}
|
2022-10-18 22:58:05 +00:00
|
|
|
}
|
2022-10-13 22:43:42 +00:00
|
|
|
|
2022-11-10 23:29:29 +00:00
|
|
|
impl<D: Domain> Db<D> {
|
2022-10-18 22:58:05 +00:00
|
|
|
/// Open or create a database at the given directory path.
|
2022-10-28 18:50:26 +00:00
|
|
|
pub fn open(db_dir: &Path, channel: &'static str) -> Self {
|
2022-10-18 22:58:05 +00:00
|
|
|
// Use 0 for now. Will implement incrementing and clearing of old db files soon TM
|
2022-10-28 18:50:26 +00:00
|
|
|
let current_db_dir = db_dir.join(Path::new(&format!("0-{}", channel)));
|
2022-10-18 22:58:05 +00:00
|
|
|
fs::create_dir_all(¤t_db_dir)
|
|
|
|
.expect("Should be able to create the database directory");
|
|
|
|
let db_path = current_db_dir.join(Path::new("db.sqlite"));
|
|
|
|
|
2022-11-10 23:29:29 +00:00
|
|
|
Db(
|
|
|
|
ThreadSafeConnection::new(db_path.to_string_lossy().as_ref(), true)
|
|
|
|
.with_initialize_query(INITIALIZE_QUERY),
|
|
|
|
)
|
2022-10-24 23:55:32 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 23:31:26 +00:00
|
|
|
/// Open a in memory database for testing and as a fallback.
|
2022-11-02 20:26:23 +00:00
|
|
|
pub fn open_in_memory(db_name: &str) -> Self {
|
2022-11-10 23:29:29 +00:00
|
|
|
Db(ThreadSafeConnection::new(db_name, false).with_initialize_query(INITIALIZE_QUERY))
|
2022-11-04 20:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn persisting(&self) -> bool {
|
|
|
|
self.persistent()
|
2022-10-13 22:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 23:55:32 +00:00
|
|
|
pub fn write_to<P: AsRef<Path>>(&self, dest: P) -> Result<()> {
|
2022-11-01 20:15:58 +00:00
|
|
|
let destination = Connection::open_file(dest.as_ref().to_string_lossy().as_ref());
|
2022-11-01 22:58:23 +00:00
|
|
|
self.backup_main(&destination)
|
2022-10-18 18:43:18 +00:00
|
|
|
}
|
2022-11-04 20:22:35 +00:00
|
|
|
|
2022-11-10 23:29:29 +00:00
|
|
|
pub fn open_as<D2: Domain>(&self) -> Db<D2> {
|
|
|
|
Db(self.0.for_domain())
|
|
|
|
}
|
2022-11-04 20:22:35 +00:00
|
|
|
}
|