79213676

Date: 2024-11-22 04:57:11
Score: 0.5
Natty:
Report link

Jofas from the Rust Forum found the solution that allows both outer and inner be compatible with the PgPool and PgConnection:

use sqlx::{Acquire, PgExecutor, PgPool, Postgres};

#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
    let pool = PgPool::connect("postgres:///").await?;

    let mut tx = pool.begin().await?;
    outer(&mut *tx).await?;
    tx.commit().await
}

async fn outer(db: impl Acquire<'_, Database = Postgres>) -> sqlx::Result<()> {
    let mut connection = db.acquire().await?;

    dbg!(inner(&mut *connection, "first").await?);
    dbg!(inner(&mut *connection, "second").await?);

    Ok(())
}

async fn inner(db: impl PgExecutor<'_>, name: &str) -> sqlx::Result<String> {
    sqlx::query_scalar!(r#"SELECT $1 as "name!""#, name)
        .fetch_one(db)
        .await
}
Reasons:
  • Contains signature (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
Posted by: imbolc