diff --git a/lib/src/sql/idiom.rs b/lib/src/sql/idiom.rs index d34043b9..1a95a322 100644 --- a/lib/src/sql/idiom.rs +++ b/lib/src/sql/idiom.rs @@ -181,6 +181,12 @@ pub fn param(i: &str) -> IResult<&str, Idiom> { Ok((i, Idiom::from(v))) } +/// Used in a RELATE statement +pub fn plain(i: &str) -> IResult<&str, Idiom> { + let (i, p) = first(i)?; + Ok((i, Idiom::from(vec![p]))) +} + pub fn idiom(i: &str) -> IResult<&str, Idiom> { alt(( |i| { diff --git a/lib/src/sql/param.rs b/lib/src/sql/param.rs index 14a8c189..57dde85a 100644 --- a/lib/src/sql/param.rs +++ b/lib/src/sql/param.rs @@ -86,9 +86,9 @@ pub fn param(i: &str) -> IResult<&str, Param> { Ok((i, Param::from(v))) } -pub fn basic(i: &str) -> IResult<&str, Param> { +pub fn plain(i: &str) -> IResult<&str, Param> { let (i, _) = char('$')(i)?; - let (i, v) = idiom::basic(i)?; + let (i, v) = idiom::plain(i)?; Ok((i, Param::from(v))) } diff --git a/lib/src/sql/statements/relate.rs b/lib/src/sql/statements/relate.rs index 67a6d304..2d3bc457 100644 --- a/lib/src/sql/statements/relate.rs +++ b/lib/src/sql/statements/relate.rs @@ -12,7 +12,7 @@ use crate::sql::comment::shouldbespace; use crate::sql::data::{data, Data}; use crate::sql::error::IResult; use crate::sql::output::{output, Output}; -use crate::sql::param::basic as param; +use crate::sql::param::plain as param; use crate::sql::subquery::subquery; use crate::sql::table::{table, Table}; use crate::sql::thing::thing; diff --git a/lib/tests/relate.rs b/lib/tests/relate.rs new file mode 100644 index 00000000..6808c166 --- /dev/null +++ b/lib/tests/relate.rs @@ -0,0 +1,41 @@ +mod parse; +use parse::Parse; +use surrealdb::sql::Value; +use surrealdb::Datastore; +use surrealdb::Error; +use surrealdb::Session; + +#[tokio::test] +async fn relate_with_parameters() -> Result<(), Error> { + let sql = " + LET $tobie = person:tobie; + LET $jaime = person:jaime; + RELATE $tobie->knows->$jaime SET id = knows: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::None; + assert_eq!(tmp, val); + // + let tmp = res.remove(0).result?; + let val = Value::None; + assert_eq!(tmp, val); + // + let tmp = res.remove(0).result?; + let val = Value::parse( + "[ + { + id: knows:test, + in: person:tobie, + out: person:jaime, + } + ]", + ); + assert_eq!(tmp, val); + // + Ok(()) +}