surrealpatch/lib/src/doc/compute.rs

46 lines
1.3 KiB
Rust
Raw Normal View History

use crate::ctx::Context;
2022-05-30 15:32:26 +00:00
use crate::dbs::Operable;
use crate::dbs::Options;
use crate::dbs::Statement;
use crate::dbs::Transaction;
2022-05-30 15:32:26 +00:00
use crate::dbs::Workable;
use crate::doc::Document;
use crate::err::Error;
2022-02-23 13:56:09 +00:00
use crate::sql::thing::Thing;
use crate::sql::value::Value;
use channel::Sender;
impl<'a> Document<'a> {
pub(crate) async fn compute(
ctx: &Context<'_>,
opt: &Options,
txn: &Transaction,
stm: &Statement<'_>,
2022-02-26 23:30:19 +00:00
chn: Sender<Result<Value, Error>>,
2022-02-23 13:56:09 +00:00
thg: Option<Thing>,
2022-05-30 15:32:26 +00:00
val: Operable,
2022-02-26 23:30:19 +00:00
) -> Result<(), Error> {
2022-05-30 15:32:26 +00:00
// Setup a new workable
let ins = match val {
Operable::Value(v) => (v, Workable::Normal),
Operable::Mergeable(v, o) => (v, Workable::Insert(o)),
Operable::Relatable(f, v, w) => (v, Workable::Relate(f, w)),
};
2022-02-23 13:56:09 +00:00
// Setup a new document
2022-05-30 15:32:26 +00:00
let mut doc = Document::new(thg, &ins.0, ins.1);
2022-02-23 13:56:09 +00:00
// Process the statement
2022-02-26 23:30:19 +00:00
let res = match stm {
Statement::Select(_) => doc.select(ctx, opt, txn, stm).await,
Statement::Create(_) => doc.create(ctx, opt, txn, stm).await,
Statement::Update(_) => doc.update(ctx, opt, txn, stm).await,
Statement::Relate(_) => doc.relate(ctx, opt, txn, stm).await,
Statement::Delete(_) => doc.delete(ctx, opt, txn, stm).await,
Statement::Insert(_) => doc.insert(ctx, opt, txn, stm).await,
2022-02-26 23:30:19 +00:00
};
// Send back the result
let _ = chn.send(res).await;
// Everything went ok
Ok(())
}
}