Don’t automatically treat naive dates as datetimes

Closes #1328
This commit is contained in:
Tobie Morgan Hitchcock 2022-10-17 03:26:32 +01:00
parent eca8cd8d1d
commit 8902fc0a70
5 changed files with 65 additions and 20 deletions

View file

@ -37,7 +37,7 @@ impl From<DateTime<Utc>> 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<Self> 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";

View file

@ -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;

View file

@ -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));
}
}

45
lib/tests/datetimes.rs Normal file
View file

@ -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 <datetime> "2012-01-01";
SELECT * FROM <string> "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(())
}

View file

@ -9,8 +9,8 @@ use surrealdb::Session;
async fn future_function_simple() -> Result<(), Error> {
let sql = "
UPDATE person:test SET can_drive = <future> { 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 = <datetime> '2007-06-22';
UPDATE person:test SET birthday = <datetime> '2001-06-22';
";
let dbs = Datastore::new("memory").await?;
let ses = Session::for_kv().with_ns("test").with_db("test");