Skip to content

Don't consume ConnectionLike when executing queries #103

Closed
@nicoburns

Description

@nicoburns

I want to do something like the following:

async fn execute_queries(sql: String, rx: Receiver<Params>) {
    let conn = Conn::new(opts).await.unwrap();
    let stmt = conn.prepare(sql).await.unwrap();

    // Next query loop
    while let Some(params) = rx.recv().await {
        // Query retry loop
        loop {
            match stmt.execute(all_params).await {
                // Query suceeded.
                Ok(res) => break,

                // Query failed: wait 3 seconds then retry query
                Err(err @ SqlError::Driver(_)) | Err(err @ SqlError::Server(_)) => {
                    println!("Error: {}", err);
                    delay_for(Duration::from_secs(3)).await;
                    continue 'query_loop;
                }

                // Entire connection failed
                Err(err) => panic!("Placeholder for more sophisticated reconnect logic I have in actual code.")
            }
        }
    }
}

However, this is not possible because stmt.execute consumes stmt, and does not return it in the error case. In the case of success it is possible to reuse the prepared stmt, but it's awkward and involves reassigning the variable as implemented in the batch method.

Could we change execute and similar methods to take &mut self rather than self? This would make reusing connections/prepared statements possible in the case of errors, and make even the success case more ergonomic.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions