surrealdb_core/sql/statements/remove/
function.rs1use crate::ctx::Context;
2use crate::dbs::Options;
3use crate::err::Error;
4use crate::iam::{Action, ResourceKind};
5use crate::sql::{Base, Ident, Value};
6
7use revision::revisioned;
8use serde::{Deserialize, Serialize};
9use std::fmt::{self, Display};
10
11#[revisioned(revision = 2)]
12#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
13#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
14#[non_exhaustive]
15pub struct RemoveFunctionStatement {
16 pub name: Ident,
17 #[revision(start = 2)]
18 pub if_exists: bool,
19}
20
21impl RemoveFunctionStatement {
22 pub(crate) async fn compute(&self, ctx: &Context, opt: &Options) -> Result<Value, Error> {
24 let future = async {
25 opt.is_allowed(Action::Edit, ResourceKind::Function, &Base::Db)?;
27 let txn = ctx.tx();
29 let (ns, db) = opt.ns_db()?;
31 let fc = txn.get_db_function(ns, db, &self.name).await?;
32 let key = crate::key::database::fc::new(ns, db, &fc.name);
34 txn.del(key).await?;
35 txn.clear();
37 Ok(Value::None)
39 }
40 .await;
41 match future {
42 Err(Error::FcNotFound {
43 ..
44 }) if self.if_exists => Ok(Value::None),
45 v => v,
46 }
47 }
48}
49
50impl Display for RemoveFunctionStatement {
51 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52 write!(f, "REMOVE FUNCTION")?;
54 if self.if_exists {
55 write!(f, " IF EXISTS")?
56 }
57 write!(f, " fn::{}", self.name.0)?;
58 Ok(())
59 }
60}