1use surrealdb_types::{SqlFormat, ToSql};
2
3use crate::expr::{Expr, Kind};
4use crate::sql::index::Distance;
5pub type MatchRef = u8;
7
8#[derive(Clone, Debug, Eq, PartialEq, Hash)]
9pub enum PrefixOperator {
10 Not,
12 Positive,
14 Negate,
16 Range,
18 RangeInclusive,
20 Cast(Kind),
21}
22
23impl ToSql for PrefixOperator {
24 fn fmt_sql(&self, f: &mut String, sql_fmt: SqlFormat) {
25 let prefix_operator: crate::sql::PrefixOperator = self.clone().into();
26 prefix_operator.fmt_sql(f, sql_fmt);
27 }
28}
29
30#[derive(Clone, Debug, Eq, PartialEq, Hash)]
31pub enum PostfixOperator {
32 Range,
33 RangeSkip,
34 MethodCall(String, Vec<Expr>),
36 Call(Vec<Expr>),
37}
38
39impl ToSql for PostfixOperator {
40 fn fmt_sql(&self, f: &mut String, sql_fmt: SqlFormat) {
41 let postfix_operator: crate::sql::PostfixOperator = self.clone().into();
42 postfix_operator.fmt_sql(f, sql_fmt);
43 }
44}
45
46#[derive(Clone, Debug, Eq, PartialEq, Hash)]
47pub enum BinaryOperator {
48 Subtract,
50 Add,
52 Multiply,
54 Divide,
56 Remainder,
58 Power,
60 Equal,
62 ExactEqual,
64 NotEqual,
66 AllEqual,
68 AnyEqual,
70
71 Or,
73 And,
75 NullCoalescing,
77 TenaryCondition,
79
80 LessThan,
82 LessThanEqual,
84 MoreThan,
86 MoreThanEqual,
88
89 Contain,
91 NotContain,
93 ContainAll,
95 ContainAny,
97 ContainNone,
99 Inside,
101 NotInside,
103 AllInside,
105 AnyInside,
107 NoneInside,
109
110 Outside,
112 Intersects,
114
115 Range,
117 RangeInclusive,
119 RangeSkip,
121 RangeSkipInclusive,
123
124 Matches(MatchesOperator),
126 NearestNeighbor(Box<NearestNeighbor>),
128}
129
130#[derive(Clone, Debug, Eq, PartialEq, Hash)]
131pub struct MatchesOperator {
132 pub rf: Option<MatchRef>,
133 pub operator: BooleanOperator,
134}
135
136impl ToSql for MatchesOperator {
137 fn fmt_sql(&self, f: &mut String, sql_fmt: SqlFormat) {
138 let matches_operator: crate::sql::operator::MatchesOperator = self.clone().into();
139 matches_operator.fmt_sql(f, sql_fmt);
140 }
141}
142
143#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
146pub enum BooleanOperator {
147 And,
148 Or,
149}
150
151impl ToSql for BooleanOperator {
152 fn fmt_sql(&self, f: &mut String, _fmt: SqlFormat) {
153 match self {
154 Self::And => f.push_str("AND"),
155 Self::Or => f.push_str("OR"),
156 }
157 }
158}
159
160#[derive(Clone, Debug, Eq, PartialEq, Hash)]
161pub enum NearestNeighbor {
162 K(u32, Distance),
164 KTree(u32),
166 Approximate(u32, u32),
168}
169
170impl ToSql for NearestNeighbor {
171 fn fmt_sql(&self, f: &mut String, sql_fmt: SqlFormat) {
172 let nn: crate::sql::operator::NearestNeighbor = self.clone().into();
173 nn.fmt_sql(f, sql_fmt);
174 }
175}
176
177impl ToSql for BinaryOperator {
178 fn fmt_sql(&self, f: &mut String, sql_fmt: SqlFormat) {
179 let binary_operator: crate::sql::BinaryOperator = self.clone().into();
180 binary_operator.fmt_sql(f, sql_fmt);
181 }
182}
183
184#[derive(Clone, Debug, Eq, PartialEq, Hash)]
185pub enum AssignOperator {
186 Assign,
187 Add,
188 Subtract,
189 Extend,
190}
191
192impl ToSql for AssignOperator {
193 fn fmt_sql(&self, f: &mut String, sql_fmt: SqlFormat) {
194 let op: crate::sql::AssignOperator = self.clone().into();
195 op.fmt_sql(f, sql_fmt);
196 }
197}
198
199#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
204#[allow(dead_code)]
205pub enum BindingPower {
206 Nullish,
207 Or,
208 And,
209 Equality,
210 Relation,
211 AddSub,
212 MulDiv,
213 Power,
214 Range,
215 Prefix,
216 Postfix,
217 Prime,
218}
219
220#[allow(dead_code)]
221impl BindingPower {
222 pub fn for_binary_operator(op: &BinaryOperator) -> Self {
232 match op {
233 BinaryOperator::Or => BindingPower::Or,
234 BinaryOperator::And => BindingPower::And,
235
236 BinaryOperator::Equal
237 | BinaryOperator::ExactEqual
238 | BinaryOperator::NotEqual
239 | BinaryOperator::AllEqual
240 | BinaryOperator::AnyEqual => BindingPower::Equality,
241
242 BinaryOperator::LessThan
243 | BinaryOperator::LessThanEqual
244 | BinaryOperator::MoreThan
245 | BinaryOperator::MoreThanEqual
246 | BinaryOperator::Matches(_)
247 | BinaryOperator::Contain
248 | BinaryOperator::NotContain
249 | BinaryOperator::ContainAll
250 | BinaryOperator::ContainAny
251 | BinaryOperator::ContainNone
252 | BinaryOperator::Inside
253 | BinaryOperator::NotInside
254 | BinaryOperator::AllInside
255 | BinaryOperator::AnyInside
256 | BinaryOperator::NoneInside
257 | BinaryOperator::Outside
258 | BinaryOperator::Intersects
259 | BinaryOperator::NearestNeighbor(_) => BindingPower::Relation,
260
261 BinaryOperator::Add | BinaryOperator::Subtract => BindingPower::AddSub,
262
263 BinaryOperator::Multiply | BinaryOperator::Divide | BinaryOperator::Remainder => {
264 BindingPower::MulDiv
265 }
266
267 BinaryOperator::Power => BindingPower::Power,
268
269 BinaryOperator::NullCoalescing | BinaryOperator::TenaryCondition => {
270 BindingPower::Nullish
271 }
272
273 BinaryOperator::Range
274 | BinaryOperator::RangeInclusive
275 | BinaryOperator::RangeSkip
276 | BinaryOperator::RangeSkipInclusive => BindingPower::Range,
277 }
278 }
279
280 pub fn for_postfix_operator(op: &PostfixOperator) -> Self {
281 match op {
282 PostfixOperator::Range | PostfixOperator::RangeSkip => BindingPower::Range,
283 PostfixOperator::MethodCall(..) | PostfixOperator::Call(..) => BindingPower::Postfix,
284 }
285 }
286
287 pub fn for_prefix_operator(op: &PrefixOperator) -> Self {
288 match op {
289 PrefixOperator::Range | PrefixOperator::RangeInclusive => BindingPower::Range,
290 PrefixOperator::Not
291 | PrefixOperator::Positive
292 | PrefixOperator::Negate
293 | PrefixOperator::Cast(_) => BindingPower::Prefix,
294 }
295 }
296 pub fn for_expr(expr: &Expr) -> BindingPower {
301 match expr {
302 Expr::Prefix {
303 op,
304 ..
305 } => Self::for_prefix_operator(op),
306 Expr::Postfix {
307 op,
308 ..
309 } => BindingPower::for_postfix_operator(op),
310 Expr::Binary {
311 op,
312 ..
313 } => BindingPower::for_binary_operator(op),
314 _ => BindingPower::Prime,
315 }
316 }
317}