surrealdb_core/sql/
statement.rs

1use crate::ctx::Context;
2use crate::dbs::Options;
3use crate::doc::CursorDoc;
4use crate::err::Error;
5use crate::sql::statements::rebuild::RebuildStatement;
6use crate::sql::statements::AccessStatement;
7use crate::sql::{
8	fmt::{Fmt, Pretty},
9	statements::{
10		AlterStatement, AnalyzeStatement, BeginStatement, BreakStatement, CancelStatement,
11		CommitStatement, ContinueStatement, CreateStatement, DefineStatement, DeleteStatement,
12		ForeachStatement, IfelseStatement, InfoStatement, InsertStatement, KillStatement,
13		LiveStatement, OptionStatement, OutputStatement, RelateStatement, RemoveStatement,
14		SelectStatement, SetStatement, ShowStatement, SleepStatement, ThrowStatement,
15		UpdateStatement, UpsertStatement, UseStatement,
16	},
17	value::Value,
18};
19use derive::Store;
20use reblessive::tree::Stk;
21use revision::revisioned;
22use serde::{Deserialize, Serialize};
23use std::{
24	fmt::{self, Display, Formatter, Write},
25	ops::Deref,
26	time::Duration,
27};
28
29#[revisioned(revision = 1)]
30#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
31#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
32#[non_exhaustive]
33pub struct Statements(pub Vec<Statement>);
34
35impl Deref for Statements {
36	type Target = Vec<Statement>;
37	fn deref(&self) -> &Self::Target {
38		&self.0
39	}
40}
41
42impl IntoIterator for Statements {
43	type Item = Statement;
44	type IntoIter = std::vec::IntoIter<Self::Item>;
45	fn into_iter(self) -> Self::IntoIter {
46		self.0.into_iter()
47	}
48}
49
50impl Display for Statements {
51	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52		Display::fmt(
53			&Fmt::one_line_separated(self.0.iter().map(|v| Fmt::new(v, |v, f| write!(f, "{v};")))),
54			f,
55		)
56	}
57}
58
59#[revisioned(revision = 5)]
60#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
61#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
62#[non_exhaustive]
63pub enum Statement {
64	Value(Value),
65	Analyze(AnalyzeStatement),
66	Begin(BeginStatement),
67	Break(BreakStatement),
68	Continue(ContinueStatement),
69	Cancel(CancelStatement),
70	Commit(CommitStatement),
71	Create(CreateStatement),
72	Define(DefineStatement),
73	Delete(DeleteStatement),
74	Foreach(ForeachStatement),
75	Ifelse(IfelseStatement),
76	Info(InfoStatement),
77	Insert(InsertStatement),
78	Kill(KillStatement),
79	Live(LiveStatement),
80	Option(OptionStatement),
81	Output(OutputStatement),
82	Relate(RelateStatement),
83	Remove(RemoveStatement),
84	Select(SelectStatement),
85	Set(SetStatement),
86	Show(ShowStatement),
87	Sleep(SleepStatement),
88	Update(UpdateStatement),
89	Throw(ThrowStatement),
90	Use(UseStatement),
91	#[revision(start = 2)]
92	Rebuild(RebuildStatement),
93	#[revision(start = 3)]
94	Upsert(UpsertStatement),
95	#[revision(start = 4)]
96	Alter(AlterStatement),
97	// TODO(gguillemas): Document once bearer access is no longer experimental.
98	#[doc(hidden)]
99	#[revision(start = 5)]
100	Access(AccessStatement),
101}
102
103impl Statement {
104	/// Get the statement timeout duration, if any
105	pub fn timeout(&self) -> Option<Duration> {
106		match self {
107			Self::Create(v) => v.timeout.as_ref().map(|v| *v.0),
108			Self::Delete(v) => v.timeout.as_ref().map(|v| *v.0),
109			Self::Insert(v) => v.timeout.as_ref().map(|v| *v.0),
110			Self::Relate(v) => v.timeout.as_ref().map(|v| *v.0),
111			Self::Select(v) => v.timeout.as_ref().map(|v| *v.0),
112			Self::Upsert(v) => v.timeout.as_ref().map(|v| *v.0),
113			Self::Update(v) => v.timeout.as_ref().map(|v| *v.0),
114			_ => None,
115		}
116	}
117	/// Check if we require a writeable transaction
118	pub(crate) fn writeable(&self) -> bool {
119		match self {
120			Self::Value(v) => v.writeable(),
121			Self::Access(_) => true,
122			Self::Alter(_) => true,
123			Self::Analyze(_) => false,
124			Self::Break(_) => false,
125			Self::Continue(_) => false,
126			Self::Create(v) => v.writeable(),
127			Self::Define(_) => true,
128			Self::Delete(v) => v.writeable(),
129			Self::Foreach(v) => v.writeable(),
130			Self::Ifelse(v) => v.writeable(),
131			Self::Info(_) => false,
132			Self::Insert(v) => v.writeable(),
133			Self::Kill(_) => true,
134			Self::Live(_) => true,
135			Self::Output(v) => v.writeable(),
136			Self::Option(_) => false,
137			Self::Rebuild(_) => true,
138			Self::Relate(v) => v.writeable(),
139			Self::Remove(_) => true,
140			Self::Select(v) => v.writeable(),
141			Self::Set(v) => v.writeable(),
142			Self::Show(_) => false,
143			Self::Sleep(_) => false,
144			Self::Throw(_) => false,
145			Self::Upsert(v) => v.writeable(),
146			Self::Update(v) => v.writeable(),
147			Self::Use(_) => false,
148			_ => unreachable!(),
149		}
150	}
151	/// Process this type returning a computed simple Value
152	pub(crate) async fn compute(
153		&self,
154		stk: &mut Stk,
155		ctx: &Context<'_>,
156		opt: &Options,
157		doc: Option<&CursorDoc<'_>>,
158	) -> Result<Value, Error> {
159		match self {
160			Self::Access(v) => v.compute(ctx, opt, doc).await,
161			Self::Alter(v) => v.compute(stk, ctx, opt, doc).await,
162			Self::Analyze(v) => v.compute(ctx, opt, doc).await,
163			Self::Break(v) => v.compute(ctx, opt, doc).await,
164			Self::Continue(v) => v.compute(ctx, opt, doc).await,
165			Self::Create(v) => v.compute(stk, ctx, opt, doc).await,
166			Self::Delete(v) => v.compute(stk, ctx, opt, doc).await,
167			Self::Define(v) => v.compute(stk, ctx, opt, doc).await,
168			Self::Foreach(v) => v.compute(stk, ctx, opt, doc).await,
169			Self::Ifelse(v) => v.compute(stk, ctx, opt, doc).await,
170			Self::Info(v) => v.compute(ctx, opt, doc).await,
171			Self::Insert(v) => v.compute(stk, ctx, opt, doc).await,
172			Self::Kill(v) => v.compute(stk, ctx, opt, doc).await,
173			Self::Live(v) => v.compute(stk, ctx, opt, doc).await,
174			Self::Output(v) => v.compute(stk, ctx, opt, doc).await,
175			Self::Relate(v) => v.compute(stk, ctx, opt, doc).await,
176			Self::Rebuild(v) => v.compute(stk, ctx, opt, doc).await,
177			Self::Remove(v) => v.compute(ctx, opt, doc).await,
178			Self::Select(v) => v.compute(stk, ctx, opt, doc).await,
179			Self::Set(v) => v.compute(stk, ctx, opt, doc).await,
180			Self::Show(v) => v.compute(ctx, opt, doc).await,
181			Self::Sleep(v) => v.compute(ctx, opt, doc).await,
182			Self::Throw(v) => v.compute(stk, ctx, opt, doc).await,
183			Self::Update(v) => v.compute(stk, ctx, opt, doc).await,
184			Self::Upsert(v) => v.compute(stk, ctx, opt, doc).await,
185			Self::Value(v) => {
186				// Ensure futures are processed
187				let opt = &opt.new_with_futures(true);
188				// Process the output value
189				v.compute_unbordered(stk, ctx, opt, doc).await
190			}
191			_ => unreachable!(),
192		}
193	}
194}
195
196impl Display for Statement {
197	fn fmt(&self, f: &mut Formatter) -> fmt::Result {
198		match self {
199			Self::Value(v) => write!(Pretty::from(f), "{v}"),
200			Self::Access(v) => write!(Pretty::from(f), "{v}"),
201			Self::Alter(v) => write!(Pretty::from(f), "{v}"),
202			Self::Analyze(v) => write!(Pretty::from(f), "{v}"),
203			Self::Begin(v) => write!(Pretty::from(f), "{v}"),
204			Self::Break(v) => write!(Pretty::from(f), "{v}"),
205			Self::Cancel(v) => write!(Pretty::from(f), "{v}"),
206			Self::Commit(v) => write!(Pretty::from(f), "{v}"),
207			Self::Continue(v) => write!(Pretty::from(f), "{v}"),
208			Self::Create(v) => write!(Pretty::from(f), "{v}"),
209			Self::Define(v) => write!(Pretty::from(f), "{v}"),
210			Self::Delete(v) => write!(Pretty::from(f), "{v}"),
211			Self::Foreach(v) => write!(Pretty::from(f), "{v}"),
212			Self::Insert(v) => write!(Pretty::from(f), "{v}"),
213			Self::Ifelse(v) => write!(Pretty::from(f), "{v}"),
214			Self::Info(v) => write!(Pretty::from(f), "{v}"),
215			Self::Kill(v) => write!(Pretty::from(f), "{v}"),
216			Self::Live(v) => write!(Pretty::from(f), "{v}"),
217			Self::Option(v) => write!(Pretty::from(f), "{v}"),
218			Self::Output(v) => write!(Pretty::from(f), "{v}"),
219			Self::Rebuild(v) => write!(Pretty::from(f), "{v}"),
220			Self::Relate(v) => write!(Pretty::from(f), "{v}"),
221			Self::Remove(v) => write!(Pretty::from(f), "{v}"),
222			Self::Select(v) => write!(Pretty::from(f), "{v}"),
223			Self::Set(v) => write!(Pretty::from(f), "{v}"),
224			Self::Show(v) => write!(Pretty::from(f), "{v}"),
225			Self::Sleep(v) => write!(Pretty::from(f), "{v}"),
226			Self::Throw(v) => write!(Pretty::from(f), "{v}"),
227			Self::Update(v) => write!(Pretty::from(f), "{v}"),
228			Self::Upsert(v) => write!(Pretty::from(f), "{v}"),
229			Self::Use(v) => write!(Pretty::from(f), "{v}"),
230		}
231	}
232}