Implement SQL Cond as a newtype tuple struct
This commit is contained in:
parent
1ed5df005e
commit
0a3fe67358
2 changed files with 11 additions and 10 deletions
|
@ -16,7 +16,7 @@ impl<'a> Document<'a> {
|
|||
// Check where condition
|
||||
if let Some(cond) = stm.conds() {
|
||||
// Check if the expression is truthy
|
||||
if !cond.expr.compute(ctx, opt, txn, Some(&self.current)).await?.is_truthy() {
|
||||
if !cond.compute(ctx, opt, txn, Some(&self.current)).await?.is_truthy() {
|
||||
// Ignore this document
|
||||
return Err(Error::Ignore);
|
||||
}
|
||||
|
|
|
@ -4,15 +4,21 @@ use crate::sql::value::{value, Value};
|
|||
use nom::bytes::complete::tag_no_case;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
use std::ops::Deref;
|
||||
|
||||
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
|
||||
pub struct Cond {
|
||||
pub expr: Value,
|
||||
pub struct Cond(pub Value);
|
||||
|
||||
impl Deref for Cond {
|
||||
type Target = Value;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Cond {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "WHERE {}", self.expr)
|
||||
write!(f, "WHERE {}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,12 +26,7 @@ pub fn cond(i: &str) -> IResult<&str, Cond> {
|
|||
let (i, _) = tag_no_case("WHERE")(i)?;
|
||||
let (i, _) = shouldbespace(i)?;
|
||||
let (i, v) = value(i)?;
|
||||
Ok((
|
||||
i,
|
||||
Cond {
|
||||
expr: v,
|
||||
},
|
||||
))
|
||||
Ok((i, Cond(v)))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
Loading…
Reference in a new issue