surrealdb_core/sql/
data.rs

1use crate::ctx::Context;
2use crate::dbs::Options;
3use crate::err::Error;
4use crate::sql::fmt::Fmt;
5use crate::sql::idiom::Idiom;
6use crate::sql::operator::Operator;
7use crate::sql::part::Part;
8use crate::sql::paths::ID;
9use crate::sql::value::Value;
10use reblessive::tree::Stk;
11use revision::revisioned;
12use serde::{Deserialize, Serialize};
13use std::fmt::{self, Display, Formatter};
14
15#[revisioned(revision = 1)]
16#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
17#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
18#[non_exhaustive]
19#[derive(Default)]
20pub enum Data {
21	#[default]
22	EmptyExpression,
23	SetExpression(Vec<(Idiom, Operator, Value)>),
24	UnsetExpression(Vec<Idiom>),
25	PatchExpression(Value),
26	MergeExpression(Value),
27	ReplaceExpression(Value),
28	ContentExpression(Value),
29	SingleExpression(Value),
30	ValuesExpression(Vec<Vec<(Idiom, Value)>>),
31	UpdateExpression(Vec<(Idiom, Operator, Value)>),
32}
33
34impl Data {
35	/// Fetch the 'id' field if one has been specified
36	pub(crate) async fn rid(
37		&self,
38		stk: &mut Stk,
39		ctx: &Context,
40		opt: &Options,
41	) -> Result<Option<Value>, Error> {
42		self.pick(stk, ctx, opt, &*ID).await
43	}
44	/// Fetch a field path value if one is specified
45	pub(crate) async fn pick(
46		&self,
47		stk: &mut Stk,
48		ctx: &Context,
49		opt: &Options,
50		path: &[Part],
51	) -> Result<Option<Value>, Error> {
52		match self {
53			Self::MergeExpression(v) => match v {
54				Value::Param(v) => Ok(v.compute(stk, ctx, opt, None).await?.pick(path).some()),
55				Value::Object(_) => Ok(v.pick(path).compute(stk, ctx, opt, None).await?.some()),
56				_ => Ok(None),
57			},
58			Self::ReplaceExpression(v) => match v {
59				Value::Param(v) => Ok(v.compute(stk, ctx, opt, None).await?.pick(path).some()),
60				Value::Object(_) => Ok(v.pick(path).compute(stk, ctx, opt, None).await?.some()),
61				_ => Ok(None),
62			},
63			Self::ContentExpression(v) => match v {
64				Value::Param(v) => Ok(v.compute(stk, ctx, opt, None).await?.pick(path).some()),
65				Value::Object(_) => Ok(v.pick(path).compute(stk, ctx, opt, None).await?.some()),
66				_ => Ok(None),
67			},
68			Self::SetExpression(v) => match v.iter().find(|f| f.0.is_field(path)) {
69				Some((_, _, v)) => {
70					// This SET expression has this field
71					Ok(v.compute(stk, ctx, opt, None).await?.some())
72				}
73				// This SET expression does not have this field
74				_ => Ok(None),
75			},
76			// Return nothing
77			_ => Ok(None),
78		}
79	}
80}
81
82impl Display for Data {
83	fn fmt(&self, f: &mut Formatter) -> fmt::Result {
84		match self {
85			Self::EmptyExpression => Ok(()),
86			Self::SetExpression(v) => write!(
87				f,
88				"SET {}",
89				Fmt::comma_separated(
90					v.iter().map(|args| Fmt::new(args, |(l, o, r), f| write!(f, "{l} {o} {r}",)))
91				)
92			),
93			Self::UnsetExpression(v) => write!(
94				f,
95				"UNSET {}",
96				Fmt::comma_separated(v.iter().map(|args| Fmt::new(args, |l, f| write!(f, "{l}",))))
97			),
98			Self::PatchExpression(v) => write!(f, "PATCH {v}"),
99			Self::MergeExpression(v) => write!(f, "MERGE {v}"),
100			Self::ReplaceExpression(v) => write!(f, "REPLACE {v}"),
101			Self::ContentExpression(v) => write!(f, "CONTENT {v}"),
102			Self::SingleExpression(v) => Display::fmt(v, f),
103			Self::ValuesExpression(v) => write!(
104				f,
105				"({}) VALUES {}",
106				Fmt::comma_separated(v.first().unwrap().iter().map(|(v, _)| v)),
107				Fmt::comma_separated(v.iter().map(|v| Fmt::new(v, |v, f| write!(
108					f,
109					"({})",
110					Fmt::comma_separated(v.iter().map(|(_, v)| v))
111				))))
112			),
113			Self::UpdateExpression(v) => write!(
114				f,
115				"ON DUPLICATE KEY UPDATE {}",
116				Fmt::comma_separated(
117					v.iter().map(|args| Fmt::new(args, |(l, o, r), f| write!(f, "{l} {o} {r}",)))
118				)
119			),
120		}
121	}
122}