surrealdb_sql/statements/define/
mod.rs

1mod analyzer;
2mod database;
3mod event;
4mod field;
5mod function;
6mod index;
7mod model;
8mod namespace;
9mod param;
10mod scope;
11mod table;
12mod token;
13mod user;
14
15pub use analyzer::DefineAnalyzerStatement;
16pub use database::DefineDatabaseStatement;
17pub use event::DefineEventStatement;
18pub use field::DefineFieldStatement;
19pub use function::DefineFunctionStatement;
20pub use index::DefineIndexStatement;
21pub use model::DefineModelStatement;
22pub use namespace::DefineNamespaceStatement;
23pub use param::DefineParamStatement;
24pub use scope::DefineScopeStatement;
25pub use table::DefineTableStatement;
26pub use token::DefineTokenStatement;
27pub use user::DefineUserStatement;
28
29use crate::ctx::Context;
30use crate::dbs::Options;
31use crate::dbs::Transaction;
32use crate::doc::CursorDoc;
33use crate::err::Error;
34use crate::value::Value;
35use derive::Store;
36use revision::revisioned;
37use serde::{Deserialize, Serialize};
38use std::fmt::{self, Display};
39
40#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
41#[revisioned(revision = 1)]
42pub enum DefineStatement {
43	Namespace(DefineNamespaceStatement),
44	Database(DefineDatabaseStatement),
45	Function(DefineFunctionStatement),
46	Analyzer(DefineAnalyzerStatement),
47	Token(DefineTokenStatement),
48	Scope(DefineScopeStatement),
49	Param(DefineParamStatement),
50	Table(DefineTableStatement),
51	Event(DefineEventStatement),
52	Field(DefineFieldStatement),
53	Index(DefineIndexStatement),
54	User(DefineUserStatement),
55	Model(DefineModelStatement),
56}
57
58impl DefineStatement {
59	/// Check if we require a writeable transaction
60	pub(crate) fn writeable(&self) -> bool {
61		true
62	}
63	/// Process this type returning a computed simple Value
64	pub(crate) async fn compute(
65		&self,
66		ctx: &Context<'_>,
67		opt: &Options,
68		txn: &Transaction,
69		doc: Option<&CursorDoc<'_>>,
70	) -> Result<Value, Error> {
71		match self {
72			Self::Namespace(ref v) => v.compute(ctx, opt, txn, doc).await,
73			Self::Database(ref v) => v.compute(ctx, opt, txn, doc).await,
74			Self::Function(ref v) => v.compute(ctx, opt, txn, doc).await,
75			Self::Token(ref v) => v.compute(ctx, opt, txn, doc).await,
76			Self::Scope(ref v) => v.compute(ctx, opt, txn, doc).await,
77			Self::Param(ref v) => v.compute(ctx, opt, txn, doc).await,
78			Self::Table(ref v) => v.compute(ctx, opt, txn, doc).await,
79			Self::Event(ref v) => v.compute(ctx, opt, txn, doc).await,
80			Self::Field(ref v) => v.compute(ctx, opt, txn, doc).await,
81			Self::Index(ref v) => v.compute(ctx, opt, txn, doc).await,
82			Self::Analyzer(ref v) => v.compute(ctx, opt, txn, doc).await,
83			Self::User(ref v) => v.compute(ctx, opt, txn, doc).await,
84			Self::Model(ref v) => v.compute(ctx, opt, txn, doc).await,
85		}
86	}
87}
88
89impl Display for DefineStatement {
90	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
91		match self {
92			Self::Namespace(v) => Display::fmt(v, f),
93			Self::Database(v) => Display::fmt(v, f),
94			Self::Function(v) => Display::fmt(v, f),
95			Self::User(v) => Display::fmt(v, f),
96			Self::Token(v) => Display::fmt(v, f),
97			Self::Scope(v) => Display::fmt(v, f),
98			Self::Param(v) => Display::fmt(v, f),
99			Self::Table(v) => Display::fmt(v, f),
100			Self::Event(v) => Display::fmt(v, f),
101			Self::Field(v) => Display::fmt(v, f),
102			Self::Index(v) => Display::fmt(v, f),
103			Self::Analyzer(v) => Display::fmt(v, f),
104			Self::Model(v) => Display::fmt(v, f),
105		}
106	}
107}
108
109#[cfg(test)]
110mod tests {
111
112	use super::*;
113	use crate::Ident;
114
115	#[test]
116	fn check_define_serialize() {
117		let stm = DefineStatement::Namespace(DefineNamespaceStatement {
118			name: Ident::from("test"),
119			..Default::default()
120		});
121		let enc: Vec<u8> = stm.try_into().unwrap();
122		assert_eq!(11, enc.len());
123	}
124}