Skip to content

Commit

Permalink
remove date time wrappers
Browse files Browse the repository at this point in the history
+ add support for writing date time types as parameter values

Signed-off-by: Michelle Dhanani <michelle@fermyon.com>
Co-authored-by: Brian Hardock <brian.hardock@fermyon.com>
  • Loading branch information
michelleN and fibonacci1729 committed Nov 8, 2024
1 parent 38f006d commit a9a3ab5
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 59 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion examples/postgres-v3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ anyhow = "1"
http = "1.0.0"
# The Spin SDK.
spin-sdk = { path = "../.." }

# For handling date/time types
chrono = "0.4.38"
12 changes: 12 additions & 0 deletions examples/postgres-v3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,15 @@ date: Sun, 25 Sep 2022 15:46:22 GMT
Count: 3
```

Curl the write_datetime_info route to experiment with date time types:
```
$ curl -i localhost:3000/write_datetime_info
HTTP/1.1 200 OK
content-length: 9
date: Sun, 25 Sep 2022 15:46:22 GMT
Count: 4
```

Read endpoint should now also show a row with publisheddate, publishedtime, publisheddatetime and readtime values.
7 changes: 5 additions & 2 deletions examples/postgres-v3/db/testdata.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ CREATE TABLE articletest (
title varchar(40) NOT NULL,
content text NOT NULL,
authorname varchar(40) NOT NULL ,
published date NOT NULL,
publisheddate date NOT NULL,
publishedtime time,
publisheddatetime timestamp,
readtime bigint,
coauthor text
);

INSERT INTO articletest (title, content, authorname, published) VALUES
INSERT INTO articletest (title, content, authorname, publisheddate) VALUES
(
'My Life as a Goat',
'I went to Nepal to live as a goat, and it was much better than being a butler.',
Expand Down
50 changes: 41 additions & 9 deletions examples/postgres-v3/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#![allow(dead_code)]
use anyhow::Result;
use http::{Request, Response};
use spin_sdk::{
http_component, pg3,
pg3::{Date, Decode},
};
use spin_sdk::{http_component, pg3, pg3::Decode};

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

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

Ok(Self {
id,
title,
content,
authorname,
published,
published_date,
published_time,
published_datetime,
read_time,
coauthor,
})
}
Expand All @@ -47,6 +53,7 @@ fn process(req: Request<()>) -> Result<Response<String>> {
match req.uri().path() {
"/read" => read(req),
"/write" => write(req),
"/write_datetime_info" => write_datetime_info(req),
"/pg_backend_pid" => pg_backend_pid(req),
_ => Ok(http::Response::builder()
.status(404)
Expand All @@ -58,7 +65,7 @@ fn read(_req: Request<()>) -> Result<Response<String>> {
let address = std::env::var(DB_URL_ENV)?;
let conn = pg3::Connection::open(&address)?;

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

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

fn write_datetime_info(_req: Request<()>) -> Result<Response<String>> {
let address = std::env::var(DB_URL_ENV)?;
let conn = pg3::Connection::open(&address)?;

let date: chrono::NaiveDate = chrono::NaiveDate::from_ymd_opt(2024, 1, 1).unwrap();
let time: chrono::NaiveTime = chrono::NaiveTime::from_hms_nano_opt(12, 34, 56, 1).unwrap();
let datetime: chrono::NaiveDateTime = chrono::NaiveDateTime::new(date, time);
let readtime = 123i64;

let nrow_executed = conn.execute(
"INSERT INTO articletest(title, content, authorname, publisheddate, publishedtime, publisheddatetime, readtime) VALUES ($1, $2, $3, $4, $5, $6, $7)",
&[ "aaa".to_string().into(), "bbb".to_string().into(), "ccc".to_string().into(), date.into(), time.into(), datetime.into(), readtime.into() ],
);

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

let sql = "SELECT COUNT(id) FROM articletest";
let rowset = conn.query(sql, &[])?;
let row = &rowset.rows[0];
let count = i64::decode(&row[0])?;
let response = format!("Count: {}\n", count);

Ok(http::Response::builder().status(200).body(response)?)
}

fn write(_req: Request<()>) -> Result<Response<String>> {
let address = std::env::var(DB_URL_ENV)?;
let conn = pg3::Connection::open(&address)?;
Expand Down
Loading

0 comments on commit a9a3ab5

Please sign in to comment.