#[macro_use]
mod macros;
pub mod impls;
pub mod operation;
pub(crate) mod row;
pub mod statements;
pub mod types;
#[cfg(feature = "vista")]
pub mod vista;
use std::sync::Arc;
use sqlx::sqlite::SqlitePool;
use vantage_vista::{Transaction, TxKey};
use crate::sql_tx::{SqlBegin, SqlTx};
pub use types::{AnySqliteType, SqliteType};
crate::define_typed_ident!(
SqliteIdent,
sqlite_ident,
AnySqliteType,
crate::condition::SqliteCondition
);
#[derive(Clone)]
pub struct SqliteDB {
pool: SqlitePool,
key: Arc<()>,
tx: Option<Arc<SqlTx<sqlx::Sqlite>>>,
}
impl SqliteDB {
pub fn new(pool: SqlitePool) -> Self {
Self {
pool,
key: Arc::new(()),
tx: None,
}
}
pub async fn connect(url: &str) -> Result<Self, sqlx::Error> {
Ok(Self::new(SqlitePool::connect(url).await?))
}
pub fn pool(&self) -> &SqlitePool {
&self.pool
}
pub fn in_transaction(&self, tx: &Transaction) -> vantage_core::Result<Self> {
tx.check_open()?;
let joined = tx.participant(self.tx_key(), || {
SqlTx::new(SqlBegin::new(self.pool.clone(), "BEGIN IMMEDIATE"), tx)
})?;
Ok(Self {
tx: Some(joined),
..self.clone()
})
}
pub fn tx_key(&self) -> TxKey {
TxKey::of(&self.key)
}
pub async fn aggregate(
&self,
select: &statements::SqliteSelect,
func: &str,
column: impl vantage_expressions::Expressive<AnySqliteType>,
) -> vantage_core::Result<AnySqliteType> {
use vantage_expressions::ExprDataSource;
let expr = select.as_aggregate(func, column);
let result = self.execute(&expr).await?;
Ok(result.unwrap_scalar())
}
}