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]
19pub enum Data {
20 EmptyExpression,
21 SetExpression(Vec<(Idiom, Operator, Value)>),
22 UnsetExpression(Vec<Idiom>),
23 PatchExpression(Value),
24 MergeExpression(Value),
25 ReplaceExpression(Value),
26 ContentExpression(Value),
27 SingleExpression(Value),
28 ValuesExpression(Vec<Vec<(Idiom, Value)>>),
29 UpdateExpression(Vec<(Idiom, Operator, Value)>),
30}
31
32impl Default for Data {
33 fn default() -> Self {
34 Self::EmptyExpression
35 }
36}
37
38impl Data {
39 pub(crate) async fn rid(
41 &self,
42 stk: &mut Stk,
43 ctx: &Context,
44 opt: &Options,
45 ) -> Result<Option<Value>, Error> {
46 self.pick(stk, ctx, opt, &*ID).await
47 }
48 pub(crate) async fn pick(
50 &self,
51 stk: &mut Stk,
52 ctx: &Context,
53 opt: &Options,
54 path: &[Part],
55 ) -> Result<Option<Value>, Error> {
56 match self {
57 Self::MergeExpression(v) => match v {
58 Value::Param(v) => Ok(v.compute(stk, ctx, opt, None).await?.pick(path).some()),
59 Value::Object(_) => Ok(v.pick(path).compute(stk, ctx, opt, None).await?.some()),
60 _ => Ok(None),
61 },
62 Self::ReplaceExpression(v) => match v {
63 Value::Param(v) => Ok(v.compute(stk, ctx, opt, None).await?.pick(path).some()),
64 Value::Object(_) => Ok(v.pick(path).compute(stk, ctx, opt, None).await?.some()),
65 _ => Ok(None),
66 },
67 Self::ContentExpression(v) => match v {
68 Value::Param(v) => Ok(v.compute(stk, ctx, opt, None).await?.pick(path).some()),
69 Value::Object(_) => Ok(v.pick(path).compute(stk, ctx, opt, None).await?.some()),
70 _ => Ok(None),
71 },
72 Self::SetExpression(v) => match v.iter().find(|f| f.0.is_field(path)) {
73 Some((_, _, v)) => {
74 Ok(v.compute(stk, ctx, opt, None).await?.some())
76 }
77 _ => Ok(None),
79 },
80 _ => Ok(None),
82 }
83 }
84}
85
86impl Display for Data {
87 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
88 match self {
89 Self::EmptyExpression => Ok(()),
90 Self::SetExpression(v) => write!(
91 f,
92 "SET {}",
93 Fmt::comma_separated(
94 v.iter().map(|args| Fmt::new(args, |(l, o, r), f| write!(f, "{l} {o} {r}",)))
95 )
96 ),
97 Self::UnsetExpression(v) => write!(
98 f,
99 "UNSET {}",
100 Fmt::comma_separated(v.iter().map(|args| Fmt::new(args, |l, f| write!(f, "{l}",))))
101 ),
102 Self::PatchExpression(v) => write!(f, "PATCH {v}"),
103 Self::MergeExpression(v) => write!(f, "MERGE {v}"),
104 Self::ReplaceExpression(v) => write!(f, "REPLACE {v}"),
105 Self::ContentExpression(v) => write!(f, "CONTENT {v}"),
106 Self::SingleExpression(v) => Display::fmt(v, f),
107 Self::ValuesExpression(v) => write!(
108 f,
109 "({}) VALUES {}",
110 Fmt::comma_separated(v.first().unwrap().iter().map(|(v, _)| v)),
111 Fmt::comma_separated(v.iter().map(|v| Fmt::new(v, |v, f| write!(
112 f,
113 "({})",
114 Fmt::comma_separated(v.iter().map(|(_, v)| v))
115 ))))
116 ),
117 Self::UpdateExpression(v) => write!(
118 f,
119 "ON DUPLICATE KEY UPDATE {}",
120 Fmt::comma_separated(
121 v.iter().map(|args| Fmt::new(args, |(l, o, r), f| write!(f, "{l} {o} {r}",)))
122 )
123 ),
124 }
125 }
126}