use crate::ctx::{Context, MutableContext};
use crate::dbs::Options;
use crate::dbs::Statement;
use crate::doc::Document;
use crate::doc::Permitted::*;
use crate::err::Error;
use crate::iam::Action;
use crate::sql::idiom::Idiom;
use crate::sql::output::Output;
use crate::sql::paths::META;
use crate::sql::permission::Permission;
use crate::sql::value::Value;
use reblessive::tree::Stk;
use std::sync::Arc;
impl Document {
pub(super) async fn pluck(
&mut self,
stk: &mut Stk,
ctx: &Context,
opt: &Options,
stm: &Statement<'_>,
) -> Result<Value, Error> {
let opt = &opt.new_with_futures(true);
self.check_permissions_view(stk, ctx, opt, stm).await?;
let mut out = match stm.output() {
Some(v) => match v {
Output::None => Err(Error::Ignore),
Output::Null => Ok(Value::Null),
Output::Diff => {
let (initial, current) = match self.reduced(stk, ctx, opt, Both).await? {
true => (&self.initial_reduced, &self.current_reduced),
false => (&self.initial, &self.current),
};
Ok(initial.doc.as_ref().diff(current.doc.as_ref(), Idiom::default()).into())
}
Output::After => {
match self.reduced(stk, ctx, opt, Current).await? {
true => Ok(self.current_reduced.doc.as_ref().to_owned()),
false => {
self.current
.doc
.as_ref()
.compute(stk, ctx, opt, Some(&self.current))
.await
}
}
}
Output::Before => {
match self.reduced(stk, ctx, opt, Initial).await? {
true => Ok(self.initial_reduced.doc.as_ref().to_owned()),
false => {
self.initial
.doc
.as_ref()
.compute(stk, ctx, opt, Some(&self.initial))
.await
}
}
}
Output::Fields(v) => {
let (initial, current) = match self.reduced(stk, ctx, opt, Both).await? {
true => (&mut self.initial_reduced, &mut self.current_reduced),
false => (&mut self.initial, &mut self.current),
};
let mut ctx = MutableContext::new(ctx);
ctx.add_value("after", current.doc.as_arc());
ctx.add_value("before", initial.doc.as_arc());
let ctx = ctx.freeze();
v.compute(stk, &ctx, opt, Some(current), false).await
}
},
None => match stm {
Statement::Live(s) => match s.expr.len() {
0 => {
let (initial, current) = match self.reduced(stk, ctx, opt, Both).await? {
true => (&self.initial_reduced, &self.current_reduced),
false => (&self.initial, &self.current),
};
Ok(initial.doc.as_ref().diff(current.doc.as_ref(), Idiom::default()).into())
}
_ => {
let current = match self.reduced(stk, ctx, opt, Current).await? {
true => &self.current_reduced,
false => &self.current,
};
s.expr.compute(stk, ctx, opt, Some(current), false).await
}
},
Statement::Select(s) => {
let current = match self.reduced(stk, ctx, opt, Current).await? {
true => &self.current_reduced,
false => &self.current,
};
s.expr.compute(stk, ctx, opt, Some(current), s.group.is_some()).await
}
Statement::Create(_) => {
match self.reduced(stk, ctx, opt, Current).await? {
true => Ok(self.current_reduced.doc.as_ref().to_owned()),
false => {
self.current
.doc
.as_ref()
.compute(stk, ctx, opt, Some(&self.current))
.await
}
}
}
Statement::Upsert(_) => {
match self.reduced(stk, ctx, opt, Current).await? {
true => Ok(self.current_reduced.doc.as_ref().to_owned()),
false => {
self.current
.doc
.as_ref()
.compute(stk, ctx, opt, Some(&self.current))
.await
}
}
}
Statement::Update(_) => {
match self.reduced(stk, ctx, opt, Current).await? {
true => Ok(self.current_reduced.doc.as_ref().to_owned()),
false => {
self.current
.doc
.as_ref()
.compute(stk, ctx, opt, Some(&self.current))
.await
}
}
}
Statement::Relate(_) => {
match self.reduced(stk, ctx, opt, Current).await? {
true => Ok(self.current_reduced.doc.as_ref().to_owned()),
false => {
self.current
.doc
.as_ref()
.compute(stk, ctx, opt, Some(&self.current))
.await
}
}
}
Statement::Insert(_) => {
match self.reduced(stk, ctx, opt, Current).await? {
true => Ok(self.current_reduced.doc.as_ref().to_owned()),
false => {
self.current
.doc
.as_ref()
.compute(stk, ctx, opt, Some(&self.current))
.await
}
}
}
_ => Err(Error::Ignore),
},
}?;
if self.id.is_some() {
if opt.check_perms(Action::View)? {
for fd in self.fd(ctx, opt).await?.iter() {
for k in out.each(&fd.name).iter() {
match &fd.permissions.select {
Permission::Full => (),
Permission::None => out.del(stk, ctx, opt, k).await?,
Permission::Specific(e) => {
let opt = &opt.new_with_perms(false);
let val = Arc::new(self.current.doc.as_ref().pick(k));
let mut ctx = MutableContext::new(ctx);
ctx.add_value("value", val);
let ctx = ctx.freeze();
if !e
.compute(stk, &ctx, opt, Some(&self.current))
.await?
.is_truthy()
{
out.cut(k);
}
}
}
}
}
}
}
if let Some(v) = stm.omit() {
for v in v.iter() {
out.del(stk, ctx, opt, v).await?;
}
}
out.cut(&*META);
Ok(out)
}
}