surql_parser/upstream/sql/statements/define/
module.rs1use super::DefineKind;
2use crate::upstream::fmt::CoverStmts;
3use crate::upstream::sql::{Expr, Literal, ModuleExecutable, Permission};
4use surrealdb_types::{SqlFormat, ToSql, write_sql};
5#[derive(Clone, Debug, PartialEq, Eq)]
6#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
7pub struct DefineModuleStatement {
8 pub kind: DefineKind,
9 pub name: Option<String>,
10 pub executable: ModuleExecutable,
11 pub comment: Expr,
12 pub permissions: Permission,
13}
14impl ToSql for DefineModuleStatement {
15 fn fmt_sql(&self, f: &mut String, sql_fmt: SqlFormat) {
16 f.push_str("DEFINE MODULE");
17 match self.kind {
18 DefineKind::Default => {}
19 DefineKind::Overwrite => f.push_str(" OVERWRITE"),
20 DefineKind::IfNotExists => f.push_str(" IF NOT EXISTS"),
21 }
22 if let Some(name) = &self.name {
23 write_sql!(f, sql_fmt, " mod::{} AS", name);
24 }
25 write_sql!(f, sql_fmt, " {}", self.executable);
26 if !matches!(self.comment, Expr::Literal(Literal::None)) {
27 write_sql!(f, sql_fmt, " COMMENT {}", CoverStmts(&self.comment));
28 }
29 if sql_fmt.is_pretty() {
30 f.push('\n');
31 sql_fmt.write_indent(f);
32 } else {
33 f.push(' ');
34 }
35 write_sql!(f, sql_fmt, "PERMISSIONS {}", self.permissions);
36 }
37}