Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions examples/postgres/db/testdata.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ CREATE TABLE articletest (
title varchar(40) NOT NULL,
content text NOT NULL,
authorname varchar(40) NOT NULL,
coauthor text
coauthor text,
authorage real,
authorheight double precision
);

INSERT INTO articletest (title, content, authorname) VALUES
INSERT INTO articletest (title, content, authorname, authorage, authorheight) VALUES
(
'My Life as a Goat',
'I went to Nepal to live as a goat, and it was much better than being a butler.',
'E. Blackadder'
'E. Blackadder',
'22.1',
'72.23'
),
(
'Magnificent Octopus',
'Once upon a time there was a lovely little sausage.',
'S. Baldrick'
'S. Baldrick',
'11.2',
'50.48'
);
11 changes: 9 additions & 2 deletions examples/postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ struct Article {
content: String,
authorname: String,
coauthor: Option<String>,
authorage: Option<f32>,
authorheight: Option<f64>,
}

impl TryFrom<&pg::Row> for Article {
Expand All @@ -28,13 +30,17 @@ impl TryFrom<&pg::Row> for Article {
let content = String::decode(&row[2])?;
let authorname = String::decode(&row[3])?;
let coauthor = Option::<String>::decode(&row[4])?;
let authorage = Option::<f32>::decode(&row[5])?;
let authorheight = Option::<f64>::decode(&row[6])?;

Ok(Self {
id,
title,
content,
authorname,
coauthor,
authorage,
authorheight,
})
}
}
Expand All @@ -55,7 +61,8 @@ fn read(_req: Request<()>) -> Result<Response<String>> {
let address = std::env::var(DB_URL_ENV)?;
let conn = pg::Connection::open(&address)?;

let sql = "SELECT id, title, content, authorname, coauthor FROM articletest";
let sql =
"SELECT id, title, content, authorname, coauthor, authorage, authorheight FROM articletest";
let rowset = conn.query(sql, &[])?;

let column_summary = rowset
Expand Down Expand Up @@ -90,7 +97,7 @@ fn write(_req: Request<()>) -> Result<Response<String>> {
let address = std::env::var(DB_URL_ENV)?;
let conn = pg::Connection::open(&address)?;

let sql = "INSERT INTO articletest (title, content, authorname) VALUES ('aaa', 'bbb', 'ccc')";
let sql = "INSERT INTO articletest (title, content, authorname, authorage, authorheight) VALUES ('aaa', 'bbb', 'ccc', 1.1, 2.22)";
let nrow_executed = conn.execute(sql, &[])?;

println!("nrow_executed: {}", nrow_executed);
Expand Down
Loading