pub enum MiddlewarePoolConnection {
Postgres {
client: PooledConnection<'static, PgManager>,
translate_placeholders: bool,
},
Sqlite {
conn: Option<SqliteConnection>,
translate_placeholders: bool,
},
}Variants§
Implementations§
Source§impl MiddlewarePoolConnection
impl MiddlewarePoolConnection
Sourcepub async fn execute_batch(
&mut self,
query: &str,
) -> Result<(), SqlMiddlewareDbError>
pub async fn execute_batch( &mut self, query: &str, ) -> Result<(), SqlMiddlewareDbError>
Executes a batch of SQL queries within a transaction by delegating to the specific database module.
§Errors
Returns an error if the selected backend cannot execute the batch or the database responds with an error.
Sourcepub fn query<'a>(&'a mut self, query: &'a str) -> QueryBuilder<'a, 'a>
pub fn query<'a>(&'a mut self, query: &'a str) -> QueryBuilder<'a, 'a>
Start a fluent query builder that can translate placeholders before executing.
§Examples
use sql_middleware::prelude::*;
let cap = ConfigAndPool::new_sqlite("file::memory:?cache=shared".into()).await?;
let mut conn = cap.get_connection().await?;
conn.execute_batch("CREATE TABLE t (id INTEGER)").await?;
let rows = conn
.query("SELECT id FROM t WHERE id = ?1")
.params(&[RowValues::Int(1)])
.select()
.await?;
assert!(rows.results.is_empty());Source§impl MiddlewarePoolConnection
impl MiddlewarePoolConnection
Sourcepub async fn with_blocking_sqlite<F, R>(
&mut self,
func: F,
) -> Result<R, SqlMiddlewareDbError>where
F: FnOnce(&mut Connection) -> Result<R, SqlMiddlewareDbError> + Send + 'static,
R: Send + 'static,
pub async fn with_blocking_sqlite<F, R>(
&mut self,
func: F,
) -> Result<R, SqlMiddlewareDbError>where
F: FnOnce(&mut Connection) -> Result<R, SqlMiddlewareDbError> + Send + 'static,
R: Send + 'static,
Run synchronous SQLite work on the underlying worker-owned connection.
Use this when you need to batch multiple statements in one worker hop, reuse rusqlite
features we don’t expose (savepoints, pragmas that return rows, custom hooks), or avoid
re-preparing statements in hot loops. It keeps blocking work off the async runtime while
letting you drive the raw rusqlite::Connection.
The closure runs on a worker thread and must not capture non-Send state across an
await. Do all work inside the closure and return promptly instead of holding onto the
connection handle.
§Errors
Returns SqlMiddlewareDbError::Unimplemented when the connection is not SQLite.
§Examples
use sql_middleware::prelude::*;
let cap = ConfigAndPool::new_sqlite("file::memory:?cache=shared".into()).await?;
let mut conn = cap.get_connection().await?;
conn.with_blocking_sqlite(|raw| {
raw.execute_batch("CREATE TABLE t (id INTEGER, name TEXT);")?;
Ok::<_, SqlMiddlewareDbError>(())
})
.await?;Sourcepub async fn prepare_sqlite_statement(
&mut self,
query: &str,
) -> Result<SqlitePreparedStatement<'_>, SqlMiddlewareDbError>
pub async fn prepare_sqlite_statement( &mut self, query: &str, ) -> Result<SqlitePreparedStatement<'_>, SqlMiddlewareDbError>
Prepare a SQLite statement and obtain a reusable handle backed by the worker thread.
§Errors
Returns SqlMiddlewareDbError::Unimplemented when the underlying connection is not
SQLite, or propagates any preparation error reported by the worker thread.
The returned handle borrows the worker-owned connection. Use it within the async scope that
created it; do not move it across tasks or hold it across long await chains.
§Examples
use sql_middleware::prelude::*;
let cap = ConfigAndPool::new_sqlite("file::memory:?cache=shared".into()).await?;
let mut conn = cap.get_connection().await?;
conn.execute_batch("CREATE TABLE t (id INTEGER, name TEXT)").await?;
let prepared = conn
.prepare_sqlite_statement("INSERT INTO t (id, name) VALUES (?1, ?2)")
.await?;
prepared
.execute(&[RowValues::Int(1), RowValues::Text("alice".into())])
.await?;Sourcepub fn into_sqlite(
self,
) -> Result<(SqliteConnection, bool), SqlMiddlewareDbError>
pub fn into_sqlite( self, ) -> Result<(SqliteConnection, bool), SqlMiddlewareDbError>
Extract the SQLite connection, returning the translation flag alongside it.
§Errors
Returns SqlMiddlewareDbError if the connection is already taken or the enum is not SQLite.
Sourcepub fn from_sqlite_parts(
conn: SqliteConnection,
translate_placeholders: bool,
) -> MiddlewarePoolConnection
pub fn from_sqlite_parts( conn: SqliteConnection, translate_placeholders: bool, ) -> MiddlewarePoolConnection
Rewrap a SQLite connection back into the enum with the original translation flag.
Source§impl MiddlewarePoolConnection
impl MiddlewarePoolConnection
Sourcepub fn translation_default(&self) -> bool
pub fn translation_default(&self) -> bool
Pool-default translation toggle attached to this connection.
Source§impl MiddlewarePoolConnection
impl MiddlewarePoolConnection
Sourcepub async fn interact_async<F, Fut>(
&mut self,
func: F,
) -> Result<Fut::Output, SqlMiddlewareDbError>where
F: FnOnce(AnyConnWrapper<'_>) -> Fut + Send + 'static,
Fut: Future<Output = Result<(), SqlMiddlewareDbError>> + Send + 'static,
pub async fn interact_async<F, Fut>(
&mut self,
func: F,
) -> Result<Fut::Output, SqlMiddlewareDbError>where
F: FnOnce(AnyConnWrapper<'_>) -> Fut + Send + 'static,
Fut: Future<Output = Result<(), SqlMiddlewareDbError>> + Send + 'static,
Interact with the connection asynchronously.
This hands the raw driver client into your closure on the async runtime.
Keep the closure non-blocking—doing heavy work here will stall the runtime.
For SQLite, use with_blocking_sqlite / interact_sync instead.
§Errors
Returns SqlMiddlewareDbError::Unimplemented for unsupported database types.
Sourcepub async fn interact_sync<F, R>(&self, f: F) -> Result<R, SqlMiddlewareDbError>
pub async fn interact_sync<F, R>(&self, f: F) -> Result<R, SqlMiddlewareDbError>
Interact with the connection synchronously
§Errors
Returns SqlMiddlewareDbError::Unimplemented for unsupported database types.