surrealpatch/src/sql/statements/relate.rs

180 lines
4.4 KiB
Rust
Raw Normal View History

2021-03-29 15:43:37 +00:00
use crate::dbs::Executor;
use crate::dbs::Iterator;
use crate::dbs::Level;
use crate::dbs::Options;
use crate::dbs::Runtime;
2021-03-29 15:43:37 +00:00
use crate::err::Error;
2020-06-29 15:36:01 +00:00
use crate::sql::comment::mightbespace;
use crate::sql::comment::shouldbespace;
use crate::sql::data::{data, Data};
2022-01-16 20:31:50 +00:00
use crate::sql::error::IResult;
2020-06-29 15:36:01 +00:00
use crate::sql::output::{output, Output};
use crate::sql::table::{table, Table};
use crate::sql::timeout::{timeout, Timeout};
use crate::sql::value::{whats, Value, Values};
2020-06-29 15:36:01 +00:00
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::bytes::complete::tag_no_case;
use nom::combinator::opt;
use nom::sequence::preceded;
use serde::{Deserialize, Serialize};
use std::fmt;
2021-03-29 15:43:37 +00:00
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
2020-06-29 15:36:01 +00:00
pub struct RelateStatement {
pub kind: Table,
pub from: Values,
pub with: Values,
2020-06-29 15:36:01 +00:00
pub uniq: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Data>,
#[serde(skip_serializing_if = "Option::is_none")]
pub output: Option<Output>,
#[serde(skip_serializing_if = "Option::is_none")]
pub timeout: Option<Timeout>,
}
2022-01-14 08:12:56 +00:00
impl RelateStatement {
pub async fn compute(
2021-03-29 15:43:37 +00:00
&self,
ctx: &Runtime,
2022-01-14 08:12:56 +00:00
opt: &Options<'_>,
exe: &mut Executor,
doc: Option<&Value>,
) -> Result<Value, Error> {
// Allowed to run?
exe.check(opt, Level::No)?;
2021-03-29 15:43:37 +00:00
// Create a new iterator
let mut i = Iterator::new();
// Ensure futures are stored
let opt = &opt.futures(false);
2021-03-29 15:43:37 +00:00
// Loop over the select targets
for f in self.from.0.iter() {
2022-01-14 08:12:56 +00:00
match f.compute(ctx, opt, exe, doc).await? {
Value::Table(v) => {
i.process_table(ctx, exe, v);
2021-03-29 15:43:37 +00:00
}
Value::Thing(v) => {
i.process_thing(ctx, exe, v);
2021-03-29 15:43:37 +00:00
}
Value::Model(v) => {
i.process_model(ctx, exe, v);
2021-03-29 15:43:37 +00:00
}
Value::Array(v) => {
i.process_array(ctx, exe, v);
2021-03-29 15:43:37 +00:00
}
Value::Object(v) => {
i.process_object(ctx, exe, v);
2021-03-29 15:43:37 +00:00
}
v => {
return Err(Error::RelateStatementError {
value: v,
})
2021-03-29 15:43:37 +00:00
}
};
}
// Output the results
i.output(ctx, exe)
}
}
2022-01-14 08:12:56 +00:00
impl fmt::Display for RelateStatement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "RELATE {} -> {} -> {}", self.from, self.kind, self.with)?;
if self.uniq {
write!(f, " UNIQUE")?
}
if let Some(ref v) = self.data {
write!(f, " {}", v)?
}
if let Some(ref v) = self.output {
write!(f, " {}", v)?
}
if let Some(ref v) = self.timeout {
write!(f, " {}", v)?
}
Ok(())
}
}
2020-06-29 15:36:01 +00:00
pub fn relate(i: &str) -> IResult<&str, RelateStatement> {
let (i, _) = tag_no_case("RELATE")(i)?;
let (i, _) = shouldbespace(i)?;
let (i, path) = alt((relate_o, relate_i))(i)?;
let (i, uniq) = opt(preceded(shouldbespace, tag_no_case("UNIQUE")))(i)?;
2020-06-29 15:36:01 +00:00
let (i, data) = opt(preceded(shouldbespace, data))(i)?;
let (i, output) = opt(preceded(shouldbespace, output))(i)?;
let (i, timeout) = opt(preceded(shouldbespace, timeout))(i)?;
Ok((
i,
RelateStatement {
kind: path.0,
from: path.1,
with: path.2,
uniq: uniq.is_some(),
data,
output,
timeout,
},
))
}
fn relate_o(i: &str) -> IResult<&str, (Table, Values, Values)> {
2020-06-29 15:36:01 +00:00
let (i, from) = whats(i)?;
let (i, _) = mightbespace(i)?;
let (i, _) = tag("->")(i)?;
let (i, _) = mightbespace(i)?;
let (i, kind) = table(i)?;
let (i, _) = mightbespace(i)?;
let (i, _) = tag("->")(i)?;
let (i, _) = mightbespace(i)?;
let (i, with) = whats(i)?;
Ok((i, (kind, from, with)))
}
fn relate_i(i: &str) -> IResult<&str, (Table, Values, Values)> {
2020-06-29 15:36:01 +00:00
let (i, with) = whats(i)?;
let (i, _) = mightbespace(i)?;
let (i, _) = tag("<-")(i)?;
let (i, _) = mightbespace(i)?;
let (i, kind) = table(i)?;
let (i, _) = mightbespace(i)?;
let (i, _) = tag("<-")(i)?;
let (i, _) = mightbespace(i)?;
let (i, from) = whats(i)?;
Ok((i, (kind, from, with)))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn relate_statement_in() {
let sql = "RELATE person->like->animal";
let res = relate(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("RELATE person -> like -> animal", format!("{}", out))
}
#[test]
fn relate_statement_out() {
let sql = "RELATE animal<-like<-person";
let res = relate(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("RELATE person -> like -> animal", format!("{}", out))
}
#[test]
fn relate_statement_thing() {
let sql = "RELATE person:tobie->like->person:jaime";
let res = relate(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("RELATE person:tobie -> like -> person:jaime", format!("{}", out))
}
2020-06-29 15:36:01 +00:00
}