Skip to main content

surrealdb_sql/statements/define/
model.rs

1use surrealdb_strand::Strand;
2use surrealdb_types::{SqlFormat, ToSql, write_sql};
3
4use super::DefineKind;
5use crate::{CoverStmts, Expr, Ident, Literal, Permission};
6
7#[derive(Clone, Debug, PartialEq, Eq)]
8#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
9pub struct DefineModelStatement {
10	pub kind: DefineKind,
11	pub hash: Strand,
12	pub name: Ident,
13	pub version: Strand,
14	pub comment: Expr,
15	pub permissions: Permission,
16}
17
18impl Default for DefineModelStatement {
19	fn default() -> Self {
20		Self {
21			kind: DefineKind::Default,
22			hash: Strand::default(),
23			name: Ident::default(),
24			version: Strand::default(),
25			comment: Expr::Literal(Literal::None),
26			permissions: Permission::default(),
27		}
28	}
29}
30
31impl ToSql for DefineModelStatement {
32	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
33		write_sql!(f, fmt, "DEFINE MODEL");
34		match self.kind {
35			DefineKind::Default => {}
36			DefineKind::Overwrite => write_sql!(f, fmt, " OVERWRITE"),
37			DefineKind::IfNotExists => write_sql!(f, fmt, " IF NOT EXISTS"),
38		}
39		write_sql!(f, fmt, " ml::{}<{}>", self.name, self.version.as_str());
40		if !matches!(self.comment, Expr::Literal(Literal::None)) {
41			write_sql!(f, fmt, " COMMENT {}", CoverStmts(&self.comment));
42		}
43		write_sql!(f, fmt, " PERMISSIONS {}", self.permissions);
44	}
45}