surrealdb_core/sql/statements/define/
mod.rs1mod access;
2mod analyzer;
3mod api;
4pub mod config;
5mod database;
6mod deprecated;
7mod event;
8mod field;
9mod function;
10mod index;
11mod model;
12mod namespace;
13mod param;
14mod table;
15mod user;
16
17pub use access::DefineAccessStatement;
18pub use analyzer::DefineAnalyzerStatement;
19pub use api::DefineApiStatement;
20pub use config::DefineConfigStatement;
21pub use database::DefineDatabaseStatement;
22pub use event::DefineEventStatement;
23pub use field::DefineFieldStatement;
24pub use function::DefineFunctionStatement;
25pub use index::DefineIndexStatement;
26pub use model::DefineModelStatement;
27pub use namespace::DefineNamespaceStatement;
28pub use param::DefineParamStatement;
29pub use table::DefineTableStatement;
30pub use user::DefineUserStatement;
31
32pub use deprecated::scope::DefineScopeStatement;
33pub use deprecated::token::DefineTokenStatement;
34
35pub use api::ApiAction;
36pub use api::ApiDefinition;
37pub use api::FindApi;
38
39use crate::ctx::Context;
40use crate::dbs::Options;
41use crate::doc::CursorDoc;
42use crate::err::Error;
43use crate::sql::value::Value;
44
45use reblessive::tree::Stk;
46use revision::revisioned;
47use serde::{Deserialize, Serialize};
48use std::fmt::{self, Display};
49
50#[revisioned(revision = 3)]
51#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
52#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
53#[non_exhaustive]
54pub enum DefineStatement {
55 Namespace(DefineNamespaceStatement),
56 Database(DefineDatabaseStatement),
57 Function(DefineFunctionStatement),
58 Analyzer(DefineAnalyzerStatement),
59 #[revision(
60 end = 2,
61 convert_fn = "convert_token_to_access",
62 fields_name = "DefineTokenStatementFields"
63 )]
64 Token(DefineTokenStatement),
65 #[revision(
66 end = 2,
67 convert_fn = "convert_scope_to_access",
68 fields_name = "DefineScopeStatementFields"
69 )]
70 Scope(DefineScopeStatement),
71 Param(DefineParamStatement),
72 Table(DefineTableStatement),
73 Event(DefineEventStatement),
74 Field(DefineFieldStatement),
75 Index(DefineIndexStatement),
76 User(DefineUserStatement),
77 Model(DefineModelStatement),
78 #[revision(start = 2)]
79 Access(DefineAccessStatement),
80 Config(DefineConfigStatement),
81 #[revision(start = 3)]
82 Api(DefineApiStatement),
83}
84
85impl DefineStatement {
87 fn convert_token_to_access(
88 fields: DefineTokenStatementFields,
89 _revision: u16,
90 ) -> Result<Self, revision::Error> {
91 Ok(DefineStatement::Access(fields.0.into()))
92 }
93
94 fn convert_scope_to_access(
95 fields: DefineScopeStatementFields,
96 _revision: u16,
97 ) -> Result<Self, revision::Error> {
98 Ok(DefineStatement::Access(fields.0.into()))
99 }
100}
101
102impl DefineStatement {
103 pub(crate) fn writeable(&self) -> bool {
105 true
106 }
107 pub(crate) async fn compute(
109 &self,
110 stk: &mut Stk,
111 ctx: &Context,
112 opt: &Options,
113 doc: Option<&CursorDoc>,
114 ) -> Result<Value, Error> {
115 match self {
116 Self::Namespace(ref v) => v.compute(ctx, opt, doc).await,
117 Self::Database(ref v) => v.compute(ctx, opt, doc).await,
118 Self::Function(ref v) => v.compute(ctx, opt, doc).await,
119 Self::Param(ref v) => v.compute(stk, ctx, opt, doc).await,
120 Self::Table(ref v) => v.compute(stk, ctx, opt, doc).await,
121 Self::Event(ref v) => v.compute(ctx, opt, doc).await,
122 Self::Field(ref v) => v.compute(ctx, opt, doc).await,
123 Self::Index(ref v) => v.compute(stk, ctx, opt, doc).await,
124 Self::Analyzer(ref v) => v.compute(ctx, opt, doc).await,
125 Self::User(ref v) => v.compute(ctx, opt, doc).await,
126 Self::Model(ref v) => v.compute(ctx, opt, doc).await,
127 Self::Access(ref v) => v.compute(ctx, opt, doc).await,
128 Self::Config(ref v) => v.compute(ctx, opt, doc).await,
129 Self::Api(ref v) => v.compute(stk, ctx, opt, doc).await,
130 }
131 }
132}
133
134impl Display for DefineStatement {
135 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
136 match self {
137 Self::Namespace(v) => Display::fmt(v, f),
138 Self::Database(v) => Display::fmt(v, f),
139 Self::Function(v) => Display::fmt(v, f),
140 Self::User(v) => Display::fmt(v, f),
141 Self::Param(v) => Display::fmt(v, f),
142 Self::Table(v) => Display::fmt(v, f),
143 Self::Event(v) => Display::fmt(v, f),
144 Self::Field(v) => Display::fmt(v, f),
145 Self::Index(v) => Display::fmt(v, f),
146 Self::Analyzer(v) => Display::fmt(v, f),
147 Self::Model(v) => Display::fmt(v, f),
148 Self::Access(v) => Display::fmt(v, f),
149 Self::Config(v) => Display::fmt(v, f),
150 Self::Api(v) => Display::fmt(v, f),
151 }
152 }
153}
154
155#[cfg(test)]
156mod tests {
157
158 use super::*;
159 use crate::sql::Ident;
160
161 #[test]
162 fn check_define_serialize() {
163 let stm = DefineStatement::Namespace(DefineNamespaceStatement {
164 name: Ident::from("test"),
165 ..Default::default()
166 });
167 let enc: Vec<u8> = revision::to_vec(&stm).unwrap();
168 assert_eq!(13, enc.len());
169 }
170}