MiddlewarePoolConnection

Enum MiddlewarePoolConnection 

Source
pub enum MiddlewarePoolConnection {
    Postgres {
        client: PooledConnection<'static, PgManager>,
        translate_placeholders: bool,
    },
    Sqlite {
        conn: Option<SqliteConnection>,
        translate_placeholders: bool,
    },
}

Variants§

§

Postgres

Fields

§client: PooledConnection<'static, PgManager>
§translate_placeholders: bool
§

Sqlite

Fields

§translate_placeholders: bool

Implementations§

Source§

impl MiddlewarePoolConnection

Source

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.

Source

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

Source

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?;
Source

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?;
Source

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.

Source

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

Source

pub fn translation_default(&self) -> bool

Pool-default translation toggle attached to this connection.

Source§

impl MiddlewarePoolConnection

Source

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.

Source

pub async fn interact_sync<F, R>(&self, f: F) -> Result<R, SqlMiddlewareDbError>
where F: FnOnce(AnyConnWrapper<'_>) -> R + Send + 'static, R: Send + 'static,

Interact with the connection synchronously

§Errors

Returns SqlMiddlewareDbError::Unimplemented for unsupported database types.

Trait Implementations§

Source§

impl Debug for MiddlewarePoolConnection

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> From<&'a mut MiddlewarePoolConnection> for BatchTarget<'a>

Source§

fn from(conn: &'a mut MiddlewarePoolConnection) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a mut MiddlewarePoolConnection> for QueryTarget<'a>

Source§

fn from(conn: &'a mut MiddlewarePoolConnection) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V