Skip to content

Unable to abstract over Pool and Transaction #1442

Answered by abonander
Wopple asked this question in Q&A
Discussion options

You must be logged in to vote

Firstly, you can avoid the consumption of the &mut PgConnection if you reborrow:

pub async fn foo(conn: &mut PgConnection) -> Result<i32, sqlx::Error> {
    sqlx::query("<do something else>")
        .execute(&mut *conn)
        .await?;

    sqlx::query("SELECT 1 AS v")
        .map(|row: PgRow| row.get("v"))
        .fetch_one(&mut *conn)
        .await?
}

Secondly, if you use impl Acquire<Database = Postgres> (so don't constrict the Connection associated type) then you should be able to use either:

pub async fn foo(conn: impl Acquire<Database = Postgres>) -> Result<i32, sqlx::Error> {
    // no-op for `PgConnection`, `Transaction`
    let mut conn = conn.acquire().await?;

    sqlx::query

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@johannescpk
Comment options

Answer selected by Wopple
Comment options

You must be logged in to vote
1 reply
@Wopple
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants
Converted from issue

This discussion was converted from issue #1441 on September 17, 2021 21:00.