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::data::Data;
|
|
|
|
use crate::sql::operator::Operator;
|
|
|
|
use crate::sql::value::Value;
|
2021-03-29 15:43:37 +00:00
|
|
|
|
2022-02-13 19:03:00 +00:00
|
|
|
impl<'a> Document<'a> {
|
2022-02-06 01:14:56 +00:00
|
|
|
pub async fn merge(
|
|
|
|
&mut self,
|
|
|
|
ctx: &Runtime,
|
2022-02-06 21:06:52 +00:00
|
|
|
opt: &Options,
|
2022-02-15 03:33:16 +00:00
|
|
|
txn: &Transaction,
|
2022-05-13 20:46:56 +00:00
|
|
|
stm: &Statement<'_>,
|
2022-02-06 01:14:56 +00:00
|
|
|
) -> Result<(), Error> {
|
2022-04-01 10:17:19 +00:00
|
|
|
// Get the record id
|
|
|
|
let rid = self.id.as_ref().unwrap();
|
2022-02-06 01:14:56 +00:00
|
|
|
// Set default field values
|
2022-04-01 10:17:19 +00:00
|
|
|
self.current.to_mut().def(ctx, opt, txn, rid).await?;
|
2022-03-04 16:01:32 +00:00
|
|
|
// The statement has a data clause
|
2022-04-01 09:15:52 +00:00
|
|
|
if let Some(v) = stm.data() {
|
2022-03-04 16:01:32 +00:00
|
|
|
match v {
|
2022-02-06 01:14:56 +00:00
|
|
|
Data::SetExpression(x) => {
|
|
|
|
for x in x.iter() {
|
2022-02-15 01:00:30 +00:00
|
|
|
let v = x.2.compute(ctx, opt, txn, Some(&self.current)).await?;
|
2022-02-06 01:14:56 +00:00
|
|
|
match x.1 {
|
|
|
|
Operator::Equal => match v {
|
2022-02-13 19:03:00 +00:00
|
|
|
Value::Void => {
|
2022-02-15 01:00:30 +00:00
|
|
|
self.current.to_mut().del(ctx, opt, txn, &x.0).await?
|
2022-02-13 19:03:00 +00:00
|
|
|
}
|
2022-02-15 01:00:30 +00:00
|
|
|
_ => self.current.to_mut().set(ctx, opt, txn, &x.0, v).await?,
|
2022-02-06 01:14:56 +00:00
|
|
|
},
|
2022-02-13 19:03:00 +00:00
|
|
|
Operator::Inc => {
|
2022-02-15 01:00:30 +00:00
|
|
|
self.current.to_mut().increment(ctx, opt, txn, &x.0, v).await?
|
2022-02-13 19:03:00 +00:00
|
|
|
}
|
|
|
|
Operator::Dec => {
|
2022-02-15 01:00:30 +00:00
|
|
|
self.current.to_mut().decrement(ctx, opt, txn, &x.0, v).await?
|
2022-02-13 19:03:00 +00:00
|
|
|
}
|
2022-02-06 01:14:56 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 12:22:53 +00:00
|
|
|
Data::PatchExpression(data) => {
|
|
|
|
self.current.to_mut().patch(ctx, opt, txn, data).await?
|
2022-02-13 19:03:00 +00:00
|
|
|
}
|
2022-04-02 12:22:53 +00:00
|
|
|
Data::MergeExpression(data) => {
|
|
|
|
self.current.to_mut().merge(ctx, opt, txn, data).await?
|
|
|
|
}
|
|
|
|
Data::ReplaceExpression(data) => {
|
|
|
|
self.current.to_mut().replace(ctx, opt, txn, data).await?
|
|
|
|
}
|
|
|
|
Data::ContentExpression(data) => {
|
|
|
|
self.current.to_mut().replace(ctx, opt, txn, data).await?
|
2022-02-13 19:03:00 +00:00
|
|
|
}
|
2022-02-06 01:14:56 +00:00
|
|
|
_ => unreachable!(),
|
2022-03-04 16:01:32 +00:00
|
|
|
};
|
2022-02-06 01:14:56 +00:00
|
|
|
};
|
|
|
|
// Set default field values
|
2022-04-01 10:17:19 +00:00
|
|
|
self.current.to_mut().def(ctx, opt, txn, rid).await?;
|
2022-02-06 01:14:56 +00:00
|
|
|
// Carry on
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|