surrealdb_sql/statements/remove/
index.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 RemoveIndexStatement {
14	pub name: Ident,
15	pub what: Ident,
16}
17
18impl RemoveIndexStatement {
19	/// Process this type returning a computed simple Value
20	pub(crate) async fn compute(
21		&self,
22		_ctx: &Context<'_>,
23		opt: &Options,
24		txn: &Transaction,
25	) -> Result<Value, Error> {
26		// Allowed to run?
27		opt.is_allowed(Action::Edit, ResourceKind::Index, &Base::Db)?;
28		// Claim transaction
29		let mut run = txn.lock().await;
30		// Clear the cache
31		run.clear_cache();
32		// Delete the definition
33		let key = crate::key::table::ix::new(opt.ns(), opt.db(), &self.what, &self.name);
34		run.del(key).await?;
35		// Remove the index data
36		let key = crate::key::index::all::new(opt.ns(), opt.db(), &self.what, &self.name);
37		run.delp(key, u32::MAX).await?;
38		// Clear the cache
39		let key = crate::key::table::ix::prefix(opt.ns(), opt.db(), &self.what);
40		run.clr(key).await?;
41		// Ok all good
42		Ok(Value::None)
43	}
44}
45
46impl Display for RemoveIndexStatement {
47	fn fmt(&self, f: &mut Formatter) -> fmt::Result {
48		write!(f, "REMOVE INDEX {} ON {}", self.name, self.what)
49	}
50}