Make code more consistent and simpler to read
This commit is contained in:
parent
a938f017c4
commit
1ea692d32e
7 changed files with 53 additions and 45 deletions
|
@ -1,9 +1,11 @@
|
||||||
use crate::sql::cond::Cond;
|
use crate::sql::cond::Cond;
|
||||||
|
use crate::sql::data::Data;
|
||||||
use crate::sql::fetch::Fetchs;
|
use crate::sql::fetch::Fetchs;
|
||||||
use crate::sql::field::Fields;
|
use crate::sql::field::Fields;
|
||||||
use crate::sql::group::Groups;
|
use crate::sql::group::Groups;
|
||||||
use crate::sql::limit::Limit;
|
use crate::sql::limit::Limit;
|
||||||
use crate::sql::order::Orders;
|
use crate::sql::order::Orders;
|
||||||
|
use crate::sql::output::Output;
|
||||||
use crate::sql::split::Splits;
|
use crate::sql::split::Splits;
|
||||||
use crate::sql::start::Start;
|
use crate::sql::start::Start;
|
||||||
use crate::sql::statements::create::CreateStatement;
|
use crate::sql::statements::create::CreateStatement;
|
||||||
|
@ -91,7 +93,15 @@ impl Statement {
|
||||||
_ => None,
|
_ => 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> {
|
pub fn conds(self: &Statement) -> Option<&Cond> {
|
||||||
match self {
|
match self {
|
||||||
Statement::Select(v) => v.cond.as_ref(),
|
Statement::Select(v) => v.cond.as_ref(),
|
||||||
|
@ -142,6 +152,17 @@ impl Statement {
|
||||||
_ => None,
|
_ => 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
|
// Returns any VERSION clause if specified
|
||||||
pub fn version(self: &Statement) -> Option<&Version> {
|
pub fn version(self: &Statement) -> Option<&Version> {
|
||||||
match self {
|
match self {
|
||||||
|
|
|
@ -13,22 +13,13 @@ impl<'a> Document<'a> {
|
||||||
txn: &Transaction,
|
txn: &Transaction,
|
||||||
stm: &Statement,
|
stm: &Statement,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
// Extract statement clause
|
// Check where condition
|
||||||
let cond = match stm {
|
if let Some(cond) = stm.conds() {
|
||||||
Statement::Select(stm) => stm.cond.as_ref(),
|
if cond.expr.compute(ctx, opt, txn, Some(&self.current)).await?.is_truthy() {
|
||||||
Statement::Update(stm) => stm.cond.as_ref(),
|
return Err(Error::Ignore);
|
||||||
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(()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => Ok(()),
|
// Carry on
|
||||||
}
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ impl<'a> Document<'a> {
|
||||||
self.allow(ctx, opt, txn, stm).await?;
|
self.allow(ctx, opt, txn, stm).await?;
|
||||||
// Erase document
|
// Erase document
|
||||||
self.erase(ctx, opt, txn, stm).await?;
|
self.erase(ctx, opt, txn, stm).await?;
|
||||||
// Store index data
|
// Purge index data
|
||||||
self.index(ctx, opt, txn, stm).await?;
|
self.index(ctx, opt, txn, stm).await?;
|
||||||
// Store record data
|
// Store record data
|
||||||
self.store(ctx, opt, txn, stm).await?;
|
self.store(ctx, opt, txn, stm).await?;
|
||||||
|
|
|
@ -15,7 +15,9 @@ impl<'a> Document<'a> {
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
// Check if this record exists
|
// Check if this record exists
|
||||||
if self.id.is_some() {
|
if self.id.is_some() {
|
||||||
|
// There is no current record
|
||||||
if self.current.is_none() {
|
if self.current.is_none() {
|
||||||
|
// Ignore this requested record
|
||||||
return Err(Error::Ignore);
|
return Err(Error::Ignore);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,9 @@ impl<'a> Document<'a> {
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
// Check if this record exists
|
// Check if this record exists
|
||||||
if let Some(id) = &self.id {
|
if let Some(id) = &self.id {
|
||||||
|
// If there is a current value
|
||||||
if self.current.is_some() {
|
if self.current.is_some() {
|
||||||
|
// The record already exists
|
||||||
return Err(Error::RecordExists {
|
return Err(Error::RecordExists {
|
||||||
thing: id.clone(),
|
thing: id.clone(),
|
||||||
});
|
});
|
||||||
|
|
|
@ -18,16 +18,10 @@ impl<'a> Document<'a> {
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
// Get the ID reference
|
// Get the ID reference
|
||||||
let id = self.id.as_ref();
|
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
|
// Set default field values
|
||||||
self.current.to_mut().def(ctx, opt, txn, id).await?;
|
self.current.to_mut().def(ctx, opt, txn, id).await?;
|
||||||
// The statement has a data clause
|
// The statement has a data clause
|
||||||
if let Some(v) = data {
|
if let Some(v) = stm.data() {
|
||||||
match v {
|
match v {
|
||||||
Data::SetExpression(x) => {
|
Data::SetExpression(x) => {
|
||||||
for x in x.iter() {
|
for x in x.iter() {
|
||||||
|
@ -62,10 +56,6 @@ impl<'a> Document<'a> {
|
||||||
};
|
};
|
||||||
// Set default field values
|
// Set default field values
|
||||||
self.current.to_mut().def(ctx, opt, txn, id).await?;
|
self.current.to_mut().def(ctx, opt, txn, id).await?;
|
||||||
// Set ASSERT and VALUE clauses
|
|
||||||
// todo!();
|
|
||||||
// Delete non-defined FIELDs
|
|
||||||
// todo!();
|
|
||||||
// Carry on
|
// Carry on
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,20 +17,10 @@ impl<'a> Document<'a> {
|
||||||
txn: &Transaction,
|
txn: &Transaction,
|
||||||
stm: &Statement,
|
stm: &Statement,
|
||||||
) -> Result<Value, Error> {
|
) -> 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
|
// Ensure futures are run
|
||||||
let opt = &opt.futures(true);
|
let opt = &opt.futures(true);
|
||||||
// Match clause
|
// Match clause
|
||||||
match expr {
|
match stm.output() {
|
||||||
Some(v) => match v {
|
Some(v) => match v {
|
||||||
Output::None => Err(Error::Ignore),
|
Output::None => Err(Error::Ignore),
|
||||||
Output::Null => Ok(Value::Null),
|
Output::Null => Ok(Value::Null),
|
||||||
|
@ -59,16 +49,16 @@ impl<'a> Document<'a> {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None => match stm {
|
None => match stm {
|
||||||
Statement::Select(stm) => {
|
Statement::Select(s) => {
|
||||||
let mut out = match stm.expr.all() {
|
let mut out = match s.expr.all() {
|
||||||
true => self.current.compute(ctx, opt, txn, Some(&self.current)).await?,
|
true => self.current.compute(ctx, opt, txn, Some(&self.current)).await?,
|
||||||
false => Value::base(),
|
false => Value::base(),
|
||||||
};
|
};
|
||||||
for v in stm.expr.other() {
|
for v in s.expr.other() {
|
||||||
match v {
|
match v {
|
||||||
Field::All => (),
|
Field::All => (),
|
||||||
Field::Alone(v) => match v {
|
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() {
|
let x = match f.args().len() {
|
||||||
0 => f.compute(ctx, opt, txn, Some(&self.current)).await?,
|
0 => f.compute(ctx, opt, txn, Some(&self.current)).await?,
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -85,7 +75,7 @@ impl<'a> Document<'a> {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Field::Alias(v, i) => match v {
|
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() {
|
let x = match f.args().len() {
|
||||||
0 => f.compute(ctx, opt, txn, Some(&self.current)).await?,
|
0 => f.compute(ctx, opt, txn, Some(&self.current)).await?,
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -105,7 +95,19 @@ impl<'a> Document<'a> {
|
||||||
}
|
}
|
||||||
Ok(out)
|
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),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue