surrealdb_core/sql/statements/remove/
analyzer.rs

1use crate::ctx::Context;
2use crate::dbs::Options;
3use crate::err::Error;
4use crate::iam::{Action, ResourceKind};
5use crate::sql::{Base, Ident, Value};
6use derive::Store;
7use revision::revisioned;
8use serde::{Deserialize, Serialize};
9use std::fmt::{self, Display, Formatter};
10
11#[revisioned(revision = 2)]
12#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
13#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
14#[non_exhaustive]
15pub struct RemoveAnalyzerStatement {
16	pub name: Ident,
17	#[revision(start = 2)]
18	pub if_exists: bool,
19}
20
21impl RemoveAnalyzerStatement {
22	pub(crate) async fn compute(&self, ctx: &Context<'_>, opt: &Options) -> Result<Value, Error> {
23		let future = async {
24			// Allowed to run?
25			opt.is_allowed(Action::Edit, ResourceKind::Analyzer, &Base::Db)?;
26			// Get the transaction
27			let txn = ctx.tx();
28			// Get the definition
29			let az = txn.get_db_analyzer(opt.ns()?, opt.db()?, &self.name).await?;
30			// Delete the definition
31			let key = crate::key::database::az::new(opt.ns()?, opt.db()?, &az.name);
32			txn.del(key).await?;
33			// Clear the cache
34			txn.clear();
35			// TODO Check that the analyzer is not used in any schema
36			// Ok all good
37			Ok(Value::None)
38		}
39		.await;
40		match future {
41			Err(Error::AzNotFound {
42				..
43			}) if self.if_exists => Ok(Value::None),
44			v => v,
45		}
46	}
47}
48
49impl Display for RemoveAnalyzerStatement {
50	fn fmt(&self, f: &mut Formatter) -> fmt::Result {
51		write!(f, "REMOVE ANALYZER")?;
52		if self.if_exists {
53			write!(f, " IF EXISTS")?
54		}
55		write!(f, " {}", self.name)?;
56		Ok(())
57	}
58}