From 7cce00a2c3c4451455f40158b0543c38e33d64a2 Mon Sep 17 00:00:00 2001 From: Gabriel Ghiuzan Date: Sat, 18 Apr 2020 23:54:01 +0100 Subject: [PATCH] Fixes #570 - Updated Postgres examples (#579) * Fixes #570 - Updated Postgres examples * Updated postgres crate to 0.17.2 Co-authored-by: Andrew Gauger --- Cargo.toml | 2 +- src/database/postgres/aggregate_data.md | 8 ++--- src/database/postgres/create_tables.md | 35 ++++++++++++---------- src/database/postgres/insert_query_data.md | 20 +++++++------ 4 files changed, 35 insertions(+), 30 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index aacb2f7..efc2817 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,7 @@ num = "0.2" num_cpus = "1.8" percent-encoding = "2.1" petgraph = "0.4" -postgres = "0.15" +postgres = "0.17.2" rand = "0.7.3" rand_distr = "0.2.2" rayon = "1.0" diff --git a/src/database/postgres/aggregate_data.md b/src/database/postgres/aggregate_data.md index 407aa04..1168732 100644 --- a/src/database/postgres/aggregate_data.md +++ b/src/database/postgres/aggregate_data.md @@ -6,7 +6,7 @@ This recipe lists the nationalities of the first 7999 artists in the database of ```rust,no_run extern crate postgres; -use postgres::{Connection, Error, TlsMode}; +use postgres::{Client, Error, NoTls}; struct Nation { nationality: String, @@ -14,12 +14,12 @@ struct Nation { } fn main() -> Result<(), Error> { - let conn = Connection::connect( + let mut client = Client::connect( "postgresql://postgres:postgres@127.0.0.1/moma", - TlsMode::None, + NoTls, )?; - for row in &conn.query + for row in client.query ("SELECT nationality, COUNT(nationality) AS count FROM artists GROUP BY nationality ORDER BY count DESC", &[])? { diff --git a/src/database/postgres/create_tables.md b/src/database/postgres/create_tables.md index 2909f3d..72ca745 100644 --- a/src/database/postgres/create_tables.md +++ b/src/database/postgres/create_tables.md @@ -4,33 +4,36 @@ Use the [`postgres`] crate to create tables in a Postgres database. -[`Connection::connect`] helps in connecting to an existing database. The recipe uses a URL string format with `Connection::connect`. It assumes an existing database named `library`, the username is `postgres` and the password is `postgres`. +[`Client::connect`] helps in connecting to an existing database. The recipe uses a URL string format with `Client::connect`. It assumes an existing database named `library`, the username is `postgres` and the password is `postgres`. ```rust,no_run extern crate postgres; -use postgres::{Connection, TlsMode, Error}; +use postgres::{Client, NoTls, Error}; fn main() -> Result<(), Error> { - let conn = Connection::connect("postgresql://postgres:postgres@localhost/library", - TlsMode::None)?; + let mut client = Client::connect("postgresql://postgres:postgres@localhost/library", NoTls)?; - conn.execute("CREATE TABLE IF NOT EXISTS author ( - id SERIAL PRIMARY KEY, - name VARCHAR NOT NULL, - country VARCHAR NOT NULL - )", &[])?; + client.batch_execute(" + CREATE TABLE IF NOT EXISTS author ( + id SERIAL PRIMARY KEY, + name VARCHAR NOT NULL, + country VARCHAR NOT NULL + ) + ")?; - conn.execute("CREATE TABLE IF NOT EXISTS book ( - id SERIAL PRIMARY KEY, - title VARCHAR NOT NULL, - author_id INTEGER NOT NULL REFERENCES author - )", &[])?; + client.batch_execute(" + CREATE TABLE IF NOT EXISTS book ( + id SERIAL PRIMARY KEY, + title VARCHAR NOT NULL, + author_id INTEGER NOT NULL REFERENCES author + ) + ")?; Ok(()) } ``` -[`postgres`]: https://docs.rs/postgres/0.15.2/postgres/ -[`Connection::connect`]: https://docs.rs/postgres/0.15.2/postgres/struct.Connection.html#method.connect +[`postgres`]: https://docs.rs/postgres/0.17.2/postgres/ +[`Client::connect`]: https://docs.rs/postgres/0.17.2/postgres/struct.Client.html#method.connect diff --git a/src/database/postgres/insert_query_data.md b/src/database/postgres/insert_query_data.md index 44e2084..cc4e7d4 100644 --- a/src/database/postgres/insert_query_data.md +++ b/src/database/postgres/insert_query_data.md @@ -2,12 +2,12 @@ [![postgres-badge]][postgres] [![cat-database-badge]][cat-database] -The recipe inserts data into the `author` table using [`execute`] method of `Connection`. Then, displays the data from the `author` table using [`query`] method of `Connection`. +The recipe inserts data into the `author` table using [`execute`] method of `Client`. Then, displays the data from the `author` table using [`query`] method of `Client`. ```rust,no_run extern crate postgres; -use postgres::{Connection, TlsMode, Error}; +use postgres::{Client, NoTls, Error}; use std::collections::HashMap; struct Author { @@ -17,8 +17,8 @@ struct Author { } fn main() -> Result<(), Error> { - let conn = Connection::connect("postgresql://postgres:postgres@localhost/library", - TlsMode::None)?; + let mut client = Client::connect("postgresql://postgres:postgres@localhost/library", + NoTls)?; let mut authors = HashMap::new(); authors.insert(String::from("Chinua Achebe"), "Nigeria"); @@ -32,11 +32,13 @@ fn main() -> Result<(), Error> { country: value.to_string() }; - conn.execute("INSERT INTO author (name, country) VALUES ($1, $2)", - &[&author.name, &author.country])?; + client.execute( + "INSERT INTO author (name, country) VALUES ($1, $2)", + &[&author.name, &author.country], + )?; } - for row in &conn.query("SELECT id, name, country FROM author", &[])? { + for row in client.query("SELECT id, name, country FROM author", &[])? { let author = Author { _id: row.get(0), name: row.get(1), @@ -50,5 +52,5 @@ fn main() -> Result<(), Error> { } ``` -[`execute`]: https://docs.rs/postgres/0.15.2/postgres/struct.Connection.html#method.execute -[`query`]: https://docs.rs/postgres/0.15.2/postgres/struct.Connection.html#method.query +[`execute`]: https://docs.rs/postgres/0.17.2/postgres/struct.Client.html#method.execute +[`query`]: https://docs.rs/postgres/0.17.2/postgres/struct.Client.html#method.query