Move duplicate code into dedicated functions

This commit is contained in:
Tobie Morgan Hitchcock 2022-04-07 00:14:26 +01:00
parent f6b47d381b
commit 2a0c8ddb4a
6 changed files with 43 additions and 17 deletions

View file

@ -17,7 +17,7 @@ impl<'a> Document<'a> {
// Check permission clause
if opt.perms && opt.auth.perms() && self.id.is_some() {
// Get the table
let tb = self.tb(ctx, opt, txn).await?;
let tb = self.tb(opt, txn).await?;
// Get the permission clause
let perms = if self.initial.is_none() {
&tb.permissions.create

View file

@ -1,7 +1,9 @@
use crate::dbs::Options;
use crate::dbs::Runtime;
use crate::dbs::Transaction;
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::thing::Thing;
use crate::sql::value::Value;
@ -38,7 +40,6 @@ impl<'a> Document<'a> {
// Get the table for this document
pub async fn tb(
&self,
_ctx: &Runtime,
opt: &Options,
txn: &Transaction,
) -> Result<DefineTableStatement, Error> {
@ -47,4 +48,37 @@ impl<'a> Document<'a> {
// Get the table definition
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
}
}

View file

@ -24,12 +24,8 @@ impl<'a> Document<'a> {
if !opt.force && !self.changed() {
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
for ev in evs.iter() {
for ev in self.ev(opt, txn).await?.iter() {
// Get the event action
let met = if self.initial.is_none() {
Value::from("CREATE")

View file

@ -17,12 +17,8 @@ impl<'a> Document<'a> {
txn: &Transaction,
_stm: &Statement,
) -> 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
for fd in fds.iter() {
for fd in self.fd(opt, txn).await?.iter() {
// Get the initial value
let old = self.initial.get(ctx, opt, txn, &fd.name).await?;
// Get the current value

View file

@ -8,7 +8,7 @@ use crate::err::Error;
impl<'a> Document<'a> {
pub async fn purge(
&self,
ctx: &Runtime,
_ctx: &Runtime,
opt: &Options,
txn: &Transaction,
_stm: &Statement,
@ -18,7 +18,7 @@ impl<'a> Document<'a> {
return Ok(());
}
// Check if the table is a view
if self.tb(ctx, opt, txn).await?.drop {
if self.tb(opt, txn).await?.drop {
return Ok(());
}
// Clone transaction

View file

@ -8,7 +8,7 @@ use crate::err::Error;
impl<'a> Document<'a> {
pub async fn store(
&self,
ctx: &Runtime,
_ctx: &Runtime,
opt: &Options,
txn: &Transaction,
_stm: &Statement,
@ -18,7 +18,7 @@ impl<'a> Document<'a> {
return Ok(());
}
// Check if the table is a view
if self.tb(ctx, opt, txn).await?.drop {
if self.tb(opt, txn).await?.drop {
return Ok(());
}
// Clone transaction