surrealdb_sql/statements/
set.rs1use 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 pub(crate) fn writeable(&self) -> bool {
22 self.what.writeable()
23 }
24 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 match PROTECTED_PARAM_NAMES.contains(&self.name.as_str()) {
34 false => self.what.compute(ctx, opt, txn, doc).await,
36 true => Err(Error::InvalidParam {
38 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}