Skip to main content

sql_builder/expr/
trait.rs

1use crate::{Expr, func::*, query::*, types::*, value::*};
2
3mod arithmetic;
4mod comparison;
5mod logical;
6mod membership;
7mod patterns;
8
9/// "Operator" methods for building expressions.
10///
11/// They can be called directly on any expression type, without constructing an
12/// [`Expr`] first:
13///
14/// ```no_run
15/// # use sql_builder::*;
16/// #
17/// let expr = 1_i32.cast_as("REAL");
18/// let expr = Func::char_length("abc").eq(3_i32);
19/// let expr = Expr::current_date().cast_as("TEXT").like("2024%");
20/// ```
21///
22/// If some methods are missing, look into [`BinOper::Custom`], [`Func::cust`],
23/// or [`Expr::cust*`][`Expr::cust_with_values`].
24pub trait ExprTrait: Sized {
25    #[doc = include_str!("trait/docs/add.md")]
26    fn add<R>(self, right: R) -> Expr
27    where
28        R: Into<Expr>,
29    {
30        arithmetic::add(self, right)
31    }
32
33    #[doc = include_str!("trait/docs/as_enum.md")]
34    #[allow(clippy::wrong_self_convention)]
35    fn as_enum<N>(self, type_name: N) -> Expr
36    where
37        N: IntoIden;
38
39    #[doc = include_str!("trait/docs/and.md")]
40    fn and<R>(self, right: R) -> Expr
41    where
42        R: Into<Expr>,
43    {
44        logical::and(self, right)
45    }
46
47    #[doc = include_str!("trait/docs/between.md")]
48    fn between<A, B>(self, a: A, b: B) -> Expr
49    where
50        A: Into<Expr>,
51        B: Into<Expr>,
52    {
53        comparison::between(self, a, b)
54    }
55
56    #[doc = include_str!("trait/docs/binary.md")]
57    fn binary<O, R>(self, op: O, right: R) -> Expr
58    where
59        O: Into<BinOper>,
60        R: Into<Expr>;
61
62    #[doc = include_str!("trait/docs/cast_as.md")]
63    fn cast_as<N>(self, type_name: N) -> Expr
64    where
65        N: IntoIden;
66
67    #[doc = include_str!("trait/docs/div.md")]
68    fn div<R>(self, right: R) -> Expr
69    where
70        R: Into<Expr>,
71    {
72        arithmetic::div(self, right)
73    }
74
75    #[doc = include_str!("trait/docs/eq.md")]
76    fn eq<R>(self, right: R) -> Expr
77    where
78        R: Into<Expr>,
79    {
80        comparison::eq(self, right)
81    }
82
83    #[doc = include_str!("trait/docs/equals.md")]
84    fn equals<C>(self, col: C) -> Expr
85    where
86        C: IntoColumnRef,
87    {
88        comparison::equals(self, col)
89    }
90
91    #[doc = include_str!("trait/docs/gt.md")]
92    fn gt<R>(self, right: R) -> Expr
93    where
94        R: Into<Expr>,
95    {
96        comparison::gt(self, right)
97    }
98
99    #[doc = include_str!("trait/docs/gte.md")]
100    fn gte<R>(self, right: R) -> Expr
101    where
102        R: Into<Expr>,
103    {
104        comparison::gte(self, right)
105    }
106
107    #[doc = include_str!("trait/docs/in_subquery.md")]
108    fn in_subquery(self, sel: SelectStatement) -> Expr {
109        membership::in_subquery(self, sel)
110    }
111
112    #[doc = include_str!("trait/docs/in_tuples.md")]
113    fn in_tuples<V, I>(self, v: I) -> Expr
114    where
115        V: IntoValueTuple,
116        I: IntoIterator<Item = V>,
117    {
118        membership::in_tuples(self, v)
119    }
120
121    #[doc = include_str!("trait/docs/is.md")]
122    fn is<R>(self, right: R) -> Expr
123    where
124        R: Into<Expr>,
125    {
126        comparison::is(self, right)
127    }
128
129    #[doc = include_str!("trait/docs/is_in.md")]
130    #[allow(clippy::wrong_self_convention)]
131    fn is_in<V, I>(self, v: I) -> Expr
132    where
133        V: Into<Expr>,
134        I: IntoIterator<Item = V>,
135    {
136        membership::is_in(self, v)
137    }
138
139    #[doc = include_str!("trait/docs/is_not.md")]
140    #[allow(clippy::wrong_self_convention)]
141    fn is_not<R>(self, right: R) -> Expr
142    where
143        R: Into<Expr>,
144    {
145        comparison::is_not(self, right)
146    }
147
148    #[doc = include_str!("trait/docs/is_not_in.md")]
149    #[allow(clippy::wrong_self_convention)]
150    fn is_not_in<V, I>(self, v: I) -> Expr
151    where
152        V: Into<Expr>,
153        I: IntoIterator<Item = V>,
154    {
155        membership::is_not_in(self, v)
156    }
157
158    #[doc = include_str!("trait/docs/is_not_null.md")]
159    #[allow(clippy::wrong_self_convention)]
160    fn is_not_null(self) -> Expr {
161        comparison::is_not_null(self)
162    }
163
164    #[doc = include_str!("trait/docs/is_null.md")]
165    #[allow(clippy::wrong_self_convention)]
166    fn is_null(self) -> Expr {
167        comparison::is_null(self)
168    }
169
170    #[doc = include_str!("trait/docs/left_shift.md")]
171    fn left_shift<R>(self, right: R) -> Expr
172    where
173        R: Into<Expr>,
174    {
175        arithmetic::left_shift(self, right)
176    }
177
178    #[doc = include_str!("trait/docs/like.md")]
179    fn like<L>(self, like: L) -> Expr
180    where
181        L: IntoLikeExpr,
182    {
183        patterns::like(self, like)
184    }
185
186    #[doc = include_str!("trait/docs/lt.md")]
187    fn lt<R>(self, right: R) -> Expr
188    where
189        R: Into<Expr>,
190    {
191        comparison::lt(self, right)
192    }
193
194    #[doc = include_str!("trait/docs/lte.md")]
195    fn lte<R>(self, right: R) -> Expr
196    where
197        R: Into<Expr>,
198    {
199        comparison::lte(self, right)
200    }
201
202    #[doc = include_str!("trait/docs/modulo.md")]
203    fn modulo<R>(self, right: R) -> Expr
204    where
205        R: Into<Expr>,
206    {
207        arithmetic::modulo(self, right)
208    }
209
210    #[doc = include_str!("trait/docs/mul.md")]
211    fn mul<R>(self, right: R) -> Expr
212    where
213        R: Into<Expr>,
214    {
215        arithmetic::mul(self, right)
216    }
217
218    #[doc = include_str!("trait/docs/ne.md")]
219    fn ne<R>(self, right: R) -> Expr
220    where
221        R: Into<Expr>,
222    {
223        comparison::ne(self, right)
224    }
225
226    #[doc = include_str!("trait/docs/not.md")]
227    fn not(self) -> Expr {
228        logical::not(self)
229    }
230
231    #[doc = include_str!("trait/docs/not_between.md")]
232    fn not_between<A, B>(self, a: A, b: B) -> Expr
233    where
234        A: Into<Expr>,
235        B: Into<Expr>,
236    {
237        comparison::not_between(self, a, b)
238    }
239
240    #[doc = include_str!("trait/docs/not_equals.md")]
241    fn not_equals<C>(self, col: C) -> Expr
242    where
243        C: IntoColumnRef,
244    {
245        comparison::not_equals(self, col)
246    }
247
248    #[doc = include_str!("trait/docs/not_in_subquery.md")]
249    fn not_in_subquery(self, sel: SelectStatement) -> Expr {
250        membership::not_in_subquery(self, sel)
251    }
252
253    #[doc = include_str!("trait/docs/not_like.md")]
254    fn not_like<L>(self, like: L) -> Expr
255    where
256        L: IntoLikeExpr,
257    {
258        patterns::not_like(self, like)
259    }
260
261    #[doc = include_str!("trait/docs/or.md")]
262    fn or<R>(self, right: R) -> Expr
263    where
264        R: Into<Expr>,
265    {
266        logical::or(self, right)
267    }
268
269    #[doc = include_str!("trait/docs/right_shift.md")]
270    fn right_shift<R>(self, right: R) -> Expr
271    where
272        R: Into<Expr>,
273    {
274        arithmetic::right_shift(self, right)
275    }
276
277    #[doc = include_str!("trait/docs/sub.md")]
278    fn sub<R>(self, right: R) -> Expr
279    where
280        R: Into<Expr>,
281    {
282        arithmetic::sub(self, right)
283    }
284
285    #[doc = include_str!("trait/docs/unary.md")]
286    fn unary(self, o: UnOper) -> Expr;
287
288    #[doc = include_str!("trait/docs/bit_and.md")]
289    fn bit_and<R>(self, right: R) -> Expr
290    where
291        R: Into<Expr>,
292    {
293        arithmetic::bit_and(self, right)
294    }
295
296    #[doc = include_str!("trait/docs/bit_or.md")]
297    fn bit_or<R>(self, right: R) -> Expr
298    where
299        R: Into<Expr>,
300    {
301        arithmetic::bit_or(self, right)
302    }
303
304    #[doc = include_str!("trait/docs/max.md")]
305    fn max(self) -> Expr;
306
307    #[doc = include_str!("trait/docs/min.md")]
308    fn min(self) -> Expr;
309
310    #[doc = include_str!("trait/docs/sum.md")]
311    fn sum(self) -> Expr;
312
313    #[doc = include_str!("trait/docs/avg.md")]
314    fn avg(self) -> Expr;
315
316    #[doc = include_str!("trait/docs/count.md")]
317    fn count(self) -> Expr;
318
319    #[doc = include_str!("trait/docs/count_distinct.md")]
320    fn count_distinct(self) -> Expr;
321
322    #[doc = include_str!("trait/docs/if_null.md")]
323    fn if_null<V>(self, v: V) -> Expr
324    where
325        V: Into<Expr>;
326}
327
328/// This generic implementation covers all expression types,
329/// including [ColumnRef], [Value], [FunctionCall], [Expr]...
330impl<T> ExprTrait for T
331where
332    T: Into<Expr>,
333{
334    fn as_enum<N>(self, type_name: N) -> Expr
335    where
336        N: IntoIden,
337    {
338        Expr::AsEnum(type_name.into_iden(), Box::new(self.into()))
339    }
340
341    fn binary<O, R>(self, op: O, right: R) -> Expr
342    where
343        O: Into<BinOper>,
344        R: Into<Expr>,
345    {
346        Expr::Binary(Box::new(self.into()), op.into(), Box::new(right.into()))
347    }
348
349    fn cast_as<N>(self, type_name: N) -> Expr
350    where
351        N: IntoIden,
352    {
353        Expr::FunctionCall(Func::cast_as(self, type_name))
354    }
355
356    fn unary(self, op: UnOper) -> Expr {
357        Expr::Unary(op, Box::new(self.into()))
358    }
359
360    fn max(self) -> Expr {
361        Func::max(self).into()
362    }
363
364    fn min(self) -> Expr {
365        Func::min(self).into()
366    }
367
368    fn sum(self) -> Expr {
369        Func::sum(self).into()
370    }
371
372    fn avg(self) -> Expr {
373        Func::avg(self).into()
374    }
375
376    fn count(self) -> Expr {
377        Func::count(self).into()
378    }
379
380    fn count_distinct(self) -> Expr {
381        Func::count_distinct(self).into()
382    }
383
384    fn if_null<V>(self, v: V) -> Expr
385    where
386        V: Into<Expr>,
387    {
388        Func::if_null(self, v).into()
389    }
390}