Skip to main content

saddle/
db.rs

1//! Business-facing managed database capability.
2//!
3//! Pool construction and shutdown are intentionally not exposed here. The
4//! facade creates exactly one underlying pool and injects this capability into
5//! Service implementations.
6
7pub use saddle_db::{
8    DbRow, DbValue, MAX_FIELD_BYTES, MAX_PARAMETER_BYTES, MAX_PARAMETERS,
9    MAX_PARAMETERS_TOTAL_BYTES, MAX_QUERY_ROWS, MAX_RESULT_BYTES, MAX_SQL_BYTES, Statement,
10    Transaction, TransactionFuture, WriteResult,
11};
12
13use crate::{CallContext, Result};
14
15/// Cloneable handle to Saddle's single process-wide database pool.
16#[derive(Clone)]
17pub struct Database(pub(crate) saddle_db::Database);
18
19impl Database {
20    pub async fn query_all(
21        &self,
22        parent: &CallContext,
23        statement: Statement,
24    ) -> Result<Vec<DbRow>> {
25        self.0.query_all(parent, statement).await
26    }
27
28    pub async fn query_optional(
29        &self,
30        parent: &CallContext,
31        statement: Statement,
32    ) -> Result<Option<DbRow>> {
33        self.0.query_optional(parent, statement).await
34    }
35
36    pub async fn write(&self, parent: &CallContext, statement: Statement) -> Result<WriteResult> {
37        self.0.write(parent, statement).await
38    }
39
40    pub async fn transaction<T, F>(
41        &self,
42        parent: &CallContext,
43        operation: impl Into<crate::OperationId>,
44        work: F,
45    ) -> Result<T>
46    where
47        T: Send,
48        F: for<'a> FnOnce(&'a mut Transaction) -> TransactionFuture<'a, T> + Send,
49    {
50        self.0.transaction(parent, operation, work).await
51    }
52}