Fixes #570 - Updated Postgres examples (#579)

* Fixes #570 - Updated Postgres examples

* Updated postgres crate to 0.17.2

Co-authored-by: Andrew Gauger <andygauge@gmail.com>
This commit is contained in:
Gabriel Ghiuzan 2020-04-18 23:54:01 +01:00 committed by GitHub
parent e8c27dc601
commit 7cce00a2c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 30 deletions

View file

@ -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"

View file

@ -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", &[])? {

View file

@ -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 (
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 (
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

View file

@ -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