mod bootstrap;
use std::path::Path;
use boson_backend_sql_common::SqlQueueBackend;
use boson_core::Result;
use sqlx::SqlitePool;
pub use bootstrap::install_default_sqlite_backend;
pub struct SqliteQueueBackend {
inner: SqlQueueBackend,
}
impl SqliteQueueBackend {
pub async fn new(path: impl AsRef<Path>) -> Result<Self> {
let url = format!("sqlite://{}?mode=rwc", path.as_ref().display());
Self::connect(&url).await
}
pub async fn connect(url: &str) -> Result<Self> {
let inner = SqlQueueBackend::connect_sqlite(url).await?;
Ok(Self { inner })
}
pub async fn from_pool(pool: SqlitePool) -> Result<Self> {
let inner = SqlQueueBackend::from_sqlite_pool(pool).await?;
Ok(Self { inner })
}
#[must_use]
pub fn pool(&self) -> &SqlitePool {
match self.inner.pool() {
boson_backend_sql_common::SqlPool::Sqlite(pool) => pool,
boson_backend_sql_common::SqlPool::Postgres(_) => {
panic!("sqlite backend has non-sqlite pool")
}
}
}
}
impl std::fmt::Debug for SqliteQueueBackend {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SqliteQueueBackend").finish_non_exhaustive()
}
}
boson_backend_sql_common::delegate_queue_backend!(SqliteQueueBackend, inner);