Skip to main content

surql_parser/upstream/sql/statements/define/
access.rs

1use super::DefineKind;
2use crate::upstream::fmt::CoverStmts;
3use crate::upstream::sql::access::AccessDuration;
4use crate::upstream::sql::{AccessType, Base, Expr, Literal};
5use surrealdb_types::{SqlFormat, ToSql, write_sql};
6#[derive(Clone, Debug, PartialEq, Eq)]
7pub struct DefineAccessStatement {
8	pub kind: DefineKind,
9	pub name: Expr,
10	pub base: Base,
11	pub access_type: AccessType,
12	pub authenticate: Option<Expr>,
13	pub duration: AccessDuration,
14	pub comment: Expr,
15}
16impl ToSql for DefineAccessStatement {
17	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
18		write_sql!(f, fmt, "DEFINE ACCESS");
19		match self.kind {
20			DefineKind::Default => {}
21			DefineKind::Overwrite => {
22				write_sql!(f, fmt, " OVERWRITE");
23			}
24			DefineKind::IfNotExists => {
25				write_sql!(f, fmt, " IF NOT EXISTS");
26			}
27		}
28		write_sql!(
29			f,
30			fmt,
31			" {} ON {} TYPE {}",
32			CoverStmts(&self.name),
33			self.base,
34			self.access_type
35		);
36		if let Some(ref v) = self.authenticate {
37			write_sql!(f, fmt, " AUTHENTICATE {}", CoverStmts(v))
38		}
39		write_sql!(f, fmt, " DURATION");
40		if self.access_type.can_issue_grants() {
41			write_sql!(f, fmt, " FOR GRANT {},", CoverStmts(&self.duration.grant));
42		}
43		if self.access_type.can_issue_tokens() {
44			write_sql!(f, fmt, " FOR TOKEN {},", CoverStmts(&self.duration.token));
45		}
46		write_sql!(
47			f,
48			fmt,
49			" FOR SESSION {}",
50			CoverStmts(&self.duration.session)
51		);
52		if !matches!(self.comment, Expr::Literal(Literal::None)) {
53			write_sql!(f, fmt, " COMMENT {}", CoverStmts(&self.comment));
54		}
55	}
56}