surrealdb_sql/statements/define/
param.rs

1use crate::ctx::Context;
2use crate::dbs::{Options, Transaction};
3use crate::doc::CursorDoc;
4use crate::err::Error;
5use crate::fmt::{is_pretty, pretty_indent};
6use crate::iam::{Action, ResourceKind};
7use crate::{Base, Ident, Permission, Strand, Value};
8use derive::Store;
9use revision::revisioned;
10use serde::{Deserialize, Serialize};
11use std::fmt::{self, Display, Write};
12
13#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
14#[revisioned(revision = 1)]
15pub struct DefineParamStatement {
16	pub name: Ident,
17	pub value: Value,
18	pub comment: Option<Strand>,
19	pub permissions: Permission,
20}
21
22impl DefineParamStatement {
23	/// Process this type returning a computed simple Value
24	pub(crate) async fn compute(
25		&self,
26		_ctx: &Context<'_>,
27		opt: &Options,
28		txn: &Transaction,
29		_doc: Option<&CursorDoc<'_>>,
30	) -> Result<Value, Error> {
31		// Allowed to run?
32		opt.is_allowed(Action::Edit, ResourceKind::Parameter, &Base::Db)?;
33		// Claim transaction
34		let mut run = txn.lock().await;
35		// Clear the cache
36		run.clear_cache();
37		// Process the statement
38		let key = crate::key::database::pa::new(opt.ns(), opt.db(), &self.name);
39		run.add_ns(opt.ns(), opt.strict).await?;
40		run.add_db(opt.ns(), opt.db(), opt.strict).await?;
41		run.set(key, self).await?;
42		// Ok all good
43		Ok(Value::None)
44	}
45}
46
47impl Display for DefineParamStatement {
48	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49		write!(f, "DEFINE PARAM ${} VALUE {}", self.name, self.value)?;
50		if let Some(ref v) = self.comment {
51			write!(f, " COMMENT {v}")?
52		}
53		let _indent = if is_pretty() {
54			Some(pretty_indent())
55		} else {
56			f.write_char(' ')?;
57			None
58		};
59		write!(f, "PERMISSIONS {}", self.permissions)?;
60		Ok(())
61	}
62}