From 5ae50469872df6ecf15e43f9ca6b7af1f10ef4cd Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sun, 20 Mar 2022 14:27:56 +0000 Subject: [PATCH] Implement REMOVE statements --- lib/src/key/mod.rs | 44 ++++---- lib/src/sql/statements/remove.rs | 169 +++++++++++++++++++++++++------ 2 files changed, 159 insertions(+), 54 deletions(-) diff --git a/lib/src/key/mod.rs b/lib/src/key/mod.rs index baf722f8..b7171c9b 100644 --- a/lib/src/key/mod.rs +++ b/lib/src/key/mod.rs @@ -1,30 +1,32 @@ /// KV / -/// NS /!ns{$ns} +/// NS /!ns{ns} /// -/// Namespace /*{$ns} -/// NL /*{$ns}!nl{$us} -/// NT /*{$ns}!nt{$tk} -/// DB /*{$ns}!db{$db} +/// Namespace /*{ns} +/// NL /*{ns}!nl{us} +/// NT /*{ns}!nt{tk} +/// DB /*{ns}!db{db} /// -/// Database /*{$ns}*{$db} -/// DL /*{$ns}*{$db}!dl{$us} -/// DT /*{$ns}*{$db}!dt{$tk} -/// SC /*{$ns}*{$db}!sc{$sc} -/// ST /*{$ns}*{$db}!st{$sc}!tk{$tk} -/// TB /*{$ns}*{$db}!tb{$tb} +/// Database /*{ns}*{db} +/// DL /*{ns}*{db}!dl{us} +/// DT /*{ns}*{db}!dt{tk} +/// SC /*{ns}*{db}!sc{sc} +/// ST /*{ns}*{db}!st{sc}!tk{tk} +/// TB /*{ns}*{db}!tb{tb} /// -/// Table /*{$ns}*{$db}*{$tb} -/// FT /*{$ns}*{$db}*{$tb}!ft{$ft} -/// FD /*{$ns}*{$db}*{$tb}!fd{$fd} -/// EV /*{$ns}*{$db}*{$tb}!ev{$ev} -/// IX /*{$ns}*{$db}*{$tb}!ix{$ix} -/// LV /*{$ns}*{$db}*{$tb}!lv{$lv} +/// Table /*{ns}*{db}*{tb} +/// FT /*{ns}*{db}*{tb}!ft{ft} +/// FD /*{ns}*{db}*{tb}!fd{fd} +/// EV /*{ns}*{db}*{tb}!ev{ev} +/// IX /*{ns}*{db}*{tb}!ix{ix} +/// LV /*{ns}*{db}*{tb}!lv{lv} /// -/// Thing /*{$ns}*{$db}*{$tb}*{$id} +/// Thing /*{ns}*{db}*{tb}*{id} /// -/// Guide /*{$ns}*{$db}*{$tb}¤{$ix} -/// Index /*{$ns}*{$db}*{$tb}¤{$ix}{$fd} -/// Point /*{$ns}*{$db}*{$tb}¤{$ix}{$fd}{$id} +/// Graph /*{ns}*{db}*{tb}~{id}{gt}{fk} +/// +/// Guide /*{ns}*{db}*{tb}¤{ix} +/// Index /*{ns}*{db}*{tb}¤{ix}{fd} +/// Point /*{ns}*{db}*{tb}¤{ix}{fd}{id} /// pub mod database; pub mod db; diff --git a/lib/src/sql/statements/remove.rs b/lib/src/sql/statements/remove.rs index 03610627..1808c9ef 100644 --- a/lib/src/sql/statements/remove.rs +++ b/lib/src/sql/statements/remove.rs @@ -95,13 +95,23 @@ impl RemoveNamespaceStatement { &self, _ctx: &Runtime, opt: &Options, - _txn: &Transaction, + txn: &Transaction, _doc: Option<&Value>, ) -> Result { // Allowed to run? opt.check(Level::Kv)?; - // Continue - todo!() + // Clone transaction + let run = txn.clone(); + // Claim transaction + let mut run = run.lock().await; + // Delete the definition + let key = crate::key::ns::new(&self.name); + run.del(key).await?; + // Delete the resource data + let key = crate::key::namespace::new(&self.name); + run.delp(key, u32::MAX).await?; + // Ok all good + Ok(Value::None) } } @@ -139,13 +149,23 @@ impl RemoveDatabaseStatement { &self, _ctx: &Runtime, opt: &Options, - _txn: &Transaction, + txn: &Transaction, _doc: Option<&Value>, ) -> Result { // Allowed to run? opt.check(Level::Ns)?; - // Continue - todo!() + // Clone transaction + let run = txn.clone(); + // Claim transaction + let mut run = run.lock().await; + // Delete the definition + let key = crate::key::db::new(opt.ns(), &self.name); + run.del(key).await?; + // Delete the resource data + let key = crate::key::database::new(opt.ns(), &self.name); + run.delp(key, u32::MAX).await?; + // Ok all good + Ok(Value::None) } } @@ -184,17 +204,38 @@ impl RemoveLoginStatement { &self, _ctx: &Runtime, opt: &Options, - _txn: &Transaction, + txn: &Transaction, _doc: Option<&Value>, ) -> Result { - // Allowed to run? match self.base { - Base::Ns => opt.check(Level::Kv)?, - Base::Db => opt.check(Level::Ns)?, + Base::Ns => { + // Allowed to run? + opt.check(Level::Kv)?; + // Clone transaction + let run = txn.clone(); + // Claim transaction + let mut run = run.lock().await; + // Delete the definition + let key = crate::key::nl::new(opt.ns(), &self.name); + run.del(key).await?; + // Ok all good + Ok(Value::None) + } + Base::Db => { + // Allowed to run? + opt.check(Level::Ns)?; + // Clone transaction + let run = txn.clone(); + // Claim transaction + let mut run = run.lock().await; + // Delete the definition + let key = crate::key::dl::new(opt.ns(), opt.db(), &self.name); + run.del(key).await?; + // Ok all good + Ok(Value::None) + } _ => unreachable!(), } - // Continue - todo!() } } @@ -238,17 +279,38 @@ impl RemoveTokenStatement { &self, _ctx: &Runtime, opt: &Options, - _txn: &Transaction, + txn: &Transaction, _doc: Option<&Value>, ) -> Result { - // Allowed to run? match self.base { - Base::Ns => opt.check(Level::Kv)?, - Base::Db => opt.check(Level::Ns)?, + Base::Ns => { + // Allowed to run? + opt.check(Level::Kv)?; + // Clone transaction + let run = txn.clone(); + // Claim transaction + let mut run = run.lock().await; + // Delete the definition + let key = crate::key::nt::new(opt.ns(), &self.name); + run.del(key).await?; + // Ok all good + Ok(Value::None) + } + Base::Db => { + // Allowed to run? + opt.check(Level::Ns)?; + // Clone transaction + let run = txn.clone(); + // Claim transaction + let mut run = run.lock().await; + // Delete the definition + let key = crate::key::dt::new(opt.ns(), opt.db(), &self.name); + run.del(key).await?; + // Ok all good + Ok(Value::None) + } _ => unreachable!(), } - // Continue - todo!() } } @@ -291,13 +353,20 @@ impl RemoveScopeStatement { &self, _ctx: &Runtime, opt: &Options, - _txn: &Transaction, + txn: &Transaction, _doc: Option<&Value>, ) -> Result { // Allowed to run? opt.check(Level::Db)?; - // Continue - todo!() + // Clone transaction + let run = txn.clone(); + // Claim transaction + let mut run = run.lock().await; + // Delete the definition + let key = crate::key::sc::new(opt.ns(), opt.db(), &self.name); + run.del(key).await?; + // Ok all good + Ok(Value::None) } } @@ -335,13 +404,23 @@ impl RemoveTableStatement { &self, _ctx: &Runtime, opt: &Options, - _txn: &Transaction, + txn: &Transaction, _doc: Option<&Value>, ) -> Result { // Allowed to run? opt.check(Level::Db)?; - // Continue - todo!() + // Clone transaction + let run = txn.clone(); + // Claim transaction + let mut run = run.lock().await; + // Delete the definition + let key = crate::key::tb::new(opt.ns(), opt.db(), &self.name); + run.del(key).await?; + // Remove the resource data + let key = crate::key::table::new(opt.ns(), opt.db(), &self.name); + run.delp(key, u32::MAX).await?; + // Ok all good + Ok(Value::None) } } @@ -380,13 +459,20 @@ impl RemoveEventStatement { &self, _ctx: &Runtime, opt: &Options, - _txn: &Transaction, + txn: &Transaction, _doc: Option<&Value>, ) -> Result { // Allowed to run? opt.check(Level::Db)?; - // Continue - todo!() + // Clone transaction + let run = txn.clone(); + // Claim transaction + let mut run = run.lock().await; + // Delete the definition + let key = crate::key::ev::new(opt.ns(), opt.db(), &self.what, &self.name); + run.del(key).await?; + // Ok all good + Ok(Value::None) } } @@ -431,13 +517,20 @@ impl RemoveFieldStatement { &self, _ctx: &Runtime, opt: &Options, - _txn: &Transaction, + txn: &Transaction, _doc: Option<&Value>, ) -> Result { // Allowed to run? opt.check(Level::Db)?; - // Continue - todo!() + // Clone transaction + let run = txn.clone(); + // Claim transaction + let mut run = run.lock().await; + // Delete the definition + let key = crate::key::fd::new(opt.ns(), opt.db(), &self.what, &self.name); + run.del(key).await?; + // Ok all good + Ok(Value::None) } } @@ -482,13 +575,23 @@ impl RemoveIndexStatement { &self, _ctx: &Runtime, opt: &Options, - _txn: &Transaction, + txn: &Transaction, _doc: Option<&Value>, ) -> Result { // Allowed to run? opt.check(Level::Db)?; - // Continue - todo!() + // Clone transaction + let run = txn.clone(); + // Claim transaction + let mut run = run.lock().await; + // Delete the definition + let key = crate::key::ix::new(opt.ns(), opt.db(), &self.what, &self.name); + run.del(key).await?; + // Remove the resource data + let key = crate::key::guide::new(opt.ns(), opt.db(), &self.what, &self.name); + run.delp(key, u32::MAX).await?; + // Ok all good + Ok(Value::None) } }