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<MySql>` 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
This commit is contained in:
Nicholas Connor 2020-02-01 02:30:08 -05:00 committed by GitHub
parent bdfea1a3cb
commit 745c5c3957
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 23 deletions

View file

@ -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<Item = { country: String, count: i64 }>
.fetch(&mut &pool) // -> impl Stream<Item = { country: String, count: i64 }>
.map_ok(|rec| (rec.country, rec.count))
.try_collect::<HashMap<_>>() // -> HashMap<String, i64>
.await?;

View file

@ -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<i64> {
let mut tx = pool.begin().await?;
async fn add_todo(mut pool: &PgPool, description: String) -> anyhow::Result<i64> {
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<bool> {
let mut tx = pool.begin().await?;
async fn complete_todo(mut pool: &PgPool, id: i64) -> anyhow::Result<bool> {
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?;