gluesql_core/ast_builder/expr/
binary_op.rs1use {super::ExprNode, crate::ast::BinaryOperator};
2
3impl ExprNode<'_> {
4 fn binary_op<T: Into<Self>>(self, op: BinaryOperator, other: T) -> Self {
5 Self::BinaryOp {
6 left: Box::new(self),
7 op,
8 right: Box::new(other.into()),
9 }
10 }
11
12 #[allow(clippy::should_implement_trait)]
13 #[must_use]
14 pub fn add<T: Into<Self>>(self, other: T) -> Self {
15 self.binary_op(BinaryOperator::Plus, other)
16 }
17
18 #[allow(clippy::should_implement_trait)]
19 #[must_use]
20 pub fn sub<T: Into<Self>>(self, other: T) -> Self {
21 self.binary_op(BinaryOperator::Minus, other)
22 }
23
24 #[allow(clippy::should_implement_trait)]
25 #[must_use]
26 pub fn mul<T: Into<Self>>(self, other: T) -> Self {
27 self.binary_op(BinaryOperator::Multiply, other)
28 }
29
30 #[allow(clippy::should_implement_trait)]
31 #[must_use]
32 pub fn div<T: Into<Self>>(self, other: T) -> Self {
33 self.binary_op(BinaryOperator::Divide, other)
34 }
35
36 #[must_use]
37 pub fn modulo<T: Into<Self>>(self, other: T) -> Self {
38 self.binary_op(BinaryOperator::Modulo, other)
39 }
40
41 #[must_use]
42 pub fn concat<T: Into<Self>>(self, other: T) -> Self {
43 self.binary_op(BinaryOperator::StringConcat, other)
44 }
45
46 #[must_use]
47 pub fn gt<T: Into<Self>>(self, other: T) -> Self {
48 self.binary_op(BinaryOperator::Gt, other)
49 }
50
51 #[must_use]
52 pub fn lt<T: Into<Self>>(self, other: T) -> Self {
53 self.binary_op(BinaryOperator::Lt, other)
54 }
55
56 #[must_use]
57 pub fn gte<T: Into<Self>>(self, other: T) -> Self {
58 self.binary_op(BinaryOperator::GtEq, other)
59 }
60
61 #[must_use]
62 pub fn lte<T: Into<Self>>(self, other: T) -> Self {
63 self.binary_op(BinaryOperator::LtEq, other)
64 }
65
66 #[must_use]
67 pub fn eq<T: Into<Self>>(self, other: T) -> Self {
68 self.binary_op(BinaryOperator::Eq, other)
69 }
70
71 #[must_use]
72 pub fn neq<T: Into<Self>>(self, other: T) -> Self {
73 self.binary_op(BinaryOperator::NotEq, other)
74 }
75
76 #[must_use]
77 pub fn and<T: Into<Self>>(self, other: T) -> Self {
78 self.binary_op(BinaryOperator::And, other)
79 }
80
81 #[must_use]
82 pub fn or<T: Into<Self>>(self, other: T) -> Self {
83 self.binary_op(BinaryOperator::Or, other)
84 }
85
86 #[must_use]
87 pub fn bitwise_and<T: Into<Self>>(self, other: T) -> Self {
88 self.binary_op(BinaryOperator::BitwiseAnd, other)
89 }
90
91 #[must_use]
92 pub fn bitwise_shift_left<T: Into<Self>>(self, other: T) -> Self {
93 self.binary_op(BinaryOperator::BitwiseShiftLeft, other)
94 }
95
96 #[must_use]
97 pub fn bitwise_shift_right<T: Into<Self>>(self, other: T) -> Self {
98 self.binary_op(BinaryOperator::BitwiseShiftRight, other)
99 }
100}
101
102#[cfg(test)]
103mod tests {
104 use crate::ast_builder::{col, num, test_expr, text};
105
106 #[test]
107 fn binary_op() {
108 let actual = col("id").add(10);
109 let expected = "id + 10";
110 test_expr(actual, expected);
111
112 let actual = num(10).sub(text("abc"));
113 let expected = "10 - 'abc'";
114 test_expr(actual, expected);
115
116 let actual = col("rate").mul("amount");
117 let expected = "rate * amount";
118 test_expr(actual, expected);
119
120 let actual = col("amount").div(30);
121 let expected = "amount / 30";
122 test_expr(actual, expected);
123
124 let actual = col("amount").modulo(30);
125 let expected = "amount % 30";
126 test_expr(actual, expected);
127
128 let actual = text("hello").concat("'world'");
129 let expected = "'hello' || 'world'";
130 test_expr(actual, expected);
131
132 let actual = col("id").gt(col("Bar.id"));
133 let expected = "id > Bar.id";
134 test_expr(actual, expected);
135
136 let actual = col("id").lt(col("Bar.id"));
137 let expected = "id < Bar.id";
138 test_expr(actual, expected);
139
140 let actual = col("id").gte(col("Bar.id"));
141 let expected = "id >= Bar.id";
142 test_expr(actual, expected);
143
144 let actual = col("id").lte(col("Bar.id"));
145 let expected = "id <= Bar.id";
146 test_expr(actual, expected);
147
148 let actual = col("id").eq(10);
149 let expected = "id = 10";
150 test_expr(actual, expected);
151
152 let actual = col("id").neq("'abcde'");
153 let expected = "id != 'abcde'";
154 test_expr(actual, expected);
155
156 let actual = (col("id").gt(num(10))).and(col("id").lt(num(20)));
157 let expected = "id > 10 AND id < 20";
158 test_expr(actual, expected);
159
160 let actual = (col("id").gt(num(10))).or(col("id").lt(num(20)));
161 let expected = "id > 10 OR id < 20";
162 test_expr(actual, expected);
163
164 let actual = col("id").bitwise_and(col("value"));
165 let expected = "id & value";
166 test_expr(actual, expected);
167
168 let actual = col("id").bitwise_shift_left(num(1));
169 let expected = "id << 1";
170 test_expr(actual, expected);
171
172 let actual = col("id").bitwise_shift_right(num(1));
173 let expected = "id >> 1";
174 test_expr(actual, expected);
175 }
176}