Skip to main content

spg_sqlx/
pool.rs

1//! v7.16.0 — `SpgPool` type alias + convenience constructors.
2//!
3//! v7.18 — pool concurrency is full-PG-parity. Each
4//! `SpgConnection` lazily attaches a per-connection
5//! `AsyncReadHandle` and refreshes it per statement, so a pool
6//! of N connections runs N concurrent SELECTs against the
7//! engine's snapshot path with read-committed semantics. The
8//! convenience constructors no longer pin `max_connections(1)`
9//! — they fall through to sqlx-core's default pool size, same
10//! as `PgPool::connect()`.
11
12use sqlx_core::error::Error;
13use sqlx_core::pool::{Pool, PoolOptions};
14
15use crate::database::Spg;
16use crate::options::SpgConnectOptions;
17
18/// Drop-in replacement for `sqlx::PgPool` over an in-process
19/// SPG. Same `Pool<Spg>` shape — every sqlx-core API generic
20/// over `Pool<DB>` works against this alias.
21pub type SpgPool = Pool<Spg>;
22
23/// Pool builder hooks — re-exported for ergonomic
24/// `SpgPoolOptions::new()` calls in mailrs-shape code.
25pub type SpgPoolOptions = PoolOptions<Spg>;
26
27/// Convenience constructors that mirror sqlx's pool-construction
28/// shape (`PgPool::connect(url)` style). Implemented as an
29/// extension trait on [`SpgPool`] so consumers can write
30/// `SpgPool::connect_in_memory().await` directly.
31pub trait SpgPoolExt: Sized {
32    /// In-memory pool — no persistence. Uses sqlx-core's
33    /// default `max_connections` (10), matching `PgPool::connect`.
34    fn connect_in_memory() -> futures_core::future::BoxFuture<'static, Result<SpgPool, Error>>;
35    /// File-backed pool at `path`. Uses sqlx-core's default
36    /// `max_connections` (10), matching `PgPool::connect`.
37    fn connect_path(
38        path: std::path::PathBuf,
39    ) -> futures_core::future::BoxFuture<'static, Result<SpgPool, Error>>;
40}
41
42impl SpgPoolExt for SpgPool {
43    fn connect_in_memory() -> futures_core::future::BoxFuture<'static, Result<SpgPool, Error>> {
44        Box::pin(async {
45            SpgPoolOptions::new()
46                .connect_with(SpgConnectOptions::in_memory())
47                .await
48        })
49    }
50
51    fn connect_path(
52        path: std::path::PathBuf,
53    ) -> futures_core::future::BoxFuture<'static, Result<SpgPool, Error>> {
54        Box::pin(async move {
55            SpgPoolOptions::new()
56                .connect_with(SpgConnectOptions::file(path))
57                .await
58        })
59    }
60}