use reblessive::tree::Stk;
use surrealdb_types::{SqlFormat, ToSql};
use crate::ctx::FrozenContext;
use crate::dbs::Options;
use crate::doc::CursorDoc;
use crate::err::Error;
use crate::expr::{ControlFlow, Expr, FlowResult, Value};
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash)]
pub(crate) struct IfelseStatement {
pub exprs: Vec<(Expr, Expr)>,
pub close: Option<Expr>,
}
impl IfelseStatement {
pub(crate) fn read_only(&self) -> bool {
self.exprs.iter().all(|x| x.0.read_only() && x.1.read_only())
&& self.close.as_ref().map(|x| x.read_only()).unwrap_or(true)
}
pub(crate) fn has_direct_write(&self) -> bool {
self.exprs.iter().any(|x| x.0.has_direct_write() || x.1.has_direct_write())
|| self.close.as_ref().map(|x| x.has_direct_write()).unwrap_or(false)
}
#[instrument(level = "trace", name = "IfelseStatement::compute", skip_all)]
pub(crate) async fn compute(
&self,
stk: &mut Stk,
ctx: &FrozenContext,
opt: &Options,
doc: Option<&CursorDoc>,
) -> FlowResult<Value> {
for (cond, then) in &self.exprs {
if let Some(d) = ctx.is_timedout().await? {
return Err(ControlFlow::from(anyhow::Error::new(Error::QueryTimedout(d.into()))));
}
let v = stk.run(|stk| cond.compute(stk, ctx, opt, doc)).await?;
if v.is_truthy() {
return stk.run(|stk| then.compute(stk, ctx, opt, doc)).await;
}
}
match self.close {
Some(ref v) => stk.run(|stk| v.compute(stk, ctx, opt, doc)).await,
None => Ok(Value::None),
}
}
}
impl ToSql for IfelseStatement {
fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
let stmt: crate::sql::statements::ifelse::IfelseStatement = self.clone().into();
stmt.fmt_sql(f, fmt);
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
use crate::syn;
#[test]
fn format_pretty() {
let query = syn::expr("IF 1 { 1 } ELSE IF 2 { 2 }").unwrap();
assert_eq!(query.to_sql(), "IF 1 { 1 } ELSE IF 2 { 2 }");
assert_eq!(query.to_sql_pretty(), "IF 1 { 1 } ELSE IF 2 { 2 }");
}
}