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 01:00:30 +00:00
|
|
|
txn: &Transaction<'_>,
|
2022-02-06 01:14:56 +00:00
|
|
|
stm: &Statement<'_>,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
// Get the ID reference
|
|
|
|
let id = self.id.as_ref();
|
|
|
|
// Extract statement clause
|
|
|
|
let data = match stm {
|
|
|
|
Statement::Create(stm) => stm.data.as_ref(),
|
|
|
|
Statement::Update(stm) => stm.data.as_ref(),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
// Set default field values
|
2022-02-15 01:00:30 +00:00
|
|
|
self.current.to_mut().def(ctx, opt, txn, id).await?;
|
2022-02-06 01:14:56 +00:00
|
|
|
// Check for a data clause
|
|
|
|
match data {
|
|
|
|
// The statement has a data clause
|
|
|
|
Some(v) => match v {
|
|
|
|
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-02-15 01:00:30 +00:00
|
|
|
Data::PatchExpression(v) => self.current.to_mut().patch(ctx, opt, txn, v).await?,
|
|
|
|
Data::MergeExpression(v) => self.current.to_mut().merge(ctx, opt, txn, v).await?,
|
2022-02-13 19:03:00 +00:00
|
|
|
Data::ReplaceExpression(v) => {
|
2022-02-15 01:00:30 +00:00
|
|
|
self.current.to_mut().replace(ctx, opt, txn, v).await?
|
2022-02-13 19:03:00 +00:00
|
|
|
}
|
|
|
|
Data::ContentExpression(v) => {
|
2022-02-15 01:00:30 +00:00
|
|
|
self.current.to_mut().replace(ctx, opt, txn, v).await?
|
2022-02-13 19:03:00 +00:00
|
|
|
}
|
2022-02-06 01:14:56 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
|
|
|
// No data clause has been set
|
|
|
|
None => (),
|
|
|
|
};
|
|
|
|
// Set default field values
|
2022-02-15 01:00:30 +00:00
|
|
|
self.current.to_mut().def(ctx, opt, txn, id).await?;
|
2022-02-06 01:14:56 +00:00
|
|
|
// Set ASSERT and VALUE clauses
|
|
|
|
// todo!();
|
|
|
|
// Delete non-defined FIELDs
|
|
|
|
// todo!();
|
|
|
|
// Carry on
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|