sql-middleware 0.9.0

Lightweight async wrappers for tokio-postgres, rusqlite, turso, and tiberius.
Documentation
#[cfg(feature = "mssql")]
mod mssql;
#[cfg(feature = "postgres")]
mod postgres;
#[cfg(feature = "sqlite")]
mod sqlite;
#[cfg(feature = "turso")]
mod turso;

#[cfg(feature = "postgres")]
use crate::postgres::typed::PgManager;
#[cfg(any(feature = "postgres", feature = "mssql", feature = "turso"))]
use bb8::PooledConnection;
#[cfg(feature = "mssql")]
use bb8_tiberius::ConnectionManager;
#[cfg(feature = "postgres")]
use std::collections::HashMap;

use super::types::MiddlewarePool;
use crate::error::SqlMiddlewareDbError;
#[cfg(feature = "sqlite")]
use crate::sqlite::SqliteConnection;
use crate::types::StatementCacheMode;

#[cfg(feature = "turso")]
use crate::turso::typed::TursoManager;

pub enum MiddlewarePoolConnection {
    #[cfg(feature = "postgres")]
    Postgres {
        client: PooledConnection<'static, PgManager>,
        translate_placeholders: bool,
        statement_cache_mode: StatementCacheMode,
        prepared_statements: HashMap<String, tokio_postgres::Statement>,
    },
    #[cfg(feature = "sqlite")]
    Sqlite {
        conn: Option<SqliteConnection>,
        translate_placeholders: bool,
        statement_cache_mode: StatementCacheMode,
    },
    #[cfg(feature = "mssql")]
    Mssql {
        conn: PooledConnection<'static, ConnectionManager>,
        translate_placeholders: bool,
        statement_cache_mode: StatementCacheMode,
    },
    #[cfg(feature = "turso")]
    Turso {
        conn: PooledConnection<'static, TursoManager>,
        translate_placeholders: bool,
        statement_cache_mode: StatementCacheMode,
    },
}

// Manual Debug implementation because some pool variants do not expose `Debug`
impl std::fmt::Debug for MiddlewarePoolConnection {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            #[cfg(feature = "postgres")]
            Self::Postgres { client, .. } => f.debug_tuple("Postgres").field(client).finish(),
            #[cfg(feature = "sqlite")]
            Self::Sqlite { conn, .. } => f.debug_tuple("Sqlite").field(conn).finish(),
            #[cfg(feature = "mssql")]
            Self::Mssql { .. } => f
                .debug_tuple("Mssql")
                .field(&"<TiberiusConnection>")
                .finish(),
            #[cfg(feature = "turso")]
            Self::Turso { .. } => f.debug_tuple("Turso").field(&"<Connection>").finish(),
        }
    }
}

impl MiddlewarePool {
    /// Get a connection from the pool
    ///
    /// # Errors
    /// Returns `SqlMiddlewareDbError::PoolErrorPostgres` or `SqlMiddlewareDbError::PoolErrorSqlite` if the pool fails to provide a connection.
    pub async fn get_connection(
        pool: &MiddlewarePool,
        translate_placeholders: bool,
        statement_cache_mode: StatementCacheMode,
    ) -> Result<MiddlewarePoolConnection, SqlMiddlewareDbError> {
        match pool {
            #[cfg(feature = "postgres")]
            MiddlewarePool::Postgres(pool) => {
                postgres::get_connection(pool, translate_placeholders, statement_cache_mode).await
            }
            #[cfg(feature = "sqlite")]
            MiddlewarePool::Sqlite(pool) => {
                sqlite::get_connection(pool, translate_placeholders, statement_cache_mode).await
            }
            #[cfg(feature = "mssql")]
            MiddlewarePool::Mssql(pool) => {
                mssql::get_connection(pool, translate_placeholders, statement_cache_mode).await
            }
            #[cfg(feature = "turso")]
            MiddlewarePool::Turso(pool) => {
                turso::get_connection(pool, translate_placeholders, statement_cache_mode).await
            }
            #[allow(unreachable_patterns)]
            _ => Err(SqlMiddlewareDbError::Unimplemented(
                "This database type is not enabled in the current build".to_string(),
            )),
        }
    }
}

impl MiddlewarePoolConnection {
    /// Pool-default translation toggle attached to this connection.
    #[must_use]
    pub fn translation_default(&self) -> bool {
        match self {
            #[cfg(feature = "postgres")]
            MiddlewarePoolConnection::Postgres {
                translate_placeholders,
                ..
            } => *translate_placeholders,
            #[cfg(feature = "sqlite")]
            MiddlewarePoolConnection::Sqlite {
                translate_placeholders,
                ..
            } => *translate_placeholders,
            #[cfg(feature = "mssql")]
            MiddlewarePoolConnection::Mssql {
                translate_placeholders,
                ..
            } => *translate_placeholders,
            #[cfg(feature = "turso")]
            MiddlewarePoolConnection::Turso {
                translate_placeholders,
                ..
            } => *translate_placeholders,
        }
    }

    /// Pool-default statement cache mode attached to this connection.
    #[must_use]
    pub fn statement_cache_mode_default(&self) -> StatementCacheMode {
        match self {
            #[cfg(feature = "postgres")]
            MiddlewarePoolConnection::Postgres {
                statement_cache_mode,
                ..
            } => *statement_cache_mode,
            #[cfg(feature = "sqlite")]
            MiddlewarePoolConnection::Sqlite {
                statement_cache_mode,
                ..
            } => *statement_cache_mode,
            #[cfg(feature = "mssql")]
            MiddlewarePoolConnection::Mssql {
                statement_cache_mode,
                ..
            } => *statement_cache_mode,
            #[cfg(feature = "turso")]
            MiddlewarePoolConnection::Turso {
                statement_cache_mode,
                ..
            } => *statement_cache_mode,
        }
    }
}