Skip to main content

surrealdb_sql/statements/
create.rs

1use common::fmt::Fmt;
2use surrealdb_types::{SqlFormat, ToSql, write_sql};
3
4use crate::{CoverStmts, Data, Expr, Literal, Output};
5
6#[derive(Clone, Debug, Eq, PartialEq)]
7#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
8pub struct CreateStatement {
9	// A keyword modifier indicating if we are expecting a single result or several
10	pub only: bool,
11	// Where we are creating (i.e. table, or record ID)
12	#[cfg_attr(feature = "arbitrary", arbitrary(with = crate::arbitrary::atleast_one))]
13	pub what: Vec<Expr>,
14	// The data associated with the record being created
15	pub data: Option<Data>,
16	//  What the result of the statement should resemble (i.e. Diff or no result etc).
17	pub output: Option<Output>,
18	// The timeout for the statement
19	pub timeout: Expr,
20}
21
22impl Default for CreateStatement {
23	fn default() -> Self {
24		Self {
25			only: Default::default(),
26			what: Default::default(),
27			data: Default::default(),
28			output: Default::default(),
29			timeout: Expr::Literal(Literal::None),
30		}
31	}
32}
33
34impl ToSql for CreateStatement {
35	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
36		write_sql!(f, fmt, "CREATE");
37		if self.only {
38			write_sql!(f, fmt, " ONLY");
39		}
40		write_sql!(f, fmt, " {}", Fmt::comma_separated(self.what.iter().map(CoverStmts)));
41		if let Some(ref v) = self.data {
42			write_sql!(f, fmt, " {v}");
43		}
44		if let Some(ref v) = self.output {
45			write_sql!(f, fmt, " {v}");
46		}
47		if !matches!(self.timeout, Expr::Literal(Literal::None)) {
48			write_sql!(f, fmt, " TIMEOUT {}", CoverStmts(&self.timeout));
49		}
50	}
51}