2025-03-22 22:17:47 -04:00
|
|
|
use crate::config::Config;
|
|
|
|
use rusqlite::{Connection, Result};
|
2025-03-23 12:31:48 -04:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use tetratto_l10n::{LangFile, read_langs};
|
2025-03-22 22:17:47 -04:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
2025-03-23 12:31:48 -04:00
|
|
|
pub struct DataManager(pub Config, pub HashMap<String, LangFile>);
|
2025-03-22 22:17:47 -04:00
|
|
|
|
|
|
|
impl DataManager {
|
|
|
|
/// Obtain a connection to the staging database.
|
|
|
|
pub(crate) async fn connect(&self) -> Result<Connection> {
|
|
|
|
Ok(Connection::open(&self.0.database.name)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new [`DataManager`] (and init database).
|
|
|
|
pub async fn new(config: Config) -> Result<Self> {
|
2025-03-23 12:31:48 -04:00
|
|
|
let this = Self(config.clone(), read_langs());
|
2025-03-22 22:17:47 -04:00
|
|
|
|
2025-03-23 18:03:11 -04:00
|
|
|
let conn = this.connect().await?;
|
2025-03-22 22:17:47 -04:00
|
|
|
conn.pragma_update(None, "journal_mode", "WAL").unwrap();
|
|
|
|
|
|
|
|
Ok(this)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "sqlite")]
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! get {
|
|
|
|
($row:ident->$idx:literal($t:tt)) => {
|
|
|
|
$row.get::<usize, $t>($idx).unwrap()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! query_row {
|
|
|
|
($conn:expr, $sql:expr, $params:expr, $f:expr) => {{
|
|
|
|
let mut query = $conn.prepare($sql).unwrap();
|
|
|
|
query.query_row($params, $f)
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! execute {
|
|
|
|
($conn:expr, $sql:expr, $params:expr) => {
|
|
|
|
$conn.prepare($sql).unwrap().execute($params)
|
|
|
|
};
|
|
|
|
}
|