surrealdb_core/sql/
subquery.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::{
7	AlterStatement, CreateStatement, DefineStatement, DeleteStatement, IfelseStatement,
8	InsertStatement, OutputStatement, RelateStatement, RemoveStatement, SelectStatement,
9	UpdateStatement, UpsertStatement,
10};
11use crate::sql::value::Value;
12use reblessive::tree::Stk;
13use revision::revisioned;
14use serde::{Deserialize, Serialize};
15use std::cmp::Ordering;
16use std::fmt::{self, Display, Formatter};
17
18pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Subquery";
19
20#[revisioned(revision = 4)]
21#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash)]
22#[serde(rename = "$surrealdb::private::sql::Subquery")]
23#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
24#[non_exhaustive]
25pub enum Subquery {
26	Value(Value),
27	Ifelse(IfelseStatement),
28	Output(OutputStatement),
29	Select(SelectStatement),
30	Create(CreateStatement),
31	Update(UpdateStatement),
32	Delete(DeleteStatement),
33	Relate(RelateStatement),
34	Insert(InsertStatement),
35	Define(DefineStatement),
36	Remove(RemoveStatement),
37	#[revision(start = 2)]
38	Rebuild(RebuildStatement),
39	#[revision(start = 3)]
40	Upsert(UpsertStatement),
41	#[revision(start = 4)]
42	Alter(AlterStatement),
43}
44
45impl PartialOrd for Subquery {
46	#[inline]
47	fn partial_cmp(&self, _: &Self) -> Option<Ordering> {
48		None
49	}
50}
51
52impl Subquery {
53	/// Check if we require a writeable transaction
54	pub(crate) fn writeable(&self) -> bool {
55		match self {
56			Self::Value(v) => v.writeable(),
57			Self::Ifelse(v) => v.writeable(),
58			Self::Output(v) => v.writeable(),
59			Self::Select(v) => v.writeable(),
60			Self::Create(v) => v.writeable(),
61			Self::Upsert(v) => v.writeable(),
62			Self::Update(v) => v.writeable(),
63			Self::Delete(v) => v.writeable(),
64			Self::Relate(v) => v.writeable(),
65			Self::Insert(v) => v.writeable(),
66			Self::Define(v) => v.writeable(),
67			Self::Remove(v) => v.writeable(),
68			Self::Rebuild(v) => v.writeable(),
69			Self::Alter(v) => v.writeable(),
70		}
71	}
72	/// Process this type returning a computed simple Value
73	pub(crate) async fn compute(
74		&self,
75		stk: &mut Stk,
76		ctx: &Context<'_>,
77		opt: &Options,
78		doc: Option<&CursorDoc<'_>>,
79	) -> Result<Value, Error> {
80		// Duplicate context
81		let mut ctx = Context::new(ctx);
82		// Add parent document
83		if let Some(doc) = doc {
84			ctx.add_value("parent", doc.doc.as_ref());
85		}
86		// Process the subquery
87		match self {
88			Self::Value(ref v) => v.compute(stk, &ctx, opt, doc).await,
89			Self::Ifelse(ref v) => v.compute(stk, &ctx, opt, doc).await,
90			Self::Output(ref v) => v.compute(stk, &ctx, opt, doc).await,
91			Self::Define(ref v) => v.compute(stk, &ctx, opt, doc).await,
92			Self::Rebuild(ref v) => v.compute(stk, &ctx, opt, doc).await,
93			Self::Remove(ref v) => v.compute(&ctx, opt, doc).await,
94			Self::Select(ref v) => v.compute(stk, &ctx, opt, doc).await,
95			Self::Create(ref v) => v.compute(stk, &ctx, opt, doc).await,
96			Self::Upsert(ref v) => v.compute(stk, &ctx, opt, doc).await,
97			Self::Update(ref v) => v.compute(stk, &ctx, opt, doc).await,
98			Self::Delete(ref v) => v.compute(stk, &ctx, opt, doc).await,
99			Self::Relate(ref v) => v.compute(stk, &ctx, opt, doc).await,
100			Self::Insert(ref v) => v.compute(stk, &ctx, opt, doc).await,
101			Self::Alter(ref v) => v.compute(stk, &ctx, opt, doc).await,
102		}
103	}
104}
105
106impl Display for Subquery {
107	fn fmt(&self, f: &mut Formatter) -> fmt::Result {
108		match self {
109			Self::Value(v) => write!(f, "({v})"),
110			Self::Output(v) => write!(f, "({v})"),
111			Self::Select(v) => write!(f, "({v})"),
112			Self::Create(v) => write!(f, "({v})"),
113			Self::Upsert(v) => write!(f, "({v})"),
114			Self::Update(v) => write!(f, "({v})"),
115			Self::Delete(v) => write!(f, "({v})"),
116			Self::Relate(v) => write!(f, "({v})"),
117			Self::Insert(v) => write!(f, "({v})"),
118			Self::Define(v) => write!(f, "({v})"),
119			Self::Remove(v) => write!(f, "({v})"),
120			Self::Rebuild(v) => write!(f, "({v})"),
121			Self::Alter(v) => write!(f, "({v})"),
122			Self::Ifelse(v) => Display::fmt(v, f),
123		}
124	}
125}