Skip to content

Function that takes transaction or connection as argument #310

Closed Answered by blackbeam
kulak asked this question in Q&A
Discussion options

You must be logged in to vote

Hi. Please look at the following code snippet:

pub async fn archive(conn: &mut impl Queryable, id: i32) -> crate::Result<bool> {
                  // ^^^^^^^^^^^^^^^^^^^^^^^^^ this works for both &mut Conn and &mut Transaction
    const QUERY: &str = "UPDATE `ReadingRooms` \
        SET `is_archived` = NOT `is_archived` WHERE `id` = ?";
    let affected_rows = conn.exec_iter(QUERY, (id,)).await?.affected_rows();
    Ok(affected_rows > 0)
}

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@kulak
Comment options

@kulak
Comment options

@blackbeam
Comment options

Answer selected by kulak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants