Trait sqlx::Acquire

source ยท
pub trait Acquire<'c> {
    type Database: Database;
    type Connection: Deref<Target = <Self::Database as Database>::Connection> + DerefMut + Send;

    // Required methods
    fn acquire(
        self
    ) -> Pin<Box<dyn Future<Output = Result<Self::Connection, Error>> + Send + 'c, Global>>;
    fn begin(
        self
    ) -> Pin<Box<dyn Future<Output = Result<Transaction<'c, Self::Database>, Error>> + Send + 'c, Global>>;
}
Expand description

Acquire connections or transactions from a database in a generic way.

If you want to accept generic database connections that implement Acquire which then allows you to acquire a connection or begin a transaction, then you can do it like that:

async fn run_query<'a, A>(conn: A) -> Result<(), BoxDynError>
where
    A: Acquire<'a, Database = Postgres>,
{
    let mut conn = conn.acquire().await?;

    sqlx::query!("SELECT 1 as v").fetch_one(&mut *conn).await?;
    sqlx::query!("SELECT 2 as v").fetch_one(&mut *conn).await?;

    Ok(())
}

If you run into a lifetime error about โ€œimplementation of sqlx::Acquire is not general enoughโ€, the workaround looks like this:

fn run_query<'a, 'c, A>(conn: A) -> impl Future<Output = Result<(), BoxDynError>> + Send + 'a
where
    A: Acquire<'c, Database = Postgres> + Send + 'a,
{
    async move {
        let mut conn = conn.acquire().await?;

        sqlx::query!("SELECT 1 as v").fetch_one(&mut *conn).await?;
        sqlx::query!("SELECT 2 as v").fetch_one(&mut *conn).await?;

        Ok(())
    }
}

However, if you really just want to accept both, a transaction or a connection as an argument to a function, then itโ€™s easier to just accept a mutable reference to a database connection like so:

async fn run_query(conn: &mut PgConnection) -> Result<(), BoxDynError> {
    sqlx::query!("SELECT 1 as v").fetch_one(&mut *conn).await?;
    sqlx::query!("SELECT 2 as v").fetch_one(&mut *conn).await?;

    Ok(())
}

The downside of this approach is that you have to acquire a connection from a pool first and canโ€™t directly pass the pool as argument.

Required Associated Typesยง

Required Methodsยง

source

fn acquire( self ) -> Pin<Box<dyn Future<Output = Result<Self::Connection, Error>> + Send + 'c, Global>>

source

fn begin( self ) -> Pin<Box<dyn Future<Output = Result<Transaction<'c, Self::Database>, Error>> + Send + 'c, Global>>

Implementorsยง

sourceยง

impl<'a, DB> Acquire<'a> for &Pool<DB>where DB: Database,

sourceยง

impl<'c> Acquire<'c> for &'c mut PoolConnection<Any>

sourceยง

impl<'c> Acquire<'c> for &'c mut PoolConnection<Mssql>

sourceยง

impl<'c> Acquire<'c> for &'c mut PoolConnection<MySql>

sourceยง

impl<'c> Acquire<'c> for &'c mut PoolConnection<Postgres>

sourceยง

impl<'c> Acquire<'c> for &'c mut PoolConnection<Sqlite>

sourceยง

impl<'c> Acquire<'c> for &'c mut AnyConnection

sourceยง

impl<'c> Acquire<'c> for &'c mut MssqlConnection

sourceยง

impl<'c> Acquire<'c> for &'c mut MySqlConnection

sourceยง

impl<'c> Acquire<'c> for &'c mut PgConnection

sourceยง

impl<'c> Acquire<'c> for &'c mut SqliteConnection

sourceยง

impl<'c, 't> Acquire<'t> for &'t mut Transaction<'c, Any>

sourceยง

impl<'c, 't> Acquire<'t> for &'t mut Transaction<'c, Mssql>

sourceยง

impl<'c, 't> Acquire<'t> for &'t mut Transaction<'c, MySql>

sourceยง

impl<'c, 't> Acquire<'t> for &'t mut Transaction<'c, Postgres>

sourceยง

impl<'c, 't> Acquire<'t> for &'t mut Transaction<'c, Sqlite>