From 2a0c8ddb4a9fadbb137fe86c550d2e9720deaae8 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Thu, 7 Apr 2022 00:14:26 +0100 Subject: [PATCH] Move duplicate code into dedicated functions --- lib/src/doc/allow.rs | 2 +- lib/src/doc/document.rs | 38 ++++++++++++++++++++++++++++++++++++-- lib/src/doc/event.rs | 6 +----- lib/src/doc/field.rs | 6 +----- lib/src/doc/purge.rs | 4 ++-- lib/src/doc/store.rs | 4 ++-- 6 files changed, 43 insertions(+), 17 deletions(-) diff --git a/lib/src/doc/allow.rs b/lib/src/doc/allow.rs index 5e1a0ae5..99bd0493 100644 --- a/lib/src/doc/allow.rs +++ b/lib/src/doc/allow.rs @@ -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 diff --git a/lib/src/doc/document.rs b/lib/src/doc/document.rs index dc1835e1..74aadd29 100644 --- a/lib/src/doc/document.rs +++ b/lib/src/doc/document.rs @@ -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 { @@ -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, 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, 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, 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 + } } diff --git a/lib/src/doc/event.rs b/lib/src/doc/event.rs index 893860c2..7467622b 100644 --- a/lib/src/doc/event.rs +++ b/lib/src/doc/event.rs @@ -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") diff --git a/lib/src/doc/field.rs b/lib/src/doc/field.rs index 4a99d89e..743a968b 100644 --- a/lib/src/doc/field.rs +++ b/lib/src/doc/field.rs @@ -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 diff --git a/lib/src/doc/purge.rs b/lib/src/doc/purge.rs index 8a4814db..3c5e4200 100644 --- a/lib/src/doc/purge.rs +++ b/lib/src/doc/purge.rs @@ -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 diff --git a/lib/src/doc/store.rs b/lib/src/doc/store.rs index 4512e63b..14d4a609 100644 --- a/lib/src/doc/store.rs +++ b/lib/src/doc/store.rs @@ -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