Skip to content

Commit f324f57

Browse files
remove date time wrappers
+ add support for writing date time types as parameter values Signed-off-by: Michelle Dhanani <[email protected]> Co-authored-by: Brian Hardock <[email protected]>
1 parent 38f006d commit f324f57

File tree

6 files changed

+198
-59
lines changed

6 files changed

+198
-59
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/postgres-v3/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ anyhow = "1"
1313
http = "1.0.0"
1414
# The Spin SDK.
1515
spin-sdk = { path = "../.." }
16-
16+
# For handling date/time types
17+
chrono = "0.4.38"

examples/postgres-v3/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,15 @@ date: Sun, 25 Sep 2022 15:46:22 GMT
5555
5656
Count: 3
5757
```
58+
59+
Curl the write_datetime_info route to experiment with date time types:
60+
```
61+
$ curl -i localhost:3000/write_datetime_info
62+
HTTP/1.1 200 OK
63+
content-length: 9
64+
date: Sun, 25 Sep 2022 15:46:22 GMT
65+
66+
Count: 4
67+
```
68+
69+
Read endpoint should now also show a row with publisheddate, publishedtime, publisheddatetime and readtime values.

examples/postgres-v3/db/testdata.sql

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ CREATE TABLE articletest (
33
title varchar(40) NOT NULL,
44
content text NOT NULL,
55
authorname varchar(40) NOT NULL ,
6-
published date NOT NULL,
6+
publisheddate date NOT NULL,
7+
publishedtime time,
8+
publisheddatetime timestamp,
9+
readtime bigint,
710
coauthor text
811
);
912

10-
INSERT INTO articletest (title, content, authorname, published) VALUES
13+
INSERT INTO articletest (title, content, authorname, publisheddate) VALUES
1114
(
1215
'My Life as a Goat',
1316
'I went to Nepal to live as a goat, and it was much better than being a butler.',

examples/postgres-v3/src/lib.rs

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
#![allow(dead_code)]
22
use anyhow::Result;
33
use http::{Request, Response};
4-
use spin_sdk::{
5-
http_component, pg3,
6-
pg3::{Date, Decode},
7-
};
4+
use spin_sdk::{http_component, pg3, pg3::Decode};
85

96
// The environment variable set in `spin.toml` that points to the
107
// address of the Pg server that the component will write to
@@ -16,7 +13,10 @@ struct Article {
1613
title: String,
1714
content: String,
1815
authorname: String,
19-
published: Date,
16+
published_date: chrono::NaiveDate,
17+
published_time: Option<chrono::NaiveTime>,
18+
published_datetime: Option<chrono::NaiveDateTime>,
19+
read_time: Option<i64>,
2020
coauthor: Option<String>,
2121
}
2222

@@ -28,15 +28,21 @@ impl TryFrom<&pg3::Row> for Article {
2828
let title = String::decode(&row[1])?;
2929
let content = String::decode(&row[2])?;
3030
let authorname = String::decode(&row[3])?;
31-
let published = Date::decode(&row[4])?;
32-
let coauthor = Option::<String>::decode(&row[5])?;
31+
let published_date = chrono::NaiveDate::decode(&row[4])?;
32+
let published_time = Option::<chrono::NaiveTime>::decode(&row[5])?;
33+
let published_datetime = Option::<chrono::NaiveDateTime>::decode(&row[6])?;
34+
let read_time = Option::<i64>::decode(&row[7])?;
35+
let coauthor = Option::<String>::decode(&row[8])?;
3336

3437
Ok(Self {
3538
id,
3639
title,
3740
content,
3841
authorname,
39-
published,
42+
published_date,
43+
published_time,
44+
published_datetime,
45+
read_time,
4046
coauthor,
4147
})
4248
}
@@ -47,6 +53,7 @@ fn process(req: Request<()>) -> Result<Response<String>> {
4753
match req.uri().path() {
4854
"/read" => read(req),
4955
"/write" => write(req),
56+
"/write_datetime_info" => write_datetime_info(req),
5057
"/pg_backend_pid" => pg_backend_pid(req),
5158
_ => Ok(http::Response::builder()
5259
.status(404)
@@ -58,7 +65,7 @@ fn read(_req: Request<()>) -> Result<Response<String>> {
5865
let address = std::env::var(DB_URL_ENV)?;
5966
let conn = pg3::Connection::open(&address)?;
6067

61-
let sql = "SELECT id, title, content, authorname, published, coauthor FROM articletest";
68+
let sql = "SELECT id, title, content, authorname, publisheddate, publishedtime, publisheddatetime, readtime, coauthor FROM articletest";
6269
let rowset = conn.query(sql, &[])?;
6370

6471
let column_summary = rowset
@@ -89,6 +96,31 @@ fn read(_req: Request<()>) -> Result<Response<String>> {
8996
Ok(http::Response::builder().status(200).body(response)?)
9097
}
9198

99+
fn write_datetime_info(_req: Request<()>) -> Result<Response<String>> {
100+
let address = std::env::var(DB_URL_ENV)?;
101+
let conn = pg3::Connection::open(&address)?;
102+
103+
let date: chrono::NaiveDate = chrono::NaiveDate::from_ymd_opt(2024, 1, 1).unwrap();
104+
let time: chrono::NaiveTime = chrono::NaiveTime::from_hms_nano_opt(12, 34, 56, 1).unwrap();
105+
let datetime: chrono::NaiveDateTime = chrono::NaiveDateTime::new(date, time);
106+
let readtime = 123i64;
107+
108+
let nrow_executed = conn.execute(
109+
"INSERT INTO articletest(title, content, authorname, publisheddate, publishedtime, publisheddatetime, readtime) VALUES ($1, $2, $3, $4, $5, $6, $7)",
110+
&[ "aaa".to_string().into(), "bbb".to_string().into(), "ccc".to_string().into(), date.into(), time.into(), datetime.into(), readtime.into() ],
111+
);
112+
113+
println!("nrow_executed: {:?}", nrow_executed);
114+
115+
let sql = "SELECT COUNT(id) FROM articletest";
116+
let rowset = conn.query(sql, &[])?;
117+
let row = &rowset.rows[0];
118+
let count = i64::decode(&row[0])?;
119+
let response = format!("Count: {}\n", count);
120+
121+
Ok(http::Response::builder().status(200).body(response)?)
122+
}
123+
92124
fn write(_req: Request<()>) -> Result<Response<String>> {
93125
let address = std::env::var(DB_URL_ENV)?;
94126
let conn = pg3::Connection::open(&address)?;

0 commit comments

Comments
 (0)