surrealdb_core/sql/statements/remove/
param.rs

1use 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, Formatter};
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 RemoveParamStatement {
16	pub name: Ident,
17	#[revision(start = 2)]
18	pub if_exists: bool,
19}
20
21impl RemoveParamStatement {
22	/// Process this type returning a computed simple Value
23	pub(crate) async fn compute(&self, ctx: &Context, opt: &Options) -> Result<Value, Error> {
24		let future = async {
25			// Allowed to run?
26			opt.is_allowed(Action::Edit, ResourceKind::Parameter, &Base::Db)?;
27			// Get the transaction
28			let txn = ctx.tx();
29			// Get the definition
30			let (ns, db) = opt.ns_db()?;
31			let pa = txn.get_db_param(ns, db, &self.name).await?;
32			// Delete the definition
33			let key = crate::key::database::pa::new(ns, db, &pa.name);
34			txn.del(key).await?;
35			// Clear the cache
36			txn.clear();
37			// Ok all good
38			Ok(Value::None)
39		}
40		.await;
41		match future {
42			Err(Error::PaNotFound {
43				..
44			}) if self.if_exists => Ok(Value::None),
45			v => v,
46		}
47	}
48}
49
50impl Display for RemoveParamStatement {
51	fn fmt(&self, f: &mut Formatter) -> fmt::Result {
52		write!(f, "REMOVE PARAM")?;
53		if self.if_exists {
54			write!(f, " IF EXISTS")?
55		}
56		write!(f, " ${}", self.name)?;
57		Ok(())
58	}
59}