Skip to main content

surrealdb_sql/statements/define/
event.rs

1use common::fmt::Fmt;
2use surrealdb_types::{SqlFormat, ToSql, write_sql};
3
4use super::DefineKind;
5use crate::{CoverStmts, EventKind, Expr, Literal};
6
7#[derive(Clone, Debug, PartialEq, Eq)]
8#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
9pub struct DefineEventStatement {
10	pub kind: DefineKind,
11	pub name: Expr,
12	pub target_table: Expr,
13	pub when: Expr,
14	#[cfg_attr(feature = "arbitrary", arbitrary(with = crate::arbitrary::atleast_one))]
15	pub then: Vec<Expr>,
16	pub comment: Expr,
17	pub event_kind: EventKind,
18}
19
20impl ToSql for DefineEventStatement {
21	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
22		f.push_str("DEFINE EVENT");
23		match self.kind {
24			DefineKind::Default => {}
25			DefineKind::Overwrite => f.push_str(" OVERWRITE"),
26			DefineKind::IfNotExists => f.push_str(" IF NOT EXISTS"),
27		}
28		write_sql!(f, fmt, " {} ON {}", CoverStmts(&self.name), CoverStmts(&self.target_table),);
29		if let EventKind::Async {
30			retry,
31			max_depth,
32		} = self.event_kind
33		{
34			write_sql!(f, fmt, " ASYNC RETRY {} MAXDEPTH {}", retry, max_depth);
35		}
36		write_sql!(f, fmt, " WHEN {}", CoverStmts(&self.when),);
37		if !self.then.is_empty() {
38			write_sql!(f, fmt, " THEN {}", Fmt::comma_separated(self.then.iter().map(CoverStmts)));
39		}
40		if !matches!(self.comment, Expr::Literal(Literal::None)) {
41			write_sql!(f, fmt, " COMMENT {}", CoverStmts(&self.comment));
42		}
43	}
44}