Enable ? operator in graph traversal queries

This commit is contained in:
Tobie Morgan Hitchcock 2022-05-31 21:30:56 +01:00
parent 4d9b3fd403
commit bea90712a3
2 changed files with 24 additions and 4 deletions

View file

@ -7,6 +7,7 @@ use crate::sql::value::{value, Value};
use nom::branch::alt; use nom::branch::alt;
use nom::bytes::complete::tag_no_case; use nom::bytes::complete::tag_no_case;
use nom::character::complete::char; use nom::character::complete::char;
use nom::combinator::map;
use nom::combinator::opt; use nom::combinator::opt;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
@ -44,10 +45,18 @@ pub struct Graph {
impl fmt::Display for Graph { impl fmt::Display for Graph {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.what.0.len() == 1 && self.cond.is_none() && self.alias.is_none() { if self.what.0.len() <= 1 && self.cond.is_none() && self.alias.is_none() {
write!(f, "{}{}", self.dir, self.what) write!(f, "{}", self.dir,)?;
match self.what.len() {
0 => write!(f, "?"),
_ => write!(f, "{}", self.what),
}
} else { } else {
write!(f, "{}({}", self.dir, self.what)?; write!(f, "{}(", self.dir,)?;
match self.what.len() {
0 => write!(f, "?"),
_ => write!(f, "{}", self.what),
}?;
if let Some(ref v) = self.cond { if let Some(ref v) = self.cond {
write!(f, " WHERE {}", v)? write!(f, " WHERE {}", v)?
} }
@ -126,10 +135,14 @@ fn custom(i: &str) -> IResult<&str, (Tables, Option<Value>, Option<Idiom>)> {
} }
fn what(i: &str) -> IResult<&str, Tables> { fn what(i: &str) -> IResult<&str, Tables> {
let (i, v) = tables(i)?; let (i, v) = alt((any, tables))(i)?;
Ok((i, v)) Ok((i, v))
} }
fn any(i: &str) -> IResult<&str, Tables> {
map(char('?'), |_| Tables::default())(i)
}
fn cond(i: &str) -> IResult<&str, Value> { fn cond(i: &str) -> IResult<&str, Value> {
let (i, _) = shouldbespace(i)?; let (i, _) = shouldbespace(i)?;
let (i, _) = tag_no_case("WHERE")(i)?; let (i, _) = tag_no_case("WHERE")(i)?;

View file

@ -13,6 +13,13 @@ use std::str;
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)] #[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Tables(pub Vec<Table>); pub struct Tables(pub Vec<Table>);
impl Deref for Tables {
type Target = Vec<Table>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl fmt::Display for Tables { impl fmt::Display for Tables {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0.iter().map(|ref v| format!("{}", v)).collect::<Vec<_>>().join(", ")) write!(f, "{}", self.0.iter().map(|ref v| format!("{}", v)).collect::<Vec<_>>().join(", "))