grafbase_sql_ast/ast/
conditions.rs1use crate::ast::{Expression, ExpressionKind, Select};
2
3use super::Table;
4
5#[derive(Debug, PartialEq, Clone, Default)]
7pub enum ConditionTree<'a> {
8 And(Vec<Expression<'a>>),
10 Or(Vec<Expression<'a>>),
12 Not(Box<Expression<'a>>),
14 Single(Box<Expression<'a>>),
16 #[default]
18 NoCondition,
19 NegativeCondition,
21 Exists(Box<Table<'a>>),
23}
24
25impl<'a> ConditionTree<'a> {
26 pub fn and<E>(mut self, other: E) -> ConditionTree<'a>
28 where
29 E: Into<Expression<'a>>,
30 {
31 match self {
32 Self::And(ref mut conditions) => {
33 conditions.push(other.into());
34 self
35 }
36 Self::Single(expr) => Self::And(vec![*expr, other.into()]),
37 _ => Self::And(vec![Expression::from(self), other.into()]),
38 }
39 }
40
41 pub fn or<E>(mut self, other: E) -> ConditionTree<'a>
43 where
44 E: Into<Expression<'a>>,
45 {
46 match self {
47 Self::Or(ref mut conditions) => {
48 conditions.push(other.into());
49 self
50 }
51 Self::Single(expr) => Self::Or(vec![*expr, other.into()]),
52 _ => Self::Or(vec![Expression::from(self), other.into()]),
53 }
54 }
55
56 pub fn not<E>(left: E) -> ConditionTree<'a>
58 where
59 E: Into<Expression<'a>>,
60 {
61 ConditionTree::Not(Box::new(left.into()))
62 }
63
64 pub fn single<E>(left: E) -> ConditionTree<'a>
66 where
67 E: Into<Expression<'a>>,
68 {
69 ConditionTree::Single(Box::new(left.into()))
70 }
71
72 pub fn exists<E>(select: E) -> ConditionTree<'a>
74 where
75 E: Into<Table<'a>>,
76 {
77 ConditionTree::Exists(Box::new(select.into()))
78 }
79
80 pub fn invert_if(self, invert: bool) -> ConditionTree<'a> {
82 if invert {
83 Self::not(self)
84 } else {
85 self
86 }
87 }
88}
89
90impl<'a> From<ConditionTree<'a>> for Expression<'a> {
91 fn from(ct: ConditionTree<'a>) -> Self {
92 Expression {
93 kind: ExpressionKind::ConditionTree(ct),
94 alias: None,
95 }
96 }
97}
98
99impl<'a> From<Select<'a>> for ConditionTree<'a> {
100 fn from(sel: Select<'a>) -> Self {
101 let exp = Expression {
102 kind: ExpressionKind::Value(Box::new(sel.into())),
103 alias: None,
104 };
105
106 ConditionTree::single(exp)
107 }
108}