sql_middleware/sqlite/
config.rs1use deadpool_sqlite::{Config as DeadpoolSqliteConfig, Runtime};
2
3use crate::middleware::{ConfigAndPool, DatabaseType, MiddlewarePool, SqlMiddlewareDbError};
4
5impl ConfigAndPool {
6 pub async fn new_sqlite(db_path: String) -> Result<Self, SqlMiddlewareDbError> {
11 let cfg: DeadpoolSqliteConfig = DeadpoolSqliteConfig::new(db_path.clone());
13
14 let pool = cfg.create_pool(Runtime::Tokio1).map_err(|e| {
16 SqlMiddlewareDbError::ConnectionError(format!("Failed to create SQLite pool: {e}"))
17 })?;
18
19 {
21 let conn = pool
22 .get()
23 .await
24 .map_err(SqlMiddlewareDbError::PoolErrorSqlite)?;
25 let _res = conn
26 .interact(|conn| {
27 conn.execute_batch(
28 "
29 PRAGMA journal_mode = WAL;
30 ",
31 )
32 .map_err(SqlMiddlewareDbError::SqliteError)
33 })
34 .await?;
35 }
36
37 Ok(ConfigAndPool {
38 pool: MiddlewarePool::Sqlite(pool),
39 db_type: DatabaseType::Sqlite,
40 })
41 }
42}
43
44impl From<deadpool_sqlite::InteractError> for SqlMiddlewareDbError {
46 fn from(err: deadpool_sqlite::InteractError) -> Self {
47 SqlMiddlewareDbError::ConnectionError(format!("SQLite Interact Error: {err}"))
48 }
49}