Skip to content

Commit b6b371b

Browse files
Merge pull request #45 from michelleN/postgresfloats
add floats to postgres example
2 parents 97803c5 + 51bc7df commit b6b371b

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

examples/postgres/db/testdata.sql

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@ CREATE TABLE articletest (
33
title varchar(40) NOT NULL,
44
content text NOT NULL,
55
authorname varchar(40) NOT NULL,
6-
coauthor text
6+
coauthor text,
7+
authorage real,
8+
authorheight double precision
79
);
810

9-
INSERT INTO articletest (title, content, authorname) VALUES
11+
INSERT INTO articletest (title, content, authorname, authorage, authorheight) VALUES
1012
(
1113
'My Life as a Goat',
1214
'I went to Nepal to live as a goat, and it was much better than being a butler.',
13-
'E. Blackadder'
15+
'E. Blackadder',
16+
'22.1',
17+
'72.23'
1418
),
1519
(
1620
'Magnificent Octopus',
1721
'Once upon a time there was a lovely little sausage.',
18-
'S. Baldrick'
22+
'S. Baldrick',
23+
'11.2',
24+
'50.48'
1925
);

examples/postgres/src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ struct Article {
1717
content: String,
1818
authorname: String,
1919
coauthor: Option<String>,
20+
authorage: Option<f32>,
21+
authorheight: Option<f64>,
2022
}
2123

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

3236
Ok(Self {
3337
id,
3438
title,
3539
content,
3640
authorname,
3741
coauthor,
42+
authorage,
43+
authorheight,
3844
})
3945
}
4046
}
@@ -55,7 +61,8 @@ fn read(_req: Request<()>) -> Result<Response<String>> {
5561
let address = std::env::var(DB_URL_ENV)?;
5662
let conn = pg::Connection::open(&address)?;
5763

58-
let sql = "SELECT id, title, content, authorname, coauthor FROM articletest";
64+
let sql =
65+
"SELECT id, title, content, authorname, coauthor, authorage, authorheight FROM articletest";
5966
let rowset = conn.query(sql, &[])?;
6067

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

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

96103
println!("nrow_executed: {}", nrow_executed);

0 commit comments

Comments
 (0)