surrealdb_sql/
statement.rs

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