Skip to main content

vantage_sql/sqlite/
mod.rs

1#[macro_use]
2mod macros;
3pub mod impls;
4pub mod operation;
5pub(crate) mod row;
6pub mod statements;
7pub mod types;
8
9#[cfg(feature = "vista")]
10pub mod vista;
11
12use sqlx::sqlite::SqlitePool;
13
14pub use types::{AnySqliteType, SqliteType};
15
16crate::define_typed_ident!(
17    SqliteIdent,
18    sqlite_ident,
19    AnySqliteType,
20    crate::condition::SqliteCondition
21);
22
23/// SQLite provider. Wraps a connection pool.
24#[derive(Clone)]
25pub struct SqliteDB {
26    pool: SqlitePool,
27}
28
29impl SqliteDB {
30    /// Create from an existing sqlx connection pool.
31    pub fn new(pool: SqlitePool) -> Self {
32        Self { pool }
33    }
34
35    pub async fn connect(url: &str) -> Result<Self, sqlx::Error> {
36        let pool = SqlitePool::connect(url).await?;
37        Ok(Self { pool })
38    }
39
40    pub fn pool(&self) -> &SqlitePool {
41        &self.pool
42    }
43
44    /// Execute an aggregate query (COUNT, SUM, MAX, MIN etc.) and return the scalar result.
45    pub async fn aggregate(
46        &self,
47        select: &statements::SqliteSelect,
48        func: &str,
49        column: impl vantage_expressions::Expressive<AnySqliteType>,
50    ) -> vantage_core::Result<AnySqliteType> {
51        use vantage_expressions::ExprDataSource;
52        let expr = select.as_aggregate(func, column);
53        let result = self.execute(&expr).await?;
54        Ok(result.unwrap_scalar())
55    }
56}