use std::path::PathBuf;
use crate::middleware::{ConfigAndPool, DatabaseType, MiddlewarePool, SqlMiddlewareDbError};
use super::config::SqliteManager;
#[derive(Debug, Clone)]
pub struct SqliteOptions {
pub db_path: PathBuf,
pub translate_placeholders: bool,
}
impl SqliteOptions {
#[must_use]
pub fn new(db_path: String) -> Self {
Self {
db_path: db_path.into(),
translate_placeholders: false,
}
}
#[must_use]
pub fn from_path(db_path: impl Into<PathBuf>) -> Self {
Self {
db_path: db_path.into(),
translate_placeholders: false,
}
}
#[must_use]
pub fn with_translation(mut self, translate_placeholders: bool) -> Self {
self.translate_placeholders = translate_placeholders;
self
}
}
#[derive(Debug, Clone)]
pub struct SqliteOptionsBuilder {
opts: SqliteOptions,
}
impl SqliteOptionsBuilder {
#[must_use]
pub fn new(db_path: String) -> Self {
Self {
opts: SqliteOptions::new(db_path),
}
}
#[must_use]
pub fn from_path(db_path: impl Into<PathBuf>) -> Self {
Self {
opts: SqliteOptions::from_path(db_path),
}
}
#[must_use]
pub fn translation(mut self, translate_placeholders: bool) -> Self {
self.opts.translate_placeholders = translate_placeholders;
self
}
#[must_use]
pub fn finish(self) -> SqliteOptions {
self.opts
}
pub async fn build(self) -> Result<ConfigAndPool, SqlMiddlewareDbError> {
ConfigAndPool::new_sqlite(self.finish()).await
}
}
impl ConfigAndPool {
#[must_use]
pub fn sqlite_builder(db_path: String) -> SqliteOptionsBuilder {
SqliteOptionsBuilder::new(db_path)
}
#[must_use]
pub fn sqlite_path_builder(db_path: impl Into<PathBuf>) -> SqliteOptionsBuilder {
SqliteOptionsBuilder::from_path(db_path)
}
pub async fn new_sqlite(opts: SqliteOptions) -> Result<Self, SqlMiddlewareDbError> {
let manager = SqliteManager::from_path(opts.db_path.clone());
let pool = manager.build_pool().await?;
{
let mut conn = pool.get_owned().await.map_err(|e| {
SqlMiddlewareDbError::ConnectionError(format!("Failed to create SQLite pool: {e}"))
})?;
crate::sqlite::apply_wal_pragmas(&mut conn).await?;
}
Ok(ConfigAndPool {
pool: MiddlewarePool::Sqlite(pool),
db_type: DatabaseType::Sqlite,
translate_placeholders: opts.translate_placeholders,
})
}
}