2022-02-06 01:14:56 +00:00
|
|
|
use crate::dbs::Options;
|
|
|
|
use crate::dbs::Runtime;
|
|
|
|
use crate::dbs::Statement;
|
2022-02-15 01:00:30 +00:00
|
|
|
use crate::dbs::Transaction;
|
2022-02-06 01:14:56 +00:00
|
|
|
use crate::doc::Document;
|
|
|
|
use crate::err::Error;
|
|
|
|
use crate::sql::field::Field;
|
|
|
|
use crate::sql::idiom::Idiom;
|
|
|
|
use crate::sql::output::Output;
|
|
|
|
use crate::sql::value::Value;
|
|
|
|
|
2022-02-13 19:03:00 +00:00
|
|
|
impl<'a> Document<'a> {
|
2022-02-06 01:14:56 +00:00
|
|
|
pub async fn pluck(
|
|
|
|
&self,
|
|
|
|
ctx: &Runtime,
|
2022-02-06 21:06:52 +00:00
|
|
|
opt: &Options,
|
2022-02-15 03:33:16 +00:00
|
|
|
txn: &Transaction,
|
2022-02-26 23:30:19 +00:00
|
|
|
stm: &Statement,
|
2022-02-06 01:14:56 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
// Ensure futures are run
|
|
|
|
let opt = &opt.futures(true);
|
2022-04-01 09:15:52 +00:00
|
|
|
match stm.output() {
|
2022-04-06 18:44:55 +00:00
|
|
|
// Process output clause
|
2022-02-06 01:14:56 +00:00
|
|
|
Some(v) => match v {
|
2022-03-06 10:58:59 +00:00
|
|
|
Output::None => Err(Error::Ignore),
|
2022-02-06 01:14:56 +00:00
|
|
|
Output::Null => Ok(Value::Null),
|
|
|
|
Output::Diff => Ok(self.initial.diff(&self.current, Idiom::default()).into()),
|
2022-02-15 01:00:30 +00:00
|
|
|
Output::After => self.current.compute(ctx, opt, txn, Some(&self.current)).await,
|
|
|
|
Output::Before => self.initial.compute(ctx, opt, txn, Some(&self.initial)).await,
|
2022-02-06 01:14:56 +00:00
|
|
|
Output::Fields(v) => {
|
|
|
|
let mut out = match v.all() {
|
2022-02-15 01:00:30 +00:00
|
|
|
true => self.current.compute(ctx, opt, txn, Some(&self.current)).await?,
|
2022-02-06 01:14:56 +00:00
|
|
|
false => Value::base(),
|
|
|
|
};
|
2022-03-04 16:01:32 +00:00
|
|
|
for v in v.other() {
|
2022-02-06 01:14:56 +00:00
|
|
|
match v {
|
|
|
|
Field::All => (),
|
|
|
|
Field::Alone(v) => {
|
2022-02-15 01:00:30 +00:00
|
|
|
let x = v.compute(ctx, opt, txn, Some(&self.current)).await?;
|
2022-02-22 19:05:58 +00:00
|
|
|
out.set(ctx, opt, txn, v.to_idiom().as_ref(), x).await?;
|
2022-02-06 01:14:56 +00:00
|
|
|
}
|
|
|
|
Field::Alias(v, i) => {
|
2022-02-15 01:00:30 +00:00
|
|
|
let x = v.compute(ctx, opt, txn, Some(&self.current)).await?;
|
2022-02-22 19:05:58 +00:00
|
|
|
out.set(ctx, opt, txn, i, x).await?;
|
2022-02-06 01:14:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(out)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => match stm {
|
2022-04-01 09:15:52 +00:00
|
|
|
Statement::Select(s) => {
|
|
|
|
let mut out = match s.expr.all() {
|
2022-02-15 01:00:30 +00:00
|
|
|
true => self.current.compute(ctx, opt, txn, Some(&self.current)).await?,
|
2022-02-06 01:14:56 +00:00
|
|
|
false => Value::base(),
|
|
|
|
};
|
2022-04-01 09:15:52 +00:00
|
|
|
for v in s.expr.other() {
|
2022-02-06 01:14:56 +00:00
|
|
|
match v {
|
|
|
|
Field::All => (),
|
2022-03-25 18:43:22 +00:00
|
|
|
Field::Alone(v) => match v {
|
2022-04-01 09:15:52 +00:00
|
|
|
Value::Function(f) if s.group.is_some() && f.is_aggregate() => {
|
2022-03-25 18:43:22 +00:00
|
|
|
let x = match f.args().len() {
|
|
|
|
0 => f.compute(ctx, opt, txn, Some(&self.current)).await?,
|
|
|
|
_ => {
|
|
|
|
f.args()[0]
|
|
|
|
.compute(ctx, opt, txn, Some(&self.current))
|
|
|
|
.await?
|
|
|
|
}
|
|
|
|
};
|
|
|
|
out.set(ctx, opt, txn, v.to_idiom().as_ref(), x).await?;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let x = v.compute(ctx, opt, txn, Some(&self.current)).await?;
|
|
|
|
out.set(ctx, opt, txn, v.to_idiom().as_ref(), x).await?;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Field::Alias(v, i) => match v {
|
2022-04-01 09:15:52 +00:00
|
|
|
Value::Function(f) if s.group.is_some() && f.is_aggregate() => {
|
2022-03-25 18:43:22 +00:00
|
|
|
let x = match f.args().len() {
|
|
|
|
0 => f.compute(ctx, opt, txn, Some(&self.current)).await?,
|
|
|
|
_ => {
|
|
|
|
f.args()[0]
|
|
|
|
.compute(ctx, opt, txn, Some(&self.current))
|
|
|
|
.await?
|
|
|
|
}
|
|
|
|
};
|
|
|
|
out.set(ctx, opt, txn, i, x).await?;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let x = v.compute(ctx, opt, txn, Some(&self.current)).await?;
|
|
|
|
out.set(ctx, opt, txn, i, x).await?;
|
|
|
|
}
|
|
|
|
},
|
2022-02-06 01:14:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(out)
|
|
|
|
}
|
2022-04-01 09:15:52 +00:00
|
|
|
Statement::Create(_) => {
|
|
|
|
self.current.compute(ctx, opt, txn, Some(&self.current)).await
|
|
|
|
}
|
|
|
|
Statement::Update(_) => {
|
|
|
|
self.current.compute(ctx, opt, txn, Some(&self.current)).await
|
|
|
|
}
|
|
|
|
Statement::Relate(_) => {
|
|
|
|
self.current.compute(ctx, opt, txn, Some(&self.current)).await
|
|
|
|
}
|
|
|
|
Statement::Insert(_) => {
|
|
|
|
self.current.compute(ctx, opt, txn, Some(&self.current)).await
|
|
|
|
}
|
|
|
|
_ => Err(Error::Ignore),
|
2022-02-06 01:14:56 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|