Skip to main content

surrealdb_sql/statements/
rebuild.rs

1use common::fmt::{EscapeKwFreeIdent, EscapeKwIdent};
2use surrealdb_strand::Strand;
3use surrealdb_types::{SqlFormat, ToSql, write_sql};
4
5use crate::TableName;
6
7#[derive(Clone, Debug, Eq, PartialEq, Hash)]
8#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
9pub enum RebuildStatement {
10	Index(RebuildIndexStatement),
11}
12
13impl ToSql for RebuildStatement {
14	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
15		match self {
16			Self::Index(v) => v.fmt_sql(f, fmt),
17		}
18	}
19}
20
21#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Hash)]
22#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
23pub struct RebuildIndexStatement {
24	pub name: Strand,
25	pub what: TableName,
26	pub if_exists: bool,
27	pub concurrently: bool,
28}
29
30impl ToSql for RebuildIndexStatement {
31	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
32		write_sql!(f, fmt, "REBUILD INDEX");
33		if self.if_exists {
34			write_sql!(f, fmt, " IF EXISTS");
35		}
36		write_sql!(
37			f,
38			fmt,
39			" {} ON {}",
40			EscapeKwIdent(self.name.as_str(), &["IF"]),
41			EscapeKwFreeIdent(self.what.as_str())
42		);
43		if self.concurrently {
44			write_sql!(f, fmt, " CONCURRENTLY");
45		}
46	}
47}