diff --git a/lib/src/dbs/statement.rs b/lib/src/dbs/statement.rs index c95be73f..3aec735e 100644 --- a/lib/src/dbs/statement.rs +++ b/lib/src/dbs/statement.rs @@ -1,9 +1,11 @@ use crate::sql::cond::Cond; +use crate::sql::data::Data; use crate::sql::fetch::Fetchs; use crate::sql::field::Fields; use crate::sql::group::Groups; use crate::sql::limit::Limit; use crate::sql::order::Orders; +use crate::sql::output::Output; use crate::sql::split::Splits; use crate::sql::start::Start; use crate::sql::statements::create::CreateStatement; @@ -91,7 +93,15 @@ impl Statement { _ => None, } } - // Returns any SPLIT clause if specified + // Returns any SET clause if specified + pub fn data(self: &Statement) -> Option<&Data> { + match self { + Statement::Create(v) => v.data.as_ref(), + Statement::Update(v) => v.data.as_ref(), + _ => None, + } + } + // Returns any WHERE clause if specified pub fn conds(self: &Statement) -> Option<&Cond> { match self { Statement::Select(v) => v.cond.as_ref(), @@ -142,6 +152,17 @@ impl Statement { _ => None, } } + // Returns any RETURN clause if specified + pub fn output(self: &Statement) -> Option<&Output> { + match self { + Statement::Create(v) => v.output.as_ref(), + Statement::Update(v) => v.output.as_ref(), + Statement::Relate(v) => v.output.as_ref(), + Statement::Delete(v) => v.output.as_ref(), + Statement::Insert(v) => v.output.as_ref(), + _ => None, + } + } // Returns any VERSION clause if specified pub fn version(self: &Statement) -> Option<&Version> { match self { diff --git a/lib/src/doc/check.rs b/lib/src/doc/check.rs index 3f7fc1fe..f7a65a92 100644 --- a/lib/src/doc/check.rs +++ b/lib/src/doc/check.rs @@ -13,22 +13,13 @@ impl<'a> Document<'a> { txn: &Transaction, stm: &Statement, ) -> Result<(), Error> { - // Extract statement clause - let cond = match stm { - Statement::Select(stm) => stm.cond.as_ref(), - Statement::Update(stm) => stm.cond.as_ref(), - Statement::Delete(stm) => stm.cond.as_ref(), - _ => unreachable!(), - }; - // Match clause - match cond { - Some(v) => { - match v.expr.compute(ctx, opt, txn, Some(&self.current)).await?.is_truthy() { - false => Err(Error::Ignore), - true => Ok(()), - } + // Check where condition + if let Some(cond) = stm.conds() { + if cond.expr.compute(ctx, opt, txn, Some(&self.current)).await?.is_truthy() { + return Err(Error::Ignore); } - None => Ok(()), } + // Carry on + Ok(()) } } diff --git a/lib/src/doc/delete.rs b/lib/src/doc/delete.rs index 04e1e15a..51096885 100644 --- a/lib/src/doc/delete.rs +++ b/lib/src/doc/delete.rs @@ -22,7 +22,7 @@ impl<'a> Document<'a> { self.allow(ctx, opt, txn, stm).await?; // Erase document self.erase(ctx, opt, txn, stm).await?; - // Store index data + // Purge index data self.index(ctx, opt, txn, stm).await?; // Store record data self.store(ctx, opt, txn, stm).await?; diff --git a/lib/src/doc/empty.rs b/lib/src/doc/empty.rs index 561e26ae..e7420c23 100644 --- a/lib/src/doc/empty.rs +++ b/lib/src/doc/empty.rs @@ -15,7 +15,9 @@ impl<'a> Document<'a> { ) -> Result<(), Error> { // Check if this record exists if self.id.is_some() { + // There is no current record if self.current.is_none() { + // Ignore this requested record return Err(Error::Ignore); } } diff --git a/lib/src/doc/exist.rs b/lib/src/doc/exist.rs index 091ea036..74392212 100644 --- a/lib/src/doc/exist.rs +++ b/lib/src/doc/exist.rs @@ -15,7 +15,9 @@ impl<'a> Document<'a> { ) -> Result<(), Error> { // Check if this record exists if let Some(id) = &self.id { + // If there is a current value if self.current.is_some() { + // The record already exists return Err(Error::RecordExists { thing: id.clone(), }); diff --git a/lib/src/doc/merge.rs b/lib/src/doc/merge.rs index fc3bf56b..f7d70e0b 100644 --- a/lib/src/doc/merge.rs +++ b/lib/src/doc/merge.rs @@ -18,16 +18,10 @@ impl<'a> Document<'a> { ) -> 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 self.current.to_mut().def(ctx, opt, txn, id).await?; // The statement has a data clause - if let Some(v) = data { + if let Some(v) = stm.data() { match v { Data::SetExpression(x) => { for x in x.iter() { @@ -62,10 +56,6 @@ impl<'a> Document<'a> { }; // Set default field values self.current.to_mut().def(ctx, opt, txn, id).await?; - // Set ASSERT and VALUE clauses - // todo!(); - // Delete non-defined FIELDs - // todo!(); // Carry on Ok(()) } diff --git a/lib/src/doc/pluck.rs b/lib/src/doc/pluck.rs index 1372c516..db88da4d 100644 --- a/lib/src/doc/pluck.rs +++ b/lib/src/doc/pluck.rs @@ -17,20 +17,10 @@ impl<'a> Document<'a> { txn: &Transaction, stm: &Statement, ) -> Result { - // Extract statement clause - let expr = match stm { - Statement::Select(_) => None, - Statement::Create(stm) => stm.output.as_ref().or(Some(&Output::After)), - Statement::Update(stm) => stm.output.as_ref().or(Some(&Output::After)), - Statement::Relate(stm) => stm.output.as_ref().or(Some(&Output::After)), - Statement::Delete(stm) => stm.output.as_ref().or(Some(&Output::None)), - Statement::Insert(stm) => stm.output.as_ref().or(Some(&Output::After)), - _ => unreachable!(), - }; // Ensure futures are run let opt = &opt.futures(true); // Match clause - match expr { + match stm.output() { Some(v) => match v { Output::None => Err(Error::Ignore), Output::Null => Ok(Value::Null), @@ -59,16 +49,16 @@ impl<'a> Document<'a> { } }, None => match stm { - Statement::Select(stm) => { - let mut out = match stm.expr.all() { + Statement::Select(s) => { + let mut out = match s.expr.all() { true => self.current.compute(ctx, opt, txn, Some(&self.current)).await?, false => Value::base(), }; - for v in stm.expr.other() { + for v in s.expr.other() { match v { Field::All => (), Field::Alone(v) => match v { - Value::Function(f) if stm.group.is_some() && f.is_aggregate() => { + Value::Function(f) if s.group.is_some() && f.is_aggregate() => { let x = match f.args().len() { 0 => f.compute(ctx, opt, txn, Some(&self.current)).await?, _ => { @@ -85,7 +75,7 @@ impl<'a> Document<'a> { } }, Field::Alias(v, i) => match v { - Value::Function(f) if stm.group.is_some() && f.is_aggregate() => { + Value::Function(f) if s.group.is_some() && f.is_aggregate() => { let x = match f.args().len() { 0 => f.compute(ctx, opt, txn, Some(&self.current)).await?, _ => { @@ -105,7 +95,19 @@ impl<'a> Document<'a> { } Ok(out) } - _ => unreachable!(), + 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), }, } }