surrealpatch/lib/src/sql/cond.rs

54 lines
1.1 KiB
Rust
Raw Normal View History

2020-06-29 15:36:01 +00:00
use crate::sql::comment::shouldbespace;
2022-01-16 20:31:50 +00:00
use crate::sql::error::IResult;
use crate::sql::value::{value, Value};
2020-06-29 15:36:01 +00:00
use nom::bytes::complete::tag_no_case;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
2020-06-29 15:36:01 +00:00
pub struct Cond {
pub expr: Value,
2020-06-29 15:36:01 +00:00
}
impl fmt::Display for Cond {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "WHERE {}", self.expr)
}
}
pub fn cond(i: &str) -> IResult<&str, Cond> {
let (i, _) = tag_no_case("WHERE")(i)?;
let (i, _) = shouldbespace(i)?;
let (i, v) = value(i)?;
2021-03-29 15:43:37 +00:00
Ok((
i,
Cond {
expr: v,
},
))
2020-06-29 15:36:01 +00:00
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cond_statement() {
let sql = "WHERE field = true";
let res = cond(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("WHERE field = true", format!("{}", out));
}
#[test]
fn cond_statement_multiple() {
let sql = "WHERE field = true AND other.field = false";
let res = cond(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
2021-03-29 15:43:37 +00:00
assert_eq!("WHERE field = true AND other.field = false", format!("{}", out));
2020-06-29 15:36:01 +00:00
}
}