From 8902fc0a70eceddbd3f8382ae0b9510c37b98058 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Mon, 17 Oct 2022 03:26:32 +0100 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20automatically=20treat=20naive?= =?UTF-8?q?=20dates=20as=20datetimes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #1328 --- lib/src/sql/datetime.rs | 30 +++++++++++++-------------- lib/src/sql/parser.rs | 2 +- lib/src/sql/version.rs | 4 ++-- lib/tests/datetimes.rs | 45 +++++++++++++++++++++++++++++++++++++++++ lib/tests/future.rs | 4 ++-- 5 files changed, 65 insertions(+), 20 deletions(-) create mode 100644 lib/tests/datetimes.rs diff --git a/lib/src/sql/datetime.rs b/lib/src/sql/datetime.rs index 91eb11d6..f51e7a44 100644 --- a/lib/src/sql/datetime.rs +++ b/lib/src/sql/datetime.rs @@ -37,7 +37,7 @@ impl From> for Datetime { impl From<&str> for Datetime { fn from(s: &str) -> Self { - match datetime_raw(s) { + match datetime_all_raw(s) { Ok((_, v)) => v, Err(_) => Self::default(), } @@ -87,14 +87,23 @@ impl ops::Sub for Datetime { } pub fn datetime(i: &str) -> IResult<&str, Datetime> { - alt(( - delimited(char('\''), datetime_raw, char('\'')), - delimited(char('\"'), datetime_raw, char('\"')), - ))(i) + alt((datetime_single, datetime_double))(i) +} + +fn datetime_single(i: &str) -> IResult<&str, Datetime> { + delimited(char('\''), datetime_raw, char('\''))(i) +} + +fn datetime_double(i: &str) -> IResult<&str, Datetime> { + delimited(char('\"'), datetime_raw, char('\"'))(i) +} + +fn datetime_all_raw(i: &str) -> IResult<&str, Datetime> { + alt((nano, time, date))(i) } fn datetime_raw(i: &str) -> IResult<&str, Datetime> { - alt((nano, time, date))(i) + alt((nano, time))(i) } fn date(i: &str) -> IResult<&str, Datetime> { @@ -247,15 +256,6 @@ mod tests { use super::*; - #[test] - fn date() { - let sql = "2012-04-23"; - let res = datetime_raw(sql); - assert!(res.is_ok()); - let out = res.unwrap().1; - assert_eq!("\"2012-04-23T00:00:00Z\"", format!("{}", out)); - } - #[test] fn date_time() { let sql = "2012-04-23T18:25:43Z"; diff --git a/lib/src/sql/parser.rs b/lib/src/sql/parser.rs index a29cf47a..205269d5 100644 --- a/lib/src/sql/parser.rs +++ b/lib/src/sql/parser.rs @@ -134,7 +134,7 @@ mod tests { AND ages CONTAINS 18 AND if IS true SPLIT test.things - VERSION '2019-01-01' + VERSION '2019-01-01T08:00:00Z' TIMEOUT 2w; CREATE person SET name = 'Tobie', age += 18; diff --git a/lib/src/sql/version.rs b/lib/src/sql/version.rs index aa93190b..d87946dd 100644 --- a/lib/src/sql/version.rs +++ b/lib/src/sql/version.rs @@ -28,11 +28,11 @@ mod tests { #[test] fn version_statement() { - let sql = "VERSION '2020-01-01'"; + let sql = "VERSION '2020-01-01T00:00:00Z'"; let res = version(sql); assert!(res.is_ok()); let out = res.unwrap().1; - assert_eq!(out, Version(Datetime::from("2020-01-01"))); + assert_eq!(out, Version(Datetime::from("2020-01-01T00:00:00Z"))); assert_eq!("VERSION \"2020-01-01T00:00:00Z\"", format!("{}", out)); } } diff --git a/lib/tests/datetimes.rs b/lib/tests/datetimes.rs new file mode 100644 index 00000000..41cef747 --- /dev/null +++ b/lib/tests/datetimes.rs @@ -0,0 +1,45 @@ +mod parse; +use parse::Parse; +use surrealdb::sql::Value; +use surrealdb::Datastore; +use surrealdb::Error; +use surrealdb::Session; + +#[tokio::test] +async fn datetimes_conversion() -> Result<(), Error> { + let sql = r#" + SELECT * FROM "2012-01-01"; + SELECT * FROM "2012-01-01"; + SELECT * FROM "2012-01-01T08:00:00Z" + "-test"; + "#; + let dbs = Datastore::new("memory").await?; + let ses = Session::for_kv().with_ns("test").with_db("test"); + let res = &mut dbs.execute(&sql, &ses, None, false).await?; + assert_eq!(res.len(), 3); + // + let tmp = res.remove(0).result?; + let val = Value::parse( + "[ + '2012-01-01' + ]", + ); + assert_eq!(tmp, val); + // + let tmp = res.remove(0).result?; + let val = Value::parse( + "[ + '2012-01-01T00:00:00Z' + ]", + ); + assert_eq!(tmp, val); + // + let tmp = res.remove(0).result?; + let val = Value::parse( + "[ + '2012-01-01T08:00:00Z-test' + ]", + ); + assert_eq!(tmp, val); + // + Ok(()) +} diff --git a/lib/tests/future.rs b/lib/tests/future.rs index c7832e2f..c1a0d9bc 100644 --- a/lib/tests/future.rs +++ b/lib/tests/future.rs @@ -9,8 +9,8 @@ use surrealdb::Session; async fn future_function_simple() -> Result<(), Error> { let sql = " UPDATE person:test SET can_drive = { birthday && time::now() > birthday + 18y }; - UPDATE person:test SET birthday = '2007-06-22'; - UPDATE person:test SET birthday = '2001-06-22'; + UPDATE person:test SET birthday = '2007-06-22'; + UPDATE person:test SET birthday = '2001-06-22'; "; let dbs = Datastore::new("memory").await?; let ses = Session::for_kv().with_ns("test").with_db("test");