sql_middleware/pool/
mod.rs

1pub mod any_conn_wrapper;
2pub mod connection;
3pub mod interaction;
4pub mod types;
5
6pub use any_conn_wrapper::AnyConnWrapper;
7pub use connection::MiddlewarePoolConnection;
8pub use types::MiddlewarePool;
9
10use crate::SqlMiddlewareDbError;
11use crate::types::DatabaseType;
12
13/// Configuration plus connection pool for a database backend.
14///
15/// Construct with the backend-specific `new_*` helpers on `ConfigAndPool` (e.g., `new_sqlite`,
16/// `new_postgres`), then borrow connections as needed:
17/// ```rust,no_run
18/// use sql_middleware::prelude::*;
19///
20/// # async fn demo() -> Result<(), SqlMiddlewareDbError> {
21/// let cap = ConfigAndPool::new_sqlite("file::memory:?cache=shared".into()).await?;
22/// let mut conn = cap.get_connection().await?;
23/// let rows = conn.query("SELECT 1").select().await?;
24/// assert_eq!(rows.results.len(), 1);
25/// # Ok(()) }
26/// ```
27#[derive(Clone, Debug)]
28pub struct ConfigAndPool {
29    /// The connection pool
30    pub pool: MiddlewarePool,
31    /// The database type
32    pub db_type: DatabaseType,
33    /// Whether placeholder translation is enabled by default for this pool
34    pub translate_placeholders: bool,
35}
36
37impl ConfigAndPool {
38    /// Get a pooled connection and attach pool-level defaults to it.
39    ///
40    /// # Errors
41    /// Bubbles up pool checkout errors for the active backend.
42    ///
43    /// # Examples
44    /// ```rust,no_run
45    /// use sql_middleware::prelude::*;
46    ///
47    /// # async fn demo() -> Result<(), SqlMiddlewareDbError> {
48    /// let cap = ConfigAndPool::new_sqlite("file::memory:?cache=shared".into()).await?;
49    /// let mut conn = cap.get_connection().await?;
50    /// conn.execute_batch("CREATE TABLE t (id INTEGER)").await?;
51    /// # Ok(()) }
52    /// ```
53    pub async fn get_connection(&self) -> Result<MiddlewarePoolConnection, SqlMiddlewareDbError> {
54        let pool_ref = self.pool.get().await?;
55        MiddlewarePool::get_connection(pool_ref, self.translate_placeholders).await
56    }
57}