surrealdb_sql/statements/
set.rs

1use crate::cnf::PROTECTED_PARAM_NAMES;
2use crate::ctx::Context;
3use crate::dbs::{Options, Transaction};
4use crate::doc::CursorDoc;
5use crate::err::Error;
6use crate::Value;
7use derive::Store;
8use revision::revisioned;
9use serde::{Deserialize, Serialize};
10use std::fmt;
11
12#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
13#[revisioned(revision = 1)]
14pub struct SetStatement {
15	pub name: String,
16	pub what: Value,
17}
18
19impl SetStatement {
20	/// Check if we require a writeable transaction
21	pub(crate) fn writeable(&self) -> bool {
22		self.what.writeable()
23	}
24	/// Process this type returning a computed simple Value
25	pub(crate) async fn compute(
26		&self,
27		ctx: &Context<'_>,
28		opt: &Options,
29		txn: &Transaction,
30		doc: Option<&CursorDoc<'_>>,
31	) -> Result<Value, Error> {
32		// Check if the variable is a protected variable
33		match PROTECTED_PARAM_NAMES.contains(&self.name.as_str()) {
34			// The variable isn't protected and can be stored
35			false => self.what.compute(ctx, opt, txn, doc).await,
36			// The user tried to set a protected variable
37			true => Err(Error::InvalidParam {
38				// Move the parameter name, as we no longer need it
39				name: self.name.to_owned(),
40			}),
41		}
42	}
43}
44
45impl fmt::Display for SetStatement {
46	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47		write!(f, "LET ${} = {}", self.name, self.what)
48	}
49}