use std::path::Path;
#[cfg(doc)]
use crate::migrate::{Database, Migrator};
pub struct Config {
pub(super) manager: r2d2_sqlite::SqliteConnectionManager,
pub(super) init: Box<dyn FnOnce(&rusqlite::Transaction)>,
pub synchronous: Synchronous,
pub foreign_keys: ForeignKeys,
}
#[non_exhaustive]
pub enum Synchronous {
Full,
Normal,
}
impl Synchronous {
#[cfg_attr(test, mutants::skip)] pub(crate) fn as_str(self) -> &'static str {
match self {
Synchronous::Full => "FULL",
Synchronous::Normal => "NORMAL",
}
}
}
#[non_exhaustive]
pub enum ForeignKeys {
Rust,
SQLite,
}
impl ForeignKeys {
pub(crate) fn as_str(self) -> &'static str {
match self {
ForeignKeys::Rust => "OFF",
ForeignKeys::SQLite => "ON",
}
}
}
impl Config {
pub fn open(p: impl AsRef<Path>) -> Self {
let manager = r2d2_sqlite::SqliteConnectionManager::file(p);
Self::open_internal(manager)
}
pub fn open_in_memory() -> Self {
let manager = r2d2_sqlite::SqliteConnectionManager::memory();
Self::open_internal(manager)
}
fn open_internal(manager: r2d2_sqlite::SqliteConnectionManager) -> Self {
Self {
manager,
init: Box::new(|_| {}),
synchronous: Synchronous::Full,
foreign_keys: ForeignKeys::SQLite,
}
}
}