Make code more consistent and simpler to read

This commit is contained in:
Tobie Morgan Hitchcock 2022-04-01 10:15:52 +01:00
parent a938f017c4
commit 1ea692d32e
7 changed files with 53 additions and 45 deletions

View file

@ -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 {

View file

@ -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(())
}
}

View file

@ -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?;

View file

@ -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);
}
}

View file

@ -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(),
});

View file

@ -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(())
}

View file

@ -17,20 +17,10 @@ impl<'a> Document<'a> {
txn: &Transaction,
stm: &Statement,
) -> Result<Value, Error> {
// 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),
},
}
}