Skip to main content

surrealdb_sql/statements/define/
module.rs

1use surrealdb_types::{SqlFormat, ToSql, write_sql};
2
3use super::DefineKind;
4use crate::{CoverStmts, Expr, Ident, Literal, ModuleExecutable, Permission};
5
6#[derive(Clone, Debug, PartialEq, Eq)]
7#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
8pub struct DefineModuleStatement {
9	pub kind: DefineKind,
10	pub name: Option<Ident>,
11	pub executable: ModuleExecutable,
12	/// Whether the module is loaded without verifying a signature.
13	///
14	/// Spelled as a trailing `UNSIGNED` clause, unordered with `COMMENT` and
15	/// `PERMISSIONS`. Signature verification is not yet implemented for any
16	/// executable form, so every module currently has to opt out of it
17	/// explicitly and this is always `true` for a freshly parsed statement.
18	/// `false` is therefore not expressible in the grammar and would not
19	/// survive a render/reparse round trip, which is why the fuzzer is pinned
20	/// to `true` rather than left to generate either.
21	#[cfg_attr(feature = "arbitrary", arbitrary(value = true))]
22	pub unsigned: bool,
23	pub comment: Expr,
24	pub permissions: Permission,
25}
26
27impl ToSql for DefineModuleStatement {
28	fn fmt_sql(&self, f: &mut String, sql_fmt: SqlFormat) {
29		f.push_str("DEFINE MODULE");
30		match self.kind {
31			DefineKind::Default => {}
32			DefineKind::Overwrite => f.push_str(" OVERWRITE"),
33			DefineKind::IfNotExists => f.push_str(" IF NOT EXISTS"),
34		}
35		if let Some(name) = &self.name {
36			write_sql!(f, sql_fmt, " mod::{} FROM", name);
37		}
38		write_sql!(f, sql_fmt, " {}", self.executable);
39		if self.unsigned {
40			f.push_str(" UNSIGNED");
41		}
42		if !matches!(self.comment, Expr::Literal(Literal::None)) {
43			write_sql!(f, sql_fmt, " COMMENT {}", CoverStmts(&self.comment));
44		}
45		if sql_fmt.is_pretty() {
46			f.push('\n');
47			sql_fmt.write_indent(f);
48		} else {
49			f.push(' ');
50		}
51		write_sql!(f, sql_fmt, "PERMISSIONS {}", self.permissions);
52	}
53}