surrealdb_sql/statements/remove/
namespace.rs

1use crate::ctx::Context;
2use crate::dbs::{Options, Transaction};
3use crate::err::Error;
4use crate::iam::{Action, ResourceKind};
5use crate::{Base, Ident, Value};
6use derive::Store;
7use revision::revisioned;
8use serde::{Deserialize, Serialize};
9use std::fmt::{self, Display, Formatter};
10
11#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
12#[revisioned(revision = 1)]
13pub struct RemoveNamespaceStatement {
14	pub name: Ident,
15}
16
17impl RemoveNamespaceStatement {
18	/// Process this type returning a computed simple Value
19	pub(crate) async fn compute(
20		&self,
21		_ctx: &Context<'_>,
22		opt: &Options,
23		txn: &Transaction,
24	) -> Result<Value, Error> {
25		// Allowed to run?
26		opt.is_allowed(Action::Edit, ResourceKind::Namespace, &Base::Root)?;
27		// Claim transaction
28		let mut run = txn.lock().await;
29		// Clear the cache
30		run.clear_cache();
31		// Delete the definition
32		let key = crate::key::root::ns::new(&self.name);
33		run.del(key).await?;
34		// Delete the resource data
35		let key = crate::key::namespace::all::new(&self.name);
36		run.delp(key, u32::MAX).await?;
37		// Ok all good
38		Ok(Value::None)
39	}
40}
41
42impl Display for RemoveNamespaceStatement {
43	fn fmt(&self, f: &mut Formatter) -> fmt::Result {
44		write!(f, "REMOVE NAMESPACE {}", self.name)
45	}
46}