Skip to main content

surql_parser/upstream/sql/statements/define/
event.rs

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