surrealdb_sql/statements/
foreach.rsuse crate::ctx::Context;
use crate::dbs::{Options, Transaction};
use crate::doc::CursorDoc;
use crate::err::Error;
use crate::{block::Entry, Block, Param, Value};
use async_recursion::async_recursion;
use derive::Store;
use revision::revisioned;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
#[revisioned(revision = 1)]
pub struct ForeachStatement {
pub param: Param,
pub range: Value,
pub block: Block,
}
impl ForeachStatement {
pub(crate) fn writeable(&self) -> bool {
self.range.writeable() || self.block.writeable()
}
#[cfg_attr(not(target_arch = "wasm32"), async_recursion)]
#[cfg_attr(target_arch = "wasm32", async_recursion(?Send))]
pub(crate) async fn compute(
&self,
ctx: &Context<'_>,
opt: &Options,
txn: &Transaction,
doc: Option<&'async_recursion CursorDoc<'_>>,
) -> Result<Value, Error> {
match &self.range.compute(ctx, opt, txn, doc).await? {
Value::Array(arr) => {
'foreach: for v in arr.iter() {
let mut ctx = Context::new(ctx);
let key = self.param.0.to_raw();
let val = v.compute(&ctx, opt, txn, doc).await?;
ctx.add_value(key, val);
for v in self.block.iter() {
let res = match v {
Entry::Set(v) => {
let val = v.compute(&ctx, opt, txn, doc).await?;
ctx.add_value(v.name.to_owned(), val);
Ok(Value::None)
}
Entry::Value(v) => v.compute(&ctx, opt, txn, doc).await,
Entry::Break(v) => v.compute(&ctx, opt, txn, doc).await,
Entry::Continue(v) => v.compute(&ctx, opt, txn, doc).await,
Entry::Foreach(v) => v.compute(&ctx, opt, txn, doc).await,
Entry::Ifelse(v) => v.compute(&ctx, opt, txn, doc).await,
Entry::Select(v) => v.compute(&ctx, opt, txn, doc).await,
Entry::Create(v) => v.compute(&ctx, opt, txn, doc).await,
Entry::Update(v) => v.compute(&ctx, opt, txn, doc).await,
Entry::Delete(v) => v.compute(&ctx, opt, txn, doc).await,
Entry::Relate(v) => v.compute(&ctx, opt, txn, doc).await,
Entry::Insert(v) => v.compute(&ctx, opt, txn, doc).await,
Entry::Define(v) => v.compute(&ctx, opt, txn, doc).await,
Entry::Remove(v) => v.compute(&ctx, opt, txn, doc).await,
Entry::Output(v) => {
return v.compute(&ctx, opt, txn, doc).await;
}
Entry::Throw(v) => {
return v.compute(&ctx, opt, txn, doc).await;
}
};
match res {
Err(Error::Continue) => continue 'foreach,
Err(Error::Break) => return Ok(Value::None),
Err(err) => return Err(err),
_ => (),
};
}
}
Ok(Value::None)
}
v => Err(Error::InvalidStatementTarget {
value: v.to_string(),
}),
}
}
}
impl Display for ForeachStatement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "FOR {} IN {} {}", self.param, self.range, self.block)
}
}