diff --git a/examples/postgres/db/testdata.sql b/examples/postgres/db/testdata.sql index 6e21a5a..809afec 100644 --- a/examples/postgres/db/testdata.sql +++ b/examples/postgres/db/testdata.sql @@ -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' ); diff --git a/examples/postgres/src/lib.rs b/examples/postgres/src/lib.rs index 94c4f6f..6eb6a40 100644 --- a/examples/postgres/src/lib.rs +++ b/examples/postgres/src/lib.rs @@ -17,6 +17,8 @@ struct Article { content: String, authorname: String, coauthor: Option, + authorage: Option, + authorheight: Option, } impl TryFrom<&pg::Row> for Article { @@ -28,6 +30,8 @@ impl TryFrom<&pg::Row> for Article { let content = String::decode(&row[2])?; let authorname = String::decode(&row[3])?; let coauthor = Option::::decode(&row[4])?; + let authorage = Option::::decode(&row[5])?; + let authorheight = Option::::decode(&row[6])?; Ok(Self { id, @@ -35,6 +39,8 @@ impl TryFrom<&pg::Row> for Article { content, authorname, coauthor, + authorage, + authorheight, }) } } @@ -55,7 +61,8 @@ fn read(_req: Request<()>) -> Result> { 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 @@ -90,7 +97,7 @@ fn write(_req: Request<()>) -> Result> { 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);