use sqlx::{Pool, Sqlite};
use sqlx::{pool::PoolOptions, Error, SqlitePool};
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqliteSynchronous};
use std::str::FromStr;
use std::sync::Arc;
use tokio::sync::OnceCell;
use std::time::Duration;
use crate::common::error::QueryError;
static DB_POOL: OnceCell<Arc<SqlitePool>> = OnceCell::const_new();
pub async fn setup_db_pool<'a>(pool: Pool<Sqlite>) -> Result<&'a SqlitePool, Error> {
let pool = Arc::new(pool);
DB_POOL.get_or_try_init(|| async { Ok(pool) }).await
.map(|arc| arc.as_ref())
}
pub async fn create_db_pool(database_url: &str) -> Result<&SqlitePool, Error> {
let connect_options = SqliteConnectOptions::from_str(database_url)
.map_err(|e| Error::from(e))?
.create_if_missing(true)
.journal_mode(SqliteJournalMode::Wal)
.synchronous(SqliteSynchronous::Normal)
.busy_timeout(Duration::from_secs(8));
let pool = PoolOptions::new()
.max_connections(8)
.min_connections(1)
.acquire_timeout(Duration::from_secs(8))
.idle_timeout(Duration::from_secs(30))
.test_before_acquire(false)
.connect_with(connect_options)
.await
.map_err(|e| Error::from(e))?;
setup_db_pool(pool).await
}
pub fn get_db_pool() -> Result<Arc<SqlitePool>, Error> {
DB_POOL.get()
.cloned() .ok_or_else(||QueryError::DBPoolNotInitialized.into())
}