surrealdb_sql/statements/remove/
function.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};
10
11#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
12#[revisioned(revision = 1)]
13pub struct RemoveFunctionStatement {
14	pub name: Ident,
15}
16
17impl RemoveFunctionStatement {
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::Function, &Base::Db)?;
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::database::fc::new(opt.ns(), opt.db(), &self.name);
33		run.del(key).await?;
34		// Ok all good
35		Ok(Value::None)
36	}
37}
38
39impl Display for RemoveFunctionStatement {
40	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
41		// Bypass ident display since we don't want backticks arround the ident.
42		write!(f, "REMOVE FUNCTION fn::{}", self.name.0)
43	}
44}