From 745c5c39577a238932127b50aea873bcb92222c4 Mon Sep 17 00:00:00 2001 From: Nicholas Connor Date: Sat, 1 Feb 2020 02:30:08 -0500 Subject: [PATCH] Updated the README example with acquire connection (#99) * Updated the README example with acquire connection Initially from reading the docs and examples I tried to use `&mut pool` instead of `&mut conn`. The compiler gave me an error that `Pool` didn't implement `Executor`. I had to do a bit of digging and eventually just viewed the source of `Pool` to find `acquire()`, `try_acquire()` etc. I think this change makes it a bit easier for someone to get started. * Update README.md to reference initial pool declaration * Fixed compile issues and added examples of using &mut &pool --- README.md | 7 +++---- examples/todos-postgres/src/main.rs | 30 +++++++++++------------------ 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 0f3cd85c..1b2346c5 100644 --- a/README.md +++ b/README.md @@ -88,8 +88,7 @@ sqlx = { version = "0.2", default-features = false, features = [ "runtime-tokio" #### Connect -It is a very good idea to always create a connection pool at the beginning of your application -and then share that. +It is a very good idea to always create a connection pool at the beginning of your application and then share that. ```rust // Postgres @@ -104,7 +103,7 @@ The result is an implementation of the `Row` trait. Values can be efficiently ac ```rust let row = sqlx::query("SELECT is_active FROM users WHERE id = ?") .bind(some_user_id) - .fetch_one(&mut conn) + .fetch_one(&mut &pool) .await?; let is_active: bool = row.get("is_active"); @@ -120,7 +119,7 @@ let countries = sqlx::query!( "SELECT country, COUNT(*) FROM users GROUP BY country WHERE organization = ?", organization ) - .fetch(&mut conn) // -> impl Stream + .fetch(&mut &pool) // -> impl Stream .map_ok(|rec| (rec.country, rec.count)) .try_collect::>() // -> HashMap .await?; diff --git a/examples/todos-postgres/src/main.rs b/examples/todos-postgres/src/main.rs index 122bba83..918c6bad 100644 --- a/examples/todos-postgres/src/main.rs +++ b/examples/todos-postgres/src/main.rs @@ -22,7 +22,7 @@ async fn main(args: Args) -> anyhow::Result<()> { match args.cmd { Some(Command::Add { description }) => { println!("Adding new todo with description '{}'", &description); - let todo_id = add_todo(&pool, &description).await?; + let todo_id = add_todo(&pool, description).await?; println!("Added new todo with id {}", todo_id); } Some(Command::Done { id }) => { @@ -42,51 +42,43 @@ async fn main(args: Args) -> anyhow::Result<()> { Ok(()) } -async fn add_todo(pool: &PgPool, description: &str) -> anyhow::Result { - let mut tx = pool.begin().await?; - +async fn add_todo(mut pool: &PgPool, description: String) -> anyhow::Result { let rec = sqlx::query!( - " + r#" INSERT INTO todos ( description ) VALUES ( $1 ) RETURNING id - ", + "#, description ) - .fetch_one(&mut tx) + .fetch_one(&mut pool) .await?; - tx.commit().await?; - Ok(rec.id) } -async fn complete_todo(pool: &PgPool, id: i64) -> anyhow::Result { - let mut tx = pool.begin().await?; - +async fn complete_todo(mut pool: &PgPool, id: i64) -> anyhow::Result { let rows_affected = sqlx::query!( - " + r#" UPDATE todos SET done = TRUE WHERE id = $1 - ", + "#, id ) - .execute(&mut tx) + .execute(&mut pool) .await?; - tx.commit().await?; - Ok(rows_affected > 0) } async fn list_todos(pool: &mut PgPool) -> anyhow::Result<()> { let recs = sqlx::query!( - " + r#" SELECT id, description, done FROM todos ORDER BY id - " + "# ) .fetch_all(pool) .await?;