surrealdb_core/sql/statements/
output.rs

1use crate::ctx::Context;
2use crate::dbs::Options;
3use crate::doc::CursorDoc;
4use crate::err::Error;
5use crate::sql::fetch::Fetchs;
6use crate::sql::value::Value;
7
8use reblessive::tree::Stk;
9use revision::revisioned;
10use serde::{Deserialize, Serialize};
11use std::fmt;
12
13#[revisioned(revision = 1)]
14#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
15#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
16#[non_exhaustive]
17pub struct OutputStatement {
18	pub what: Value,
19	pub fetch: Option<Fetchs>,
20}
21
22impl OutputStatement {
23	/// Check if we require a writeable transaction
24	pub(crate) fn writeable(&self) -> bool {
25		self.what.writeable()
26	}
27	/// Process this type returning a computed simple Value
28	pub(crate) async fn compute(
29		&self,
30		stk: &mut Stk,
31		ctx: &Context,
32		opt: &Options,
33		doc: Option<&CursorDoc>,
34	) -> Result<Value, Error> {
35		// Ensure futures are processed
36		let opt = &opt.new_with_futures(true);
37		// Process the output value
38		let mut value = self.what.compute(stk, ctx, opt, doc).await?;
39		// Fetch any
40		if let Some(fetchs) = &self.fetch {
41			let mut idioms = Vec::with_capacity(fetchs.0.len());
42			for fetch in fetchs.iter() {
43				fetch.compute(stk, ctx, opt, &mut idioms).await?
44			}
45			for i in &idioms {
46				value.fetch(stk, ctx, opt, i).await?;
47			}
48		}
49		//
50		Err(Error::Return {
51			value,
52		})
53	}
54}
55
56impl fmt::Display for OutputStatement {
57	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58		write!(f, "RETURN {}", self.what)?;
59		if let Some(ref v) = self.fetch {
60			write!(f, " {v}")?
61		}
62		Ok(())
63	}
64}