Move duplicate code into dedicated functions
This commit is contained in:
parent
f6b47d381b
commit
2a0c8ddb4a
6 changed files with 43 additions and 17 deletions
|
@ -17,7 +17,7 @@ impl<'a> Document<'a> {
|
||||||
// Check permission clause
|
// Check permission clause
|
||||||
if opt.perms && opt.auth.perms() && self.id.is_some() {
|
if opt.perms && opt.auth.perms() && self.id.is_some() {
|
||||||
// Get the table
|
// Get the table
|
||||||
let tb = self.tb(ctx, opt, txn).await?;
|
let tb = self.tb(opt, txn).await?;
|
||||||
// Get the permission clause
|
// Get the permission clause
|
||||||
let perms = if self.initial.is_none() {
|
let perms = if self.initial.is_none() {
|
||||||
&tb.permissions.create
|
&tb.permissions.create
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
use crate::dbs::Options;
|
use crate::dbs::Options;
|
||||||
use crate::dbs::Runtime;
|
|
||||||
use crate::dbs::Transaction;
|
use crate::dbs::Transaction;
|
||||||
use crate::err::Error;
|
use crate::err::Error;
|
||||||
|
use crate::sql::statements::define::DefineEventStatement;
|
||||||
|
use crate::sql::statements::define::DefineFieldStatement;
|
||||||
|
use crate::sql::statements::define::DefineIndexStatement;
|
||||||
use crate::sql::statements::define::DefineTableStatement;
|
use crate::sql::statements::define::DefineTableStatement;
|
||||||
use crate::sql::thing::Thing;
|
use crate::sql::thing::Thing;
|
||||||
use crate::sql::value::Value;
|
use crate::sql::value::Value;
|
||||||
|
@ -38,7 +40,6 @@ impl<'a> Document<'a> {
|
||||||
// Get the table for this document
|
// Get the table for this document
|
||||||
pub async fn tb(
|
pub async fn tb(
|
||||||
&self,
|
&self,
|
||||||
_ctx: &Runtime,
|
|
||||||
opt: &Options,
|
opt: &Options,
|
||||||
txn: &Transaction,
|
txn: &Transaction,
|
||||||
) -> Result<DefineTableStatement, Error> {
|
) -> Result<DefineTableStatement, Error> {
|
||||||
|
@ -47,4 +48,37 @@ impl<'a> Document<'a> {
|
||||||
// Get the table definition
|
// Get the table definition
|
||||||
txn.clone().lock().await.get_tb(opt.ns(), opt.db(), &id.tb).await
|
txn.clone().lock().await.get_tb(opt.ns(), opt.db(), &id.tb).await
|
||||||
}
|
}
|
||||||
|
// Get the events for this document
|
||||||
|
pub async fn ev(
|
||||||
|
&self,
|
||||||
|
opt: &Options,
|
||||||
|
txn: &Transaction,
|
||||||
|
) -> Result<Vec<DefineEventStatement>, Error> {
|
||||||
|
// Get the record id
|
||||||
|
let id = self.id.as_ref().unwrap();
|
||||||
|
// Get the table definition
|
||||||
|
txn.clone().lock().await.all_ev(opt.ns(), opt.db(), &id.tb).await
|
||||||
|
}
|
||||||
|
// Get the fields for this document
|
||||||
|
pub async fn fd(
|
||||||
|
&self,
|
||||||
|
opt: &Options,
|
||||||
|
txn: &Transaction,
|
||||||
|
) -> Result<Vec<DefineFieldStatement>, Error> {
|
||||||
|
// Get the record id
|
||||||
|
let id = self.id.as_ref().unwrap();
|
||||||
|
// Get the table definition
|
||||||
|
txn.clone().lock().await.all_fd(opt.ns(), opt.db(), &id.tb).await
|
||||||
|
}
|
||||||
|
// Get the indexes for this document
|
||||||
|
pub async fn ix(
|
||||||
|
&self,
|
||||||
|
opt: &Options,
|
||||||
|
txn: &Transaction,
|
||||||
|
) -> Result<Vec<DefineIndexStatement>, Error> {
|
||||||
|
// Get the record id
|
||||||
|
let id = self.id.as_ref().unwrap();
|
||||||
|
// Get the table definition
|
||||||
|
txn.clone().lock().await.all_ix(opt.ns(), opt.db(), &id.tb).await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,12 +24,8 @@ impl<'a> Document<'a> {
|
||||||
if !opt.force && !self.changed() {
|
if !opt.force && !self.changed() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
// Get the record id
|
|
||||||
let rid = self.id.as_ref().unwrap();
|
|
||||||
// Get all event statements
|
|
||||||
let evs = txn.clone().lock().await.all_ev(opt.ns(), opt.db(), &rid.tb).await?;
|
|
||||||
// Loop through all event statements
|
// Loop through all event statements
|
||||||
for ev in evs.iter() {
|
for ev in self.ev(opt, txn).await?.iter() {
|
||||||
// Get the event action
|
// Get the event action
|
||||||
let met = if self.initial.is_none() {
|
let met = if self.initial.is_none() {
|
||||||
Value::from("CREATE")
|
Value::from("CREATE")
|
||||||
|
|
|
@ -17,12 +17,8 @@ impl<'a> Document<'a> {
|
||||||
txn: &Transaction,
|
txn: &Transaction,
|
||||||
_stm: &Statement,
|
_stm: &Statement,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
// Get the record id
|
|
||||||
let rid = self.id.as_ref().unwrap();
|
|
||||||
// Get all field statements
|
|
||||||
let fds = txn.clone().lock().await.all_fd(opt.ns(), opt.db(), &rid.tb).await?;
|
|
||||||
// Loop through all field statements
|
// Loop through all field statements
|
||||||
for fd in fds.iter() {
|
for fd in self.fd(opt, txn).await?.iter() {
|
||||||
// Get the initial value
|
// Get the initial value
|
||||||
let old = self.initial.get(ctx, opt, txn, &fd.name).await?;
|
let old = self.initial.get(ctx, opt, txn, &fd.name).await?;
|
||||||
// Get the current value
|
// Get the current value
|
||||||
|
|
|
@ -8,7 +8,7 @@ use crate::err::Error;
|
||||||
impl<'a> Document<'a> {
|
impl<'a> Document<'a> {
|
||||||
pub async fn purge(
|
pub async fn purge(
|
||||||
&self,
|
&self,
|
||||||
ctx: &Runtime,
|
_ctx: &Runtime,
|
||||||
opt: &Options,
|
opt: &Options,
|
||||||
txn: &Transaction,
|
txn: &Transaction,
|
||||||
_stm: &Statement,
|
_stm: &Statement,
|
||||||
|
@ -18,7 +18,7 @@ impl<'a> Document<'a> {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
// Check if the table is a view
|
// Check if the table is a view
|
||||||
if self.tb(ctx, opt, txn).await?.drop {
|
if self.tb(opt, txn).await?.drop {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
// Clone transaction
|
// Clone transaction
|
||||||
|
|
|
@ -8,7 +8,7 @@ use crate::err::Error;
|
||||||
impl<'a> Document<'a> {
|
impl<'a> Document<'a> {
|
||||||
pub async fn store(
|
pub async fn store(
|
||||||
&self,
|
&self,
|
||||||
ctx: &Runtime,
|
_ctx: &Runtime,
|
||||||
opt: &Options,
|
opt: &Options,
|
||||||
txn: &Transaction,
|
txn: &Transaction,
|
||||||
_stm: &Statement,
|
_stm: &Statement,
|
||||||
|
@ -18,7 +18,7 @@ impl<'a> Document<'a> {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
// Check if the table is a view
|
// Check if the table is a view
|
||||||
if self.tb(ctx, opt, txn).await?.drop {
|
if self.tb(opt, txn).await?.drop {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
// Clone transaction
|
// Clone transaction
|
||||||
|
|
Loading…
Reference in a new issue