2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2023-07-06 14:57:42 +00:00
|
|
|
use crate::dbs::{Options, Transaction};
|
|
|
|
use crate::doc::CursorDoc;
|
2021-03-29 15:43:37 +00:00
|
|
|
use crate::err::Error;
|
2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::common::commas;
|
2022-01-16 20:31:50 +00:00
|
|
|
use crate::sql::error::IResult;
|
2023-01-19 09:53:33 +00:00
|
|
|
use crate::sql::fmt::{fmt_separated_by, Fmt};
|
2023-08-29 10:50:00 +00:00
|
|
|
use crate::sql::part::{basic_part, first, graph, local_part, part, Part};
|
2023-08-01 07:30:13 +00:00
|
|
|
use crate::sql::part::{flatten, Next};
|
2023-04-16 17:17:44 +00:00
|
|
|
use crate::sql::paths::{ID, IN, META, OUT};
|
2022-01-13 17:36:41 +00:00
|
|
|
use crate::sql::value::Value;
|
2022-07-16 22:23:13 +00:00
|
|
|
use md5::Digest;
|
|
|
|
use md5::Md5;
|
2022-01-13 17:36:41 +00:00
|
|
|
use nom::branch::alt;
|
2023-08-01 07:30:13 +00:00
|
|
|
use nom::combinator::opt;
|
2021-03-29 15:43:37 +00:00
|
|
|
use nom::multi::separated_list1;
|
2022-06-09 08:18:08 +00:00
|
|
|
use nom::multi::{many0, many1};
|
2023-08-17 18:03:46 +00:00
|
|
|
use revision::revisioned;
|
2020-06-29 15:36:01 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-10-04 21:51:18 +00:00
|
|
|
use std::fmt::{self, Display, Formatter};
|
2022-02-22 19:05:58 +00:00
|
|
|
use std::ops::Deref;
|
2020-06-29 15:36:01 +00:00
|
|
|
use std::str;
|
|
|
|
|
2023-09-08 11:28:36 +00:00
|
|
|
use super::dir::dir;
|
|
|
|
use super::error::{expected, ExplainResultExt};
|
|
|
|
|
2023-03-30 10:41:44 +00:00
|
|
|
pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Idiom";
|
|
|
|
|
2022-10-27 12:23:24 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
|
2023-08-17 18:03:46 +00:00
|
|
|
#[revisioned(revision = 1)]
|
2022-01-13 17:36:41 +00:00
|
|
|
pub struct Idioms(pub Vec<Idiom>);
|
2020-06-29 15:36:01 +00:00
|
|
|
|
2022-04-09 09:09:01 +00:00
|
|
|
impl Deref for Idioms {
|
|
|
|
type Target = Vec<Idiom>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 18:00:11 +00:00
|
|
|
impl IntoIterator for Idioms {
|
|
|
|
type Item = Idiom;
|
|
|
|
type IntoIter = std::vec::IntoIter<Self::Item>;
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.0.into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 21:51:18 +00:00
|
|
|
impl Display for Idioms {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
Display::fmt(&Fmt::comma_separated(&self.0), f)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
pub fn locals(i: &str) -> IResult<&str, Idioms> {
|
|
|
|
let (i, v) = separated_list1(commas, local)(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
Ok((i, Idioms(v)))
|
|
|
|
}
|
|
|
|
|
2023-04-29 15:58:22 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
|
|
|
|
#[serde(rename = "$surrealdb::private::sql::Idiom")]
|
2023-08-17 18:03:46 +00:00
|
|
|
#[revisioned(revision = 1)]
|
2022-05-05 11:26:46 +00:00
|
|
|
pub struct Idiom(pub Vec<Part>);
|
2020-06-29 15:36:01 +00:00
|
|
|
|
2022-02-22 19:05:58 +00:00
|
|
|
impl Deref for Idiom {
|
|
|
|
type Target = [Part];
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2022-05-05 11:26:46 +00:00
|
|
|
self.0.as_slice()
|
2022-02-22 19:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 19:44:27 +00:00
|
|
|
impl From<String> for Idiom {
|
|
|
|
fn from(v: String) -> Self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self(vec![Part::from(v)])
|
2022-05-31 19:44:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
impl From<Vec<Part>> for Idiom {
|
|
|
|
fn from(v: Vec<Part>) -> Self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self(v)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 09:24:14 +00:00
|
|
|
impl From<&[Part]> for Idiom {
|
|
|
|
fn from(v: &[Part]) -> Self {
|
|
|
|
Self(v.to_vec())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
impl Idiom {
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Appends a part to the end of this Idiom
|
2022-06-04 08:55:05 +00:00
|
|
|
pub(crate) fn push(mut self, n: Part) -> Idiom {
|
2022-05-05 11:26:46 +00:00
|
|
|
self.0.push(n);
|
2022-04-07 14:33:57 +00:00
|
|
|
self
|
2022-01-22 21:16:13 +00:00
|
|
|
}
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Convert this Idiom to a unique hash
|
2022-07-16 22:23:13 +00:00
|
|
|
pub(crate) fn to_hash(&self) -> String {
|
|
|
|
let mut hasher = Md5::new();
|
|
|
|
hasher.update(self.to_string().as_str());
|
|
|
|
format!("{:x}", hasher.finalize())
|
|
|
|
}
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Convert this Idiom to a JSON Path string
|
2022-06-04 08:55:05 +00:00
|
|
|
pub(crate) fn to_path(&self) -> String {
|
2023-02-03 11:47:07 +00:00
|
|
|
format!("/{self}").replace(']', "").replace(&['.', '['][..], "/")
|
2022-01-31 23:11:06 +00:00
|
|
|
}
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Simplifies this Idiom for use in object keys
|
2022-06-04 08:55:05 +00:00
|
|
|
pub(crate) fn simplify(&self) -> Idiom {
|
2022-05-31 19:44:27 +00:00
|
|
|
self.0
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
2023-07-15 07:08:26 +00:00
|
|
|
.filter(|p| {
|
|
|
|
matches!(p, Part::Field(_) | Part::Start(_) | Part::Value(_) | Part::Graph(_))
|
|
|
|
})
|
2022-05-31 19:44:27 +00:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.into()
|
|
|
|
}
|
2023-09-01 11:52:02 +00:00
|
|
|
/// Check if this Idiom is an 'id' field
|
2022-07-04 16:54:43 +00:00
|
|
|
pub(crate) fn is_id(&self) -> bool {
|
|
|
|
self.0.len() == 1 && self.0[0].eq(&ID[0])
|
|
|
|
}
|
2023-09-01 11:52:02 +00:00
|
|
|
/// Check if this Idiom is an 'in' field
|
2022-07-04 16:54:43 +00:00
|
|
|
pub(crate) fn is_in(&self) -> bool {
|
|
|
|
self.0.len() == 1 && self.0[0].eq(&IN[0])
|
|
|
|
}
|
2023-09-01 11:52:02 +00:00
|
|
|
/// Check if this Idiom is an 'out' field
|
2022-07-04 16:54:43 +00:00
|
|
|
pub(crate) fn is_out(&self) -> bool {
|
|
|
|
self.0.len() == 1 && self.0[0].eq(&OUT[0])
|
|
|
|
}
|
2023-09-01 11:52:02 +00:00
|
|
|
/// Check if this Idiom is a 'meta' field
|
2023-04-16 17:17:44 +00:00
|
|
|
pub(crate) fn is_meta(&self) -> bool {
|
|
|
|
self.0.len() == 1 && self.0[0].eq(&META[0])
|
|
|
|
}
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Check if this is an expression with multiple yields
|
2022-06-04 08:55:05 +00:00
|
|
|
pub(crate) fn is_multi_yield(&self) -> bool {
|
|
|
|
self.iter().any(Self::split_multi_yield)
|
|
|
|
}
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Check if the path part is a yield in a multi-yield expression
|
2022-06-04 08:55:05 +00:00
|
|
|
pub(crate) fn split_multi_yield(v: &Part) -> bool {
|
|
|
|
matches!(v, Part::Graph(g) if g.alias.is_some())
|
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 08:12:56 +00:00
|
|
|
impl Idiom {
|
2023-05-09 22:17:29 +00:00
|
|
|
/// Check if we require a writeable transaction
|
|
|
|
pub(crate) fn writeable(&self) -> bool {
|
|
|
|
self.0.iter().any(|v| v.writeable())
|
|
|
|
}
|
|
|
|
/// Process this type returning a computed simple Value
|
2023-07-06 14:57:42 +00:00
|
|
|
pub(crate) async fn compute(
|
|
|
|
&self,
|
|
|
|
ctx: &Context<'_>,
|
|
|
|
opt: &Options,
|
|
|
|
txn: &Transaction,
|
|
|
|
doc: Option<&CursorDoc<'_>>,
|
|
|
|
) -> Result<Value, Error> {
|
2022-06-09 08:18:08 +00:00
|
|
|
match self.first() {
|
2023-03-31 15:42:29 +00:00
|
|
|
// The starting part is a value
|
2023-07-15 07:08:26 +00:00
|
|
|
Some(Part::Start(v)) => {
|
2023-07-06 14:57:42 +00:00
|
|
|
v.compute(ctx, opt, txn, doc)
|
2022-06-09 08:18:08 +00:00
|
|
|
.await?
|
2023-07-06 14:57:42 +00:00
|
|
|
.get(ctx, opt, txn, doc, self.as_ref().next())
|
2023-03-31 15:42:29 +00:00
|
|
|
.await?
|
2023-07-06 14:57:42 +00:00
|
|
|
.compute(ctx, opt, txn, doc)
|
2022-06-09 08:18:08 +00:00
|
|
|
.await
|
|
|
|
}
|
|
|
|
// Otherwise use the current document
|
2023-07-06 14:57:42 +00:00
|
|
|
_ => match doc {
|
2022-06-09 08:18:08 +00:00
|
|
|
// There is a current document
|
2023-07-06 14:57:42 +00:00
|
|
|
Some(v) => {
|
|
|
|
v.doc.get(ctx, opt, txn, doc, self).await?.compute(ctx, opt, txn, doc).await
|
|
|
|
}
|
2022-06-09 08:18:08 +00:00
|
|
|
// There isn't any document
|
|
|
|
None => Ok(Value::None),
|
|
|
|
},
|
2022-01-14 08:12:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 15:42:29 +00:00
|
|
|
impl Display for Idiom {
|
2020-06-29 15:36:01 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2023-01-19 09:53:33 +00:00
|
|
|
Display::fmt(
|
|
|
|
&Fmt::new(
|
|
|
|
self.0.iter().enumerate().map(|args| {
|
|
|
|
Fmt::new(args, |(i, p), f| match (i, p) {
|
|
|
|
(0, Part::Field(v)) => Display::fmt(v, f),
|
|
|
|
_ => Display::fmt(p, f),
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
fmt_separated_by(""),
|
|
|
|
),
|
2022-01-13 17:36:41 +00:00
|
|
|
f,
|
|
|
|
)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 15:42:29 +00:00
|
|
|
/// Used in DEFINE FIELD and DEFINE INDEX clauses
|
2022-01-13 17:36:41 +00:00
|
|
|
pub fn local(i: &str) -> IResult<&str, Idiom> {
|
2023-09-08 11:28:36 +00:00
|
|
|
expected("a local idiom", |i| {
|
|
|
|
let (i, p) = first(i).explain("graphs are not allowed in a local idioms.", dir)?;
|
|
|
|
let (i, mut v) = many0(local_part)(i)?;
|
|
|
|
// Flatten is only allowed at the end
|
|
|
|
let (i, flat) = opt(flatten)(i)?;
|
|
|
|
if let Some(p) = flat {
|
|
|
|
v.push(p);
|
|
|
|
}
|
|
|
|
v.insert(0, p);
|
|
|
|
Ok((i, Idiom::from(v)))
|
|
|
|
})(i)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Used in a SPLIT, ORDER, and GROUP clauses
|
2022-03-23 11:51:36 +00:00
|
|
|
pub fn basic(i: &str) -> IResult<&str, Idiom> {
|
2023-09-08 11:28:36 +00:00
|
|
|
expected("a basic idiom", |i| {
|
|
|
|
let (i, p) = first(i).explain("graphs are not allowed in a basic idioms.", dir)?;
|
|
|
|
let (i, mut v) = many0(basic_part)(i)?;
|
|
|
|
v.insert(0, p);
|
|
|
|
Ok((i, Idiom::from(v)))
|
|
|
|
})(i)
|
2022-03-23 11:51:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 15:42:29 +00:00
|
|
|
/// A simple idiom with one or more parts
|
|
|
|
pub fn plain(i: &str) -> IResult<&str, Idiom> {
|
2023-09-08 11:28:36 +00:00
|
|
|
expected("a idiom", |i| {
|
|
|
|
let (i, p) = alt((first, graph))(i)?;
|
|
|
|
let (i, mut v) = many0(part)(i)?;
|
|
|
|
v.insert(0, p);
|
|
|
|
Ok((i, Idiom::from(v)))
|
|
|
|
})(i)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 10:33:57 +00:00
|
|
|
/// Reparse a value which might part of an idiom.
|
|
|
|
pub fn reparse_idiom_start(start: Value, i: &str) -> IResult<&str, Value> {
|
|
|
|
if start.can_start_idiom() {
|
|
|
|
if let (i, Some(mut parts)) = opt(many1(part))(i)? {
|
|
|
|
let start = Part::Start(start);
|
|
|
|
parts.insert(0, start);
|
|
|
|
let v = Value::from(Idiom::from(parts));
|
|
|
|
return Ok((i, v));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok((i, start))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A complex idiom with graph or many parts excluding idioms which start with a value.
|
|
|
|
pub fn multi_without_start(i: &str) -> IResult<&str, Idiom> {
|
2022-06-09 08:18:08 +00:00
|
|
|
alt((
|
|
|
|
|i| {
|
2023-03-31 15:42:29 +00:00
|
|
|
let (i, p) = graph(i)?;
|
|
|
|
let (i, mut v) = many0(part)(i)?;
|
|
|
|
v.insert(0, p);
|
|
|
|
Ok((i, Idiom::from(v)))
|
|
|
|
},
|
|
|
|
|i| {
|
2023-08-21 10:33:57 +00:00
|
|
|
let (i, p) = first(i)?;
|
2023-03-31 15:42:29 +00:00
|
|
|
let (i, mut v) = many1(part)(i)?;
|
2022-06-09 08:18:08 +00:00
|
|
|
v.insert(0, p);
|
|
|
|
Ok((i, Idiom::from(v)))
|
|
|
|
},
|
|
|
|
))(i)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 15:42:29 +00:00
|
|
|
/// A simple field based idiom
|
|
|
|
pub fn path(i: &str) -> IResult<&str, Idiom> {
|
|
|
|
let (i, p) = first(i)?;
|
|
|
|
let (i, mut v) = many0(part)(i)?;
|
|
|
|
v.insert(0, p);
|
|
|
|
Ok((i, Idiom::from(v)))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A full complex idiom with any number of parts
|
|
|
|
#[cfg(test)]
|
|
|
|
pub fn idiom(i: &str) -> IResult<&str, Idiom> {
|
2023-08-21 10:33:57 +00:00
|
|
|
use nom::combinator::fail;
|
|
|
|
|
|
|
|
use crate::sql::value::value;
|
|
|
|
|
|
|
|
alt((
|
|
|
|
plain,
|
|
|
|
alt((multi_without_start, |i| {
|
|
|
|
let (i, v) = value(i)?;
|
|
|
|
let (i, v) = reparse_idiom_start(v, i)?;
|
|
|
|
if let Value::Idiom(x) = v {
|
|
|
|
return Ok((i, x));
|
|
|
|
}
|
|
|
|
fail(i)
|
|
|
|
})),
|
|
|
|
))(i)
|
2023-03-31 15:42:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
2022-06-09 08:18:08 +00:00
|
|
|
use crate::sql::dir::Dir;
|
2022-01-13 17:36:41 +00:00
|
|
|
use crate::sql::expression::Expression;
|
2023-03-31 17:50:11 +00:00
|
|
|
use crate::sql::field::Fields;
|
2022-06-09 08:18:08 +00:00
|
|
|
use crate::sql::graph::Graph;
|
2023-03-31 15:42:29 +00:00
|
|
|
use crate::sql::number::Number;
|
|
|
|
use crate::sql::param::Param;
|
2022-06-09 08:18:08 +00:00
|
|
|
use crate::sql::table::Table;
|
2022-01-13 17:36:41 +00:00
|
|
|
use crate::sql::test::Parse;
|
2022-06-09 08:18:08 +00:00
|
|
|
use crate::sql::thing::Thing;
|
2020-06-29 15:36:01 +00:00
|
|
|
|
2023-03-31 15:42:29 +00:00
|
|
|
#[test]
|
|
|
|
fn idiom_number() {
|
|
|
|
let sql = "13.495";
|
|
|
|
let res = idiom(sql);
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
#[test]
|
|
|
|
fn idiom_normal() {
|
|
|
|
let sql = "test";
|
|
|
|
let res = idiom(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test", format!("{}", out));
|
2022-05-05 11:26:46 +00:00
|
|
|
assert_eq!(out, Idiom(vec![Part::from("test")]));
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn idiom_quoted_backtick() {
|
|
|
|
let sql = "`test`";
|
|
|
|
let res = idiom(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test", format!("{}", out));
|
2022-05-05 11:26:46 +00:00
|
|
|
assert_eq!(out, Idiom(vec![Part::from("test")]));
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn idiom_quoted_brackets() {
|
|
|
|
let sql = "⟨test⟩";
|
|
|
|
let res = idiom(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test", format!("{}", out));
|
2022-05-05 11:26:46 +00:00
|
|
|
assert_eq!(out, Idiom(vec![Part::from("test")]));
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn idiom_nested() {
|
|
|
|
let sql = "test.temp";
|
|
|
|
let res = idiom(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test.temp", format!("{}", out));
|
2023-03-31 15:42:29 +00:00
|
|
|
assert_eq!(out, Idiom(vec![Part::from("test"), Part::from("temp")]));
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn idiom_nested_quoted() {
|
|
|
|
let sql = "test.`some key`";
|
|
|
|
let res = idiom(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test.`some key`", format!("{}", out));
|
2023-03-31 15:42:29 +00:00
|
|
|
assert_eq!(out, Idiom(vec![Part::from("test"), Part::from("some key")]));
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn idiom_nested_array_all() {
|
|
|
|
let sql = "test.temp[*]";
|
|
|
|
let res = idiom(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test.temp[*]", format!("{}", out));
|
2023-03-31 15:42:29 +00:00
|
|
|
assert_eq!(out, Idiom(vec![Part::from("test"), Part::from("temp"), Part::All]));
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn idiom_nested_array_last() {
|
|
|
|
let sql = "test.temp[$]";
|
|
|
|
let res = idiom(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test.temp[$]", format!("{}", out));
|
2023-03-31 15:42:29 +00:00
|
|
|
assert_eq!(out, Idiom(vec![Part::from("test"), Part::from("temp"), Part::Last]));
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn idiom_nested_array_value() {
|
|
|
|
let sql = "test.temp[*].text";
|
|
|
|
let res = idiom(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test.temp[*].text", format!("{}", out));
|
|
|
|
assert_eq!(
|
|
|
|
out,
|
2022-05-05 11:26:46 +00:00
|
|
|
Idiom(vec![Part::from("test"), Part::from("temp"), Part::All, Part::from("text")])
|
2020-06-29 15:36:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn idiom_nested_array_question() {
|
|
|
|
let sql = "test.temp[? test = true].text";
|
|
|
|
let res = idiom(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test.temp[WHERE test = true].text", format!("{}", out));
|
|
|
|
assert_eq!(
|
|
|
|
out,
|
2022-05-05 11:26:46 +00:00
|
|
|
Idiom(vec![
|
|
|
|
Part::from("test"),
|
|
|
|
Part::from("temp"),
|
2023-03-31 15:42:29 +00:00
|
|
|
Part::Where(Value::from(Expression::parse("test = true"))),
|
2022-05-05 11:26:46 +00:00
|
|
|
Part::from("text")
|
|
|
|
])
|
2020-06-29 15:36:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn idiom_nested_array_condition() {
|
|
|
|
let sql = "test.temp[WHERE test = true].text";
|
|
|
|
let res = idiom(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test.temp[WHERE test = true].text", format!("{}", out));
|
|
|
|
assert_eq!(
|
|
|
|
out,
|
2022-05-05 11:26:46 +00:00
|
|
|
Idiom(vec![
|
|
|
|
Part::from("test"),
|
|
|
|
Part::from("temp"),
|
2023-03-31 15:42:29 +00:00
|
|
|
Part::Where(Value::from(Expression::parse("test = true"))),
|
2022-05-05 11:26:46 +00:00
|
|
|
Part::from("text")
|
|
|
|
])
|
2020-06-29 15:36:01 +00:00
|
|
|
);
|
|
|
|
}
|
2022-06-09 08:18:08 +00:00
|
|
|
|
2023-03-31 15:42:29 +00:00
|
|
|
#[test]
|
|
|
|
fn idiom_start_param_local_field() {
|
2023-08-01 07:30:13 +00:00
|
|
|
let sql = "$test.temporary[0].embedded…";
|
2023-03-31 15:42:29 +00:00
|
|
|
let res = idiom(sql);
|
|
|
|
let out = res.unwrap().1;
|
2023-08-01 07:30:13 +00:00
|
|
|
assert_eq!("$test.temporary[0].embedded…", format!("{}", out));
|
2023-03-31 15:42:29 +00:00
|
|
|
assert_eq!(
|
|
|
|
out,
|
|
|
|
Idiom(vec![
|
2023-07-15 07:08:26 +00:00
|
|
|
Part::Start(Param::from("test").into()),
|
2023-03-31 15:42:29 +00:00
|
|
|
Part::from("temporary"),
|
|
|
|
Part::Index(Number::Int(0)),
|
|
|
|
Part::from("embedded"),
|
2023-08-01 07:30:13 +00:00
|
|
|
Part::Flatten,
|
2023-03-31 15:42:29 +00:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-09 08:18:08 +00:00
|
|
|
#[test]
|
|
|
|
fn idiom_start_thing_remote_traversal() {
|
|
|
|
let sql = "person:test.friend->like->person";
|
|
|
|
let res = idiom(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("person:test.friend->like->person", format!("{}", out));
|
|
|
|
assert_eq!(
|
|
|
|
out,
|
|
|
|
Idiom(vec![
|
2023-07-15 07:08:26 +00:00
|
|
|
Part::Start(Thing::from(("person", "test")).into()),
|
2022-06-09 08:18:08 +00:00
|
|
|
Part::from("friend"),
|
2023-03-31 15:42:29 +00:00
|
|
|
Part::Graph(Graph {
|
2022-06-09 08:18:08 +00:00
|
|
|
dir: Dir::Out,
|
2023-03-31 17:50:11 +00:00
|
|
|
expr: Fields::all(),
|
2022-06-09 08:18:08 +00:00
|
|
|
what: Table::from("like").into(),
|
|
|
|
cond: None,
|
|
|
|
alias: None,
|
2023-03-31 17:50:11 +00:00
|
|
|
split: None,
|
|
|
|
group: None,
|
|
|
|
order: None,
|
|
|
|
limit: None,
|
|
|
|
start: None,
|
2022-06-09 08:18:08 +00:00
|
|
|
}),
|
2023-03-31 15:42:29 +00:00
|
|
|
Part::Graph(Graph {
|
2022-06-09 08:18:08 +00:00
|
|
|
dir: Dir::Out,
|
2023-03-31 17:50:11 +00:00
|
|
|
expr: Fields::all(),
|
2022-06-09 08:18:08 +00:00
|
|
|
what: Table::from("person").into(),
|
|
|
|
cond: None,
|
|
|
|
alias: None,
|
2023-03-31 17:50:11 +00:00
|
|
|
split: None,
|
|
|
|
group: None,
|
|
|
|
order: None,
|
|
|
|
limit: None,
|
|
|
|
start: None,
|
2022-06-09 08:18:08 +00:00
|
|
|
}),
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|