surrealdb_core/sql/statements/define/
event.rs1use crate::ctx::Context;
2use crate::dbs::Options;
3use crate::doc::CursorDoc;
4use crate::err::Error;
5use crate::iam::{Action, ResourceKind};
6use crate::sql::statements::define::DefineTableStatement;
7use crate::sql::statements::info::InfoStructure;
8use crate::sql::{Base, Ident, Strand, Value, Values};
9
10use revision::revisioned;
11use serde::{Deserialize, Serialize};
12use std::fmt::{self, Display};
13use uuid::Uuid;
14
15#[revisioned(revision = 3)]
16#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
17#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
18#[non_exhaustive]
19pub struct DefineEventStatement {
20 pub name: Ident,
21 pub what: Ident,
22 pub when: Value,
23 pub then: Values,
24 pub comment: Option<Strand>,
25 #[revision(start = 2)]
26 pub if_not_exists: bool,
27 #[revision(start = 3)]
28 pub overwrite: bool,
29}
30
31impl DefineEventStatement {
32 pub(crate) async fn compute(
34 &self,
35 ctx: &Context,
36 opt: &Options,
37 _doc: Option<&CursorDoc>,
38 ) -> Result<Value, Error> {
39 opt.is_allowed(Action::Edit, ResourceKind::Event, &Base::Db)?;
41 let (ns, db) = opt.ns_db()?;
43 let txn = ctx.tx();
45 if txn.get_tb_event(ns, db, &self.what, &self.name).await.is_ok() {
47 if self.if_not_exists {
48 return Ok(Value::None);
49 } else if !self.overwrite {
50 return Err(Error::EvAlreadyExists {
51 name: self.name.to_string(),
52 });
53 }
54 }
55 let key = crate::key::table::ev::new(ns, db, &self.what, &self.name);
57 txn.get_or_add_ns(ns, opt.strict).await?;
58 txn.get_or_add_db(ns, db, opt.strict).await?;
59 txn.get_or_add_tb(ns, db, &self.what, opt.strict).await?;
60 txn.set(
61 key,
62 revision::to_vec(&DefineEventStatement {
63 if_not_exists: false,
65 overwrite: false,
66 ..self.clone()
67 })?,
68 None,
69 )
70 .await?;
71 let key = crate::key::database::tb::new(ns, db, &self.what);
73 let tb = txn.get_tb(ns, db, &self.what).await?;
74 txn.set(
75 key,
76 revision::to_vec(&DefineTableStatement {
77 cache_events_ts: Uuid::now_v7(),
78 ..tb.as_ref().clone()
79 })?,
80 None,
81 )
82 .await?;
83 if let Some(cache) = ctx.get_cache() {
85 cache.clear_tb(ns, db, &self.what);
86 }
87 txn.clear();
89 Ok(Value::None)
91 }
92}
93
94impl Display for DefineEventStatement {
95 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
96 write!(f, "DEFINE EVENT",)?;
97 if self.if_not_exists {
98 write!(f, " IF NOT EXISTS")?
99 }
100 if self.overwrite {
101 write!(f, " OVERWRITE")?
102 }
103 write!(f, " {} ON {} WHEN {} THEN {}", self.name, self.what, self.when, self.then)?;
104 if let Some(ref v) = self.comment {
105 write!(f, " COMMENT {v}")?
106 }
107 Ok(())
108 }
109}
110
111impl InfoStructure for DefineEventStatement {
112 fn structure(self) -> Value {
113 Value::from(map! {
114 "name".to_string() => self.name.structure(),
115 "what".to_string() => self.what.structure(),
116 "when".to_string() => self.when.structure(),
117 "then".to_string() => self.then.structure(),
118 "comment".to_string(), if let Some(v) = self.comment => v.into(),
119 })
120 }
121}