easy-sql 0.101.1

Macro-first SQL toolkit with compile-time checked queries, optional migrations on top of sqlx.
Documentation
use std::{
    fmt::Debug,
    ops::{Deref, DerefMut},
};

use easy_macros::always_context;

use sqlx::{Database, Executor};

use crate::{
    Driver, EasyExecutor,
    traits::{DriverConnection, InternalDriver, SetupSql},
};
/// Wrapper around [`sqlx::pool::PoolConnection`](https://docs.rs/sqlx/latest/sqlx/pool/struct.PoolConnection.html)
///
/// Will contain sql query watch data in the future (gated by a feature)
#[derive(Debug)]
pub struct Connection<D: Driver> {
    internal: sqlx::pool::PoolConnection<InternalDriver<D>>,
}

#[always_context]
impl<D: Driver> Connection<D> {
    pub fn new(conn: sqlx::pool::PoolConnection<InternalDriver<D>>) -> Self {
        Connection { internal: conn }
    }
}

#[always_context]
impl<D: Driver> EasyExecutor<D> for Connection<D>
where
    for<'b> &'b mut DriverConnection<D>: Executor<'b, Database = D::InternalDriver>,
{
    type InternalExecutor<'b>
        = &'b mut DriverConnection<D>
    where
        Self: 'b;

    async fn query_setup<O: SetupSql<D> + Send + Sync>(
        &mut self,
        sql: O,
    ) -> anyhow::Result<O::Output>
    where
        DriverConnection<D>: Send + Sync,
    {
        sql.query(self).await
    }

    fn executor<'a>(&'a mut self) -> Self::InternalExecutor<'a> {
        &mut *self.internal
    }
}

impl<D: Driver> Deref for Connection<D> {
    type Target = <InternalDriver<D> as Database>::Connection;

    fn deref(&self) -> &Self::Target {
        &self.internal
    }
}

impl<D: Driver> DerefMut for Connection<D> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.internal
    }
}