1use std::marker::PhantomData;
2use std::ops::{Add, Mul, Sub};
3
4use crate::compile::CompiledSql;
5use crate::schema::{Column, ColumnRef};
6use crate::types::{PgInterval, PgVector};
7
8#[derive(Debug, Clone, PartialEq)]
9pub enum Value {
10 Null,
11 Bool(bool),
12 I16(i16),
13 I32(i32),
14 I64(i64),
15 F32(f32),
16 F64(f64),
17 String(String),
18 Array(Vec<String>),
19 Bytes(Vec<u8>),
20 Json(serde_json::Value),
21 Uuid(uuid::Uuid),
22 DateTime(chrono::NaiveDateTime),
23 DateTimeUtc(chrono::DateTime<chrono::Utc>),
24 Date(chrono::NaiveDate),
25 Time(chrono::NaiveTime),
26 Interval(PgInterval),
27 Vector(Vec<f32>),
28 Enum { type_name: &'static str, value: String },
29}
30
31pub trait ColumnValue<T> {
32 fn into_value(self) -> Option<Value>;
33}
34
35impl<T> ColumnValue<T> for T
36where
37 T: Into<Value>,
38{
39 fn into_value(self) -> Option<Value> {
40 Some(self.into())
41 }
42}
43
44impl<T> ColumnValue<T> for Option<T>
45where
46 T: Into<Value>,
47{
48 fn into_value(self) -> Option<Value> {
49 self.map(Into::into)
50 }
51}
52
53impl ColumnValue<String> for &str {
54 fn into_value(self) -> Option<Value> {
55 Some(Value::String(self.to_string()))
56 }
57}
58
59impl<T> ColumnValue<T> for &T
60where
61 T: Clone + Into<Value>,
62{
63 fn into_value(self) -> Option<Value> {
64 Some(self.clone().into())
65 }
66}
67
68impl<T> ColumnValue<Option<T>> for T
69where
70 T: Into<Value>,
71{
72 fn into_value(self) -> Option<Value> {
73 Some(self.into())
74 }
75}
76
77impl<T> ColumnValue<Option<T>> for &T
78where
79 T: Clone + Into<Value>,
80{
81 fn into_value(self) -> Option<Value> {
82 Some(self.clone().into())
83 }
84}
85
86impl ColumnValue<Option<String>> for &str {
87 fn into_value(self) -> Option<Value> {
88 Some(Value::String(self.to_string()))
89 }
90}
91
92impl From<bool> for Value {
93 fn from(value: bool) -> Self {
94 Self::Bool(value)
95 }
96}
97
98impl From<i16> for Value {
99 fn from(value: i16) -> Self {
100 Self::I16(value)
101 }
102}
103
104impl From<i32> for Value {
105 fn from(value: i32) -> Self {
106 Self::I32(value)
107 }
108}
109
110impl From<i64> for Value {
111 fn from(value: i64) -> Self {
112 Self::I64(value)
113 }
114}
115
116impl From<f32> for Value {
117 fn from(value: f32) -> Self {
118 Self::F32(value)
119 }
120}
121
122impl From<f64> for Value {
123 fn from(value: f64) -> Self {
124 Self::F64(value)
125 }
126}
127
128impl From<String> for Value {
129 fn from(value: String) -> Self {
130 Self::String(value)
131 }
132}
133
134impl From<&str> for Value {
135 fn from(value: &str) -> Self {
136 Self::String(value.to_string())
137 }
138}
139
140impl From<Vec<String>> for Value {
141 fn from(value: Vec<String>) -> Self {
142 Self::Array(value)
143 }
144}
145
146impl From<Vec<u8>> for Value {
147 fn from(value: Vec<u8>) -> Self {
148 Self::Bytes(value)
149 }
150}
151
152impl From<serde_json::Value> for Value {
153 fn from(value: serde_json::Value) -> Self {
154 Self::Json(value)
155 }
156}
157
158impl From<uuid::Uuid> for Value {
159 fn from(value: uuid::Uuid) -> Self {
160 Self::Uuid(value)
161 }
162}
163
164impl From<chrono::NaiveDateTime> for Value {
165 fn from(value: chrono::NaiveDateTime) -> Self {
166 Self::DateTime(value)
167 }
168}
169
170impl From<chrono::DateTime<chrono::Utc>> for Value {
171 fn from(value: chrono::DateTime<chrono::Utc>) -> Self {
172 Self::DateTimeUtc(value)
173 }
174}
175
176impl From<chrono::NaiveDate> for Value {
177 fn from(value: chrono::NaiveDate) -> Self {
178 Self::Date(value)
179 }
180}
181
182impl From<chrono::NaiveTime> for Value {
183 fn from(value: chrono::NaiveTime) -> Self {
184 Self::Time(value)
185 }
186}
187
188impl From<PgInterval> for Value {
189 fn from(value: PgInterval) -> Self {
190 Self::Interval(value)
191 }
192}
193
194impl<const N: usize> From<PgVector<N>> for Value {
195 fn from(value: PgVector<N>) -> Self {
196 Self::Vector(value.to_vec())
197 }
198}
199
200impl<T> From<Option<T>> for Value
201where
202 T: Into<Value>,
203{
204 fn from(value: Option<T>) -> Self {
205 match value {
206 Some(v) => v.into(),
207 None => Self::Null,
208 }
209 }
210}
211
212#[derive(Debug, Clone, Copy)]
213pub enum BinaryOp {
214 Add,
215 Sub,
216 Mul,
217 Eq,
218 Ne,
219 IsDistinctFrom,
220 IsNotDistinctFrom,
221 Lt,
222 Le,
223 Gt,
224 Ge,
225}
226
227#[derive(Debug, Clone, Copy)]
228pub enum BoolOp {
229 And,
230 Or,
231}
232
233#[derive(Debug, Clone, Copy)]
234pub enum UnaryOp {
235 Not,
236}
237
238#[derive(Debug, Clone, Copy)]
239pub enum VectorBinaryOp {
240 L2Distance,
241 CosineDistance,
242 InnerProductDistance,
243 L1Distance,
244}
245
246#[derive(Debug, Clone, Copy)]
247pub enum IntervalField {
248 Days,
249 Hours,
250 Minutes,
251 Seconds,
252}
253
254#[derive(Debug, Clone, Copy)]
255pub enum TrimDirection {
256 Both,
257 Leading,
258 Trailing,
259}
260
261#[derive(Debug, Clone)]
262pub enum ExprNode {
263 Column(ColumnRef),
264 Value(Value),
265 Row {
266 values: Vec<ExprNode>,
267 },
268 Func {
269 name: &'static str,
270 args: Vec<ExprNode>,
271 },
272 Normalize {
273 expr: Box<ExprNode>,
274 form: crate::func::NormalizationForm,
275 },
276 Trim {
277 direction: TrimDirection,
278 expr: Box<ExprNode>,
279 characters: Option<Box<ExprNode>>,
280 },
281 AggregateFilter {
282 aggregate: Box<ExprNode>,
283 predicate: Box<ExprNode>,
284 },
285 VectorBinary {
286 left: Box<ExprNode>,
287 op: VectorBinaryOp,
288 right: Box<ExprNode>,
289 },
290 MakeInterval {
291 field: IntervalField,
292 value: Box<ExprNode>,
293 },
294 Binary {
295 left: Box<ExprNode>,
296 op: BinaryOp,
297 right: Box<ExprNode>,
298 },
299 Bool {
300 left: Box<ExprNode>,
301 op: BoolOp,
302 right: Box<ExprNode>,
303 },
304 Unary {
305 op: UnaryOp,
306 expr: Box<ExprNode>,
307 },
308 In {
309 expr: Box<ExprNode>,
310 values: Vec<Value>,
311 },
312 RowIn {
313 expr: Box<ExprNode>,
314 rows: Vec<Vec<Value>>,
315 },
316 IsNull {
317 expr: Box<ExprNode>,
318 negated: bool,
319 },
320 Like {
321 expr: Box<ExprNode>,
322 pattern: Value,
323 case_insensitive: bool,
324 },
325 Exists {
326 subquery: CompiledSql,
327 },
328}
329
330#[derive(Debug, Clone)]
331#[doc(hidden)]
332pub struct ScalarExpression;
333
334#[derive(Debug, Clone)]
335#[doc(hidden)]
336pub struct AggregateExpression;
337
338#[derive(Debug, Clone)]
339pub struct Expr<T, Kind = ScalarExpression> {
340 pub node: ExprNode,
341 _marker: PhantomData<(T, Kind)>,
342}
343
344pub type AggregateExpr<T> = Expr<T, AggregateExpression>;
345
346#[derive(Debug, Clone)]
347pub struct RowExpr<T> {
348 node: ExprNode,
349 _marker: PhantomData<T>,
350}
351
352impl<T, Kind> Expr<T, Kind> {
353 pub fn new(node: ExprNode) -> Self {
354 Self {
355 node,
356 _marker: PhantomData,
357 }
358 }
359}
360
361impl<T> AggregateExpr<T> {
362 pub fn filter(self, predicate: Expr<bool>) -> Expr<T> {
364 Expr::new(ExprNode::AggregateFilter {
365 aggregate: Box::new(self.node),
366 predicate: Box::new(predicate.node),
367 })
368 }
369}
370
371impl<T> RowExpr<T> {
372 pub fn new(node: ExprNode) -> Self {
373 Self {
374 node,
375 _marker: PhantomData,
376 }
377 }
378}
379
380pub trait IntoExpr<T> {
381 fn into_expr(self) -> Expr<T>;
382}
383
384pub trait ExprOperand {
385 type Value;
386
387 fn into_operand_expr(self) -> Expr<Self::Value>;
388}
389
390#[doc(hidden)]
391pub struct ValueComparisonMarker;
392
393#[doc(hidden)]
394pub struct ExprComparisonMarker;
395
396pub trait ComparisonValue<T, Marker = ValueComparisonMarker> {
397 fn into_comparison_expr(self) -> Expr<T>;
398}
399
400pub trait SqlAdd<Rhs> {
401 type Output;
402}
403
404pub trait SqlSub<Rhs> {
405 type Output;
406}
407
408pub trait SqlMul<Rhs> {
409 type Output;
410}
411
412pub trait NumericExprType {}
413
414mod row_columns_private {
415 pub trait Sealed {}
416}
417
418pub trait RowColumns: row_columns_private::Sealed {
419 type ValueTuple;
420
421 fn into_row_expr(self) -> RowExpr<Self::ValueTuple>;
422}
423
424pub fn row<R>(columns: R) -> RowExpr<R::ValueTuple>
425where
426 R: RowColumns,
427{
428 columns.into_row_expr()
429}
430
431impl<T, Kind> IntoExpr<T> for Expr<T, Kind> {
432 fn into_expr(self) -> Expr<T> {
433 Expr::new(self.node)
434 }
435}
436
437impl<T, Kind> ExprOperand for Expr<T, Kind> {
438 type Value = T;
439
440 fn into_operand_expr(self) -> Expr<Self::Value> {
441 self.into_expr()
442 }
443}
444
445impl<T, Kind> ComparisonValue<T, ExprComparisonMarker> for Expr<T, Kind> {
446 fn into_comparison_expr(self) -> Expr<T> {
447 self.into_expr()
448 }
449}
450
451impl<T, Kind> ComparisonValue<Option<T>, ExprComparisonMarker> for Expr<T, Kind> {
452 fn into_comparison_expr(self) -> Expr<Option<T>> {
453 Expr::new(self.node)
454 }
455}
456
457impl<M, T> IntoExpr<T> for Column<M, T> {
458 fn into_expr(self) -> Expr<T> {
459 Expr::new(ExprNode::Column(self.as_ref()))
460 }
461}
462
463impl<M, T> ExprOperand for Column<M, T> {
464 type Value = T;
465
466 fn into_operand_expr(self) -> Expr<Self::Value> {
467 self.into_expr()
468 }
469}
470
471impl<M, T> ComparisonValue<T, ExprComparisonMarker> for Column<M, T> {
472 fn into_comparison_expr(self) -> Expr<T> {
473 self.into_expr()
474 }
475}
476
477impl<M, T> ComparisonValue<Option<T>, ExprComparisonMarker> for Column<M, T> {
478 fn into_comparison_expr(self) -> Expr<Option<T>> {
479 Expr::new(ExprNode::Column(self.as_ref()))
480 }
481}
482
483macro_rules! impl_row_tuple_support {
484 ($(($($model:ident:$col_ty:ident:$col_ident:ident:$value_ty:ident:$value_ident:ident),+)),+ $(,)?) => {
485 $(
486 impl<$($model, $col_ty),+> RowColumns for ($(Column<$model, $col_ty>,)+) {
487 type ValueTuple = ($($col_ty,)+);
488
489 fn into_row_expr(self) -> RowExpr<Self::ValueTuple> {
490 let ($($col_ident,)+) = self;
491 RowExpr::new(ExprNode::Row {
492 values: vec![$(ExprNode::Column($col_ident.as_ref())),+],
493 })
494 }
495 }
496
497 impl<$($model, $col_ty),+> row_columns_private::Sealed for ($(Column<$model, $col_ty>,)+) {}
498
499 impl<$($col_ty),+> RowExpr<($($col_ty,)+)> {
500 pub fn in_<I, $($value_ty),+>(self, values: I) -> Expr<bool>
501 where
502 I: IntoIterator<Item = ($($value_ty,)+)>,
503 $($value_ty: ColumnValue<$col_ty>,)+
504 {
505 let rows = values
506 .into_iter()
507 .map(|($($value_ident,)+)| vec![$($value_ident.into_value().unwrap_or(Value::Null)),+])
508 .collect();
509
510 Expr::new(ExprNode::RowIn {
511 expr: Box::new(self.node),
512 rows,
513 })
514 }
515 }
516 )+
517 };
518}
519
520impl_row_tuple_support!(
521 (M1:T1:c1:V1:v1, M2:T2:c2:V2:v2),
522 (M1:T1:c1:V1:v1, M2:T2:c2:V2:v2, M3:T3:c3:V3:v3),
523 (M1:T1:c1:V1:v1, M2:T2:c2:V2:v2, M3:T3:c3:V3:v3, M4:T4:c4:V4:v4),
524 (M1:T1:c1:V1:v1, M2:T2:c2:V2:v2, M3:T3:c3:V3:v3, M4:T4:c4:V4:v4, M5:T5:c5:V5:v5),
525 (M1:T1:c1:V1:v1, M2:T2:c2:V2:v2, M3:T3:c3:V3:v3, M4:T4:c4:V4:v4, M5:T5:c5:V5:v5, M6:T6:c6:V6:v6),
526 (M1:T1:c1:V1:v1, M2:T2:c2:V2:v2, M3:T3:c3:V3:v3, M4:T4:c4:V4:v4, M5:T5:c5:V5:v5, M6:T6:c6:V6:v6, M7:T7:c7:V7:v7),
527 (M1:T1:c1:V1:v1, M2:T2:c2:V2:v2, M3:T3:c3:V3:v3, M4:T4:c4:V4:v4, M5:T5:c5:V5:v5, M6:T6:c6:V6:v6, M7:T7:c7:V7:v7, M8:T8:c8:V8:v8),
528 (M1:T1:c1:V1:v1, M2:T2:c2:V2:v2, M3:T3:c3:V3:v3, M4:T4:c4:V4:v4, M5:T5:c5:V5:v5, M6:T6:c6:V6:v6, M7:T7:c7:V7:v7, M8:T8:c8:V8:v8, M9:T9:c9:V9:v9),
529 (M1:T1:c1:V1:v1, M2:T2:c2:V2:v2, M3:T3:c3:V3:v3, M4:T4:c4:V4:v4, M5:T5:c5:V5:v5, M6:T6:c6:V6:v6, M7:T7:c7:V7:v7, M8:T8:c8:V8:v8, M9:T9:c9:V9:v9, M10:T10:c10:V10:v10),
530 (M1:T1:c1:V1:v1, M2:T2:c2:V2:v2, M3:T3:c3:V3:v3, M4:T4:c4:V4:v4, M5:T5:c5:V5:v5, M6:T6:c6:V6:v6, M7:T7:c7:V7:v7, M8:T8:c8:V8:v8, M9:T9:c9:V9:v9, M10:T10:c10:V10:v10, M11:T11:c11:V11:v11),
531 (M1:T1:c1:V1:v1, M2:T2:c2:V2:v2, M3:T3:c3:V3:v3, M4:T4:c4:V4:v4, M5:T5:c5:V5:v5, M6:T6:c6:V6:v6, M7:T7:c7:V7:v7, M8:T8:c8:V8:v8, M9:T9:c9:V9:v9, M10:T10:c10:V10:v10, M11:T11:c11:V11:v11, M12:T12:c12:V12:v12),
532 (M1:T1:c1:V1:v1, M2:T2:c2:V2:v2, M3:T3:c3:V3:v3, M4:T4:c4:V4:v4, M5:T5:c5:V5:v5, M6:T6:c6:V6:v6, M7:T7:c7:V7:v7, M8:T8:c8:V8:v8, M9:T9:c9:V9:v9, M10:T10:c10:V10:v10, M11:T11:c11:V11:v11, M12:T12:c12:V12:v12, M13:T13:c13:V13:v13),
533 (M1:T1:c1:V1:v1, M2:T2:c2:V2:v2, M3:T3:c3:V3:v3, M4:T4:c4:V4:v4, M5:T5:c5:V5:v5, M6:T6:c6:V6:v6, M7:T7:c7:V7:v7, M8:T8:c8:V8:v8, M9:T9:c9:V9:v9, M10:T10:c10:V10:v10, M11:T11:c11:V11:v11, M12:T12:c12:V12:v12, M13:T13:c13:V13:v13, M14:T14:c14:V14:v14),
534 (M1:T1:c1:V1:v1, M2:T2:c2:V2:v2, M3:T3:c3:V3:v3, M4:T4:c4:V4:v4, M5:T5:c5:V5:v5, M6:T6:c6:V6:v6, M7:T7:c7:V7:v7, M8:T8:c8:V8:v8, M9:T9:c9:V9:v9, M10:T10:c10:V10:v10, M11:T11:c11:V11:v11, M12:T12:c12:V12:v12, M13:T13:c13:V13:v13, M14:T14:c14:V14:v14, M15:T15:c15:V15:v15),
535 (M1:T1:c1:V1:v1, M2:T2:c2:V2:v2, M3:T3:c3:V3:v3, M4:T4:c4:V4:v4, M5:T5:c5:V5:v5, M6:T6:c6:V6:v6, M7:T7:c7:V7:v7, M8:T8:c8:V8:v8, M9:T9:c9:V9:v9, M10:T10:c10:V10:v10, M11:T11:c11:V11:v11, M12:T12:c12:V12:v12, M13:T13:c13:V13:v13, M14:T14:c14:V14:v14, M15:T15:c15:V15:v15, M16:T16:c16:V16:v16)
536);
537
538impl<T, V> ComparisonValue<T, ValueComparisonMarker> for V
539where
540 V: Into<Value>,
541{
542 fn into_comparison_expr(self) -> Expr<T> {
543 Expr::new(ExprNode::Value(self.into()))
544 }
545}
546
547impl IntoExpr<String> for String {
548 fn into_expr(self) -> Expr<String> {
549 Expr::new(ExprNode::Value(Value::String(self)))
550 }
551}
552
553impl ExprOperand for String {
554 type Value = String;
555
556 fn into_operand_expr(self) -> Expr<Self::Value> {
557 self.into_expr()
558 }
559}
560
561impl IntoExpr<String> for &str {
562 fn into_expr(self) -> Expr<String> {
563 Expr::new(ExprNode::Value(Value::String(self.to_string())))
564 }
565}
566
567impl ExprOperand for &str {
568 type Value = String;
569
570 fn into_operand_expr(self) -> Expr<Self::Value> {
571 self.into_expr()
572 }
573}
574
575impl IntoExpr<bool> for bool {
576 fn into_expr(self) -> Expr<bool> {
577 Expr::new(ExprNode::Value(Value::Bool(self)))
578 }
579}
580
581impl ExprOperand for bool {
582 type Value = bool;
583
584 fn into_operand_expr(self) -> Expr<Self::Value> {
585 self.into_expr()
586 }
587}
588
589impl IntoExpr<i16> for i16 {
590 fn into_expr(self) -> Expr<i16> {
591 Expr::new(ExprNode::Value(Value::I16(self)))
592 }
593}
594
595impl ExprOperand for i16 {
596 type Value = i16;
597
598 fn into_operand_expr(self) -> Expr<Self::Value> {
599 self.into_expr()
600 }
601}
602
603impl NumericExprType for i16 {}
604
605impl IntoExpr<i32> for i32 {
606 fn into_expr(self) -> Expr<i32> {
607 Expr::new(ExprNode::Value(Value::I32(self)))
608 }
609}
610
611impl ExprOperand for i32 {
612 type Value = i32;
613
614 fn into_operand_expr(self) -> Expr<Self::Value> {
615 self.into_expr()
616 }
617}
618
619impl NumericExprType for i32 {}
620
621impl IntoExpr<i64> for i64 {
622 fn into_expr(self) -> Expr<i64> {
623 Expr::new(ExprNode::Value(Value::I64(self)))
624 }
625}
626
627impl ExprOperand for i64 {
628 type Value = i64;
629
630 fn into_operand_expr(self) -> Expr<Self::Value> {
631 self.into_expr()
632 }
633}
634
635impl NumericExprType for i64 {}
636
637impl IntoExpr<f32> for f32 {
638 fn into_expr(self) -> Expr<f32> {
639 Expr::new(ExprNode::Value(Value::F32(self)))
640 }
641}
642
643impl ExprOperand for f32 {
644 type Value = f32;
645
646 fn into_operand_expr(self) -> Expr<Self::Value> {
647 self.into_expr()
648 }
649}
650
651impl NumericExprType for f32 {}
652
653impl IntoExpr<f64> for f64 {
654 fn into_expr(self) -> Expr<f64> {
655 Expr::new(ExprNode::Value(Value::F64(self)))
656 }
657}
658
659impl ExprOperand for f64 {
660 type Value = f64;
661
662 fn into_operand_expr(self) -> Expr<Self::Value> {
663 self.into_expr()
664 }
665}
666
667impl NumericExprType for f64 {}
668
669impl IntoExpr<uuid::Uuid> for uuid::Uuid {
670 fn into_expr(self) -> Expr<uuid::Uuid> {
671 Expr::new(ExprNode::Value(Value::Uuid(self)))
672 }
673}
674
675impl ExprOperand for uuid::Uuid {
676 type Value = uuid::Uuid;
677
678 fn into_operand_expr(self) -> Expr<Self::Value> {
679 self.into_expr()
680 }
681}
682
683impl IntoExpr<chrono::NaiveDateTime> for chrono::NaiveDateTime {
684 fn into_expr(self) -> Expr<chrono::NaiveDateTime> {
685 Expr::new(ExprNode::Value(Value::DateTime(self)))
686 }
687}
688
689impl ExprOperand for chrono::NaiveDateTime {
690 type Value = chrono::NaiveDateTime;
691
692 fn into_operand_expr(self) -> Expr<Self::Value> {
693 self.into_expr()
694 }
695}
696
697impl IntoExpr<chrono::DateTime<chrono::Utc>> for chrono::DateTime<chrono::Utc> {
698 fn into_expr(self) -> Expr<chrono::DateTime<chrono::Utc>> {
699 Expr::new(ExprNode::Value(Value::DateTimeUtc(self)))
700 }
701}
702
703impl ExprOperand for chrono::DateTime<chrono::Utc> {
704 type Value = chrono::DateTime<chrono::Utc>;
705
706 fn into_operand_expr(self) -> Expr<Self::Value> {
707 self.into_expr()
708 }
709}
710
711impl IntoExpr<chrono::NaiveDate> for chrono::NaiveDate {
712 fn into_expr(self) -> Expr<chrono::NaiveDate> {
713 Expr::new(ExprNode::Value(Value::Date(self)))
714 }
715}
716
717impl ExprOperand for chrono::NaiveDate {
718 type Value = chrono::NaiveDate;
719
720 fn into_operand_expr(self) -> Expr<Self::Value> {
721 self.into_expr()
722 }
723}
724
725impl IntoExpr<chrono::NaiveTime> for chrono::NaiveTime {
726 fn into_expr(self) -> Expr<chrono::NaiveTime> {
727 Expr::new(ExprNode::Value(Value::Time(self)))
728 }
729}
730
731impl ExprOperand for chrono::NaiveTime {
732 type Value = chrono::NaiveTime;
733
734 fn into_operand_expr(self) -> Expr<Self::Value> {
735 self.into_expr()
736 }
737}
738
739impl IntoExpr<PgInterval> for PgInterval {
740 fn into_expr(self) -> Expr<PgInterval> {
741 Expr::new(ExprNode::Value(Value::Interval(self)))
742 }
743}
744
745impl ExprOperand for PgInterval {
746 type Value = PgInterval;
747
748 fn into_operand_expr(self) -> Expr<Self::Value> {
749 self.into_expr()
750 }
751}
752
753impl IntoExpr<Vec<String>> for Vec<String> {
754 fn into_expr(self) -> Expr<Vec<String>> {
755 Expr::new(ExprNode::Value(Value::Array(self)))
756 }
757}
758
759impl ExprOperand for Vec<String> {
760 type Value = Vec<String>;
761
762 fn into_operand_expr(self) -> Expr<Self::Value> {
763 self.into_expr()
764 }
765}
766
767impl IntoExpr<Vec<u8>> for Vec<u8> {
768 fn into_expr(self) -> Expr<Vec<u8>> {
769 Expr::new(ExprNode::Value(Value::Bytes(self)))
770 }
771}
772
773impl ExprOperand for Vec<u8> {
774 type Value = Vec<u8>;
775
776 fn into_operand_expr(self) -> Expr<Self::Value> {
777 self.into_expr()
778 }
779}
780
781impl IntoExpr<serde_json::Value> for serde_json::Value {
782 fn into_expr(self) -> Expr<serde_json::Value> {
783 Expr::new(ExprNode::Value(Value::Json(self)))
784 }
785}
786
787impl ExprOperand for serde_json::Value {
788 type Value = serde_json::Value;
789
790 fn into_operand_expr(self) -> Expr<Self::Value> {
791 self.into_expr()
792 }
793}
794
795impl<const N: usize> IntoExpr<PgVector<N>> for PgVector<N> {
796 fn into_expr(self) -> Expr<PgVector<N>> {
797 Expr::new(ExprNode::Value(Value::from(self)))
798 }
799}
800
801impl<const N: usize> ExprOperand for PgVector<N> {
802 type Value = PgVector<N>;
803
804 fn into_operand_expr(self) -> Expr<Self::Value> {
805 self.into_expr()
806 }
807}
808
809macro_rules! impl_numeric_arithmetic {
810 ($($ty:ty),* $(,)?) => {
811 $(
812 impl SqlAdd<$ty> for $ty {
813 type Output = $ty;
814 }
815
816 impl SqlSub<$ty> for $ty {
817 type Output = $ty;
818 }
819
820 impl SqlMul<$ty> for $ty {
821 type Output = $ty;
822 }
823 )*
824 };
825}
826
827impl SqlAdd<i16> for i16 {
828 type Output = i32;
829}
830
831impl SqlSub<i16> for i16 {
832 type Output = i32;
833}
834
835impl SqlMul<i16> for i16 {
836 type Output = i32;
837}
838
839impl_numeric_arithmetic!(i32, i64, f32, f64);
840
841impl SqlAdd<PgInterval> for chrono::NaiveDateTime {
842 type Output = chrono::NaiveDateTime;
843}
844
845impl SqlSub<PgInterval> for chrono::NaiveDateTime {
846 type Output = chrono::NaiveDateTime;
847}
848
849impl SqlAdd<PgInterval> for chrono::DateTime<chrono::Utc> {
850 type Output = chrono::DateTime<chrono::Utc>;
851}
852
853impl SqlSub<PgInterval> for chrono::DateTime<chrono::Utc> {
854 type Output = chrono::DateTime<chrono::Utc>;
855}
856
857impl<Kind> Add<Expr<PgInterval, Kind>> for chrono::NaiveDateTime {
858 type Output = Expr<chrono::NaiveDateTime>;
859
860 fn add(self, rhs: Expr<PgInterval, Kind>) -> Self::Output {
861 arithmetic_expr(self.into_expr().node, BinaryOp::Add, rhs.node)
862 }
863}
864
865impl<Kind> Sub<Expr<PgInterval, Kind>> for chrono::NaiveDateTime {
866 type Output = Expr<chrono::NaiveDateTime>;
867
868 fn sub(self, rhs: Expr<PgInterval, Kind>) -> Self::Output {
869 arithmetic_expr(self.into_expr().node, BinaryOp::Sub, rhs.node)
870 }
871}
872
873impl<Kind> Add<Expr<PgInterval, Kind>> for chrono::DateTime<chrono::Utc> {
874 type Output = Expr<chrono::DateTime<chrono::Utc>>;
875
876 fn add(self, rhs: Expr<PgInterval, Kind>) -> Self::Output {
877 arithmetic_expr(self.into_expr().node, BinaryOp::Add, rhs.node)
878 }
879}
880
881impl<Kind> Sub<Expr<PgInterval, Kind>> for chrono::DateTime<chrono::Utc> {
882 type Output = Expr<chrono::DateTime<chrono::Utc>>;
883
884 fn sub(self, rhs: Expr<PgInterval, Kind>) -> Self::Output {
885 arithmetic_expr(self.into_expr().node, BinaryOp::Sub, rhs.node)
886 }
887}
888
889fn arithmetic_expr<Out>(left: ExprNode, op: BinaryOp, right: ExprNode) -> Expr<Out> {
890 Expr::new(ExprNode::Binary {
891 left: Box::new(left),
892 op,
893 right: Box::new(right),
894 })
895}
896
897impl<Lhs, RhsExpr, Kind> Add<RhsExpr> for Expr<Lhs, Kind>
898where
899 RhsExpr: ExprOperand,
900 Lhs: SqlAdd<RhsExpr::Value>,
901{
902 type Output = Expr<<Lhs as SqlAdd<RhsExpr::Value>>::Output>;
903
904 fn add(self, rhs: RhsExpr) -> Self::Output {
905 arithmetic_expr(self.node, BinaryOp::Add, rhs.into_operand_expr().node)
906 }
907}
908
909impl<Lhs, RhsExpr, Kind> Sub<RhsExpr> for Expr<Lhs, Kind>
910where
911 RhsExpr: ExprOperand,
912 Lhs: SqlSub<RhsExpr::Value>,
913{
914 type Output = Expr<<Lhs as SqlSub<RhsExpr::Value>>::Output>;
915
916 fn sub(self, rhs: RhsExpr) -> Self::Output {
917 arithmetic_expr(self.node, BinaryOp::Sub, rhs.into_operand_expr().node)
918 }
919}
920
921impl<Lhs, RhsExpr, Kind> Mul<RhsExpr> for Expr<Lhs, Kind>
922where
923 RhsExpr: ExprOperand,
924 Lhs: SqlMul<RhsExpr::Value>,
925{
926 type Output = Expr<<Lhs as SqlMul<RhsExpr::Value>>::Output>;
927
928 fn mul(self, rhs: RhsExpr) -> Self::Output {
929 arithmetic_expr(self.node, BinaryOp::Mul, rhs.into_operand_expr().node)
930 }
931}
932
933impl<M, Lhs, RhsExpr> Add<RhsExpr> for Column<M, Lhs>
934where
935 RhsExpr: ExprOperand,
936 Lhs: SqlAdd<RhsExpr::Value>,
937{
938 type Output = Expr<<Lhs as SqlAdd<RhsExpr::Value>>::Output>;
939
940 fn add(self, rhs: RhsExpr) -> Self::Output {
941 arithmetic_expr(ExprNode::Column(self.as_ref()), BinaryOp::Add, rhs.into_operand_expr().node)
942 }
943}
944
945impl<M, Lhs, RhsExpr> Sub<RhsExpr> for Column<M, Lhs>
946where
947 RhsExpr: ExprOperand,
948 Lhs: SqlSub<RhsExpr::Value>,
949{
950 type Output = Expr<<Lhs as SqlSub<RhsExpr::Value>>::Output>;
951
952 fn sub(self, rhs: RhsExpr) -> Self::Output {
953 arithmetic_expr(ExprNode::Column(self.as_ref()), BinaryOp::Sub, rhs.into_operand_expr().node)
954 }
955}
956
957impl<M, Lhs, RhsExpr> Mul<RhsExpr> for Column<M, Lhs>
958where
959 RhsExpr: ExprOperand,
960 Lhs: SqlMul<RhsExpr::Value>,
961{
962 type Output = Expr<<Lhs as SqlMul<RhsExpr::Value>>::Output>;
963
964 fn mul(self, rhs: RhsExpr) -> Self::Output {
965 arithmetic_expr(ExprNode::Column(self.as_ref()), BinaryOp::Mul, rhs.into_operand_expr().node)
966 }
967}
968
969impl<T, Kind> Expr<T, Kind>
970where
971 T: 'static,
972{
973 pub fn eq<V>(self, value: V) -> Expr<bool>
974 where
975 V: ColumnValue<T>,
976 {
977 match value.into_value() {
978 Some(Value::Null) => Expr::new(ExprNode::IsNull {
979 expr: Box::new(self.node),
980 negated: false,
981 }),
982 Some(value) => Expr::new(ExprNode::Binary {
983 left: Box::new(self.node),
984 op: BinaryOp::Eq,
985 right: Box::new(ExprNode::Value(value)),
986 }),
987 None => Expr::new(ExprNode::IsNull {
988 expr: Box::new(self.node),
989 negated: false,
990 }),
991 }
992 }
993
994 pub fn eq_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
995 Expr::new(ExprNode::Binary {
996 left: Box::new(self.node),
997 op: BinaryOp::Eq,
998 right: Box::new(ExprNode::Column(other.as_ref())),
999 })
1000 }
1001
1002 pub fn ne<V>(self, value: V) -> Expr<bool>
1003 where
1004 V: ColumnValue<T>,
1005 {
1006 match value.into_value() {
1007 Some(Value::Null) => Expr::new(ExprNode::IsNull {
1008 expr: Box::new(self.node),
1009 negated: true,
1010 }),
1011 Some(value) => Expr::new(ExprNode::Binary {
1012 left: Box::new(self.node),
1013 op: BinaryOp::Ne,
1014 right: Box::new(ExprNode::Value(value)),
1015 }),
1016 None => Expr::new(ExprNode::IsNull {
1017 expr: Box::new(self.node),
1018 negated: true,
1019 }),
1020 }
1021 }
1022
1023 pub fn ne_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
1024 Expr::new(ExprNode::Binary {
1025 left: Box::new(self.node),
1026 op: BinaryOp::Ne,
1027 right: Box::new(ExprNode::Column(other.as_ref())),
1028 })
1029 }
1030
1031 pub fn is_distinct_from_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
1032 Expr::new(ExprNode::Binary {
1033 left: Box::new(self.node),
1034 op: BinaryOp::IsDistinctFrom,
1035 right: Box::new(ExprNode::Column(other.as_ref())),
1036 })
1037 }
1038
1039 pub fn is_not_distinct_from_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
1040 Expr::new(ExprNode::Binary {
1041 left: Box::new(self.node),
1042 op: BinaryOp::IsNotDistinctFrom,
1043 right: Box::new(ExprNode::Column(other.as_ref())),
1044 })
1045 }
1046
1047 pub fn lt<V>(self, value: V) -> Expr<bool>
1048 where
1049 V: ColumnValue<T>,
1050 {
1051 Expr::new(ExprNode::Binary {
1052 left: Box::new(self.node),
1053 op: BinaryOp::Lt,
1054 right: Box::new(ExprNode::Value(value.into_value().unwrap_or(Value::Null))),
1055 })
1056 }
1057
1058 pub fn lt_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
1059 Expr::new(ExprNode::Binary {
1060 left: Box::new(self.node),
1061 op: BinaryOp::Lt,
1062 right: Box::new(ExprNode::Column(other.as_ref())),
1063 })
1064 }
1065
1066 pub fn le<V>(self, value: V) -> Expr<bool>
1067 where
1068 V: ColumnValue<T>,
1069 {
1070 Expr::new(ExprNode::Binary {
1071 left: Box::new(self.node),
1072 op: BinaryOp::Le,
1073 right: Box::new(ExprNode::Value(value.into_value().unwrap_or(Value::Null))),
1074 })
1075 }
1076
1077 pub fn le_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
1078 Expr::new(ExprNode::Binary {
1079 left: Box::new(self.node),
1080 op: BinaryOp::Le,
1081 right: Box::new(ExprNode::Column(other.as_ref())),
1082 })
1083 }
1084
1085 pub fn gt<V>(self, value: V) -> Expr<bool>
1086 where
1087 V: ColumnValue<T>,
1088 {
1089 Expr::new(ExprNode::Binary {
1090 left: Box::new(self.node),
1091 op: BinaryOp::Gt,
1092 right: Box::new(ExprNode::Value(value.into_value().unwrap_or(Value::Null))),
1093 })
1094 }
1095
1096 pub fn gt_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
1097 Expr::new(ExprNode::Binary {
1098 left: Box::new(self.node),
1099 op: BinaryOp::Gt,
1100 right: Box::new(ExprNode::Column(other.as_ref())),
1101 })
1102 }
1103
1104 pub fn ge<V>(self, value: V) -> Expr<bool>
1105 where
1106 V: ColumnValue<T>,
1107 {
1108 Expr::new(ExprNode::Binary {
1109 left: Box::new(self.node),
1110 op: BinaryOp::Ge,
1111 right: Box::new(ExprNode::Value(value.into_value().unwrap_or(Value::Null))),
1112 })
1113 }
1114
1115 pub fn ge_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
1116 Expr::new(ExprNode::Binary {
1117 left: Box::new(self.node),
1118 op: BinaryOp::Ge,
1119 right: Box::new(ExprNode::Column(other.as_ref())),
1120 })
1121 }
1122
1123 pub fn between<L, U>(self, low: L, high: U) -> Expr<bool>
1124 where
1125 L: ColumnValue<T>,
1126 U: ColumnValue<T>,
1127 {
1128 let low_value = low.into_value().unwrap_or(Value::Null);
1129 let high_value = high.into_value().unwrap_or(Value::Null);
1130 let node = self.node;
1131 let left = ExprNode::Binary {
1132 left: Box::new(node.clone()),
1133 op: BinaryOp::Ge,
1134 right: Box::new(ExprNode::Value(low_value)),
1135 };
1136 let right = ExprNode::Binary {
1137 left: Box::new(node),
1138 op: BinaryOp::Le,
1139 right: Box::new(ExprNode::Value(high_value)),
1140 };
1141 Expr::new(ExprNode::Bool {
1142 left: Box::new(left),
1143 op: BoolOp::And,
1144 right: Box::new(right),
1145 })
1146 }
1147
1148 pub fn like<V>(self, pattern: V) -> Expr<bool>
1149 where
1150 V: ColumnValue<T>,
1151 {
1152 Expr::new(ExprNode::Like {
1153 expr: Box::new(self.node),
1154 pattern: pattern.into_value().unwrap_or(Value::Null),
1155 case_insensitive: false,
1156 })
1157 }
1158
1159 pub fn ilike<V>(self, pattern: V) -> Expr<bool>
1160 where
1161 V: ColumnValue<T>,
1162 {
1163 Expr::new(ExprNode::Like {
1164 expr: Box::new(self.node),
1165 pattern: pattern.into_value().unwrap_or(Value::Null),
1166 case_insensitive: true,
1167 })
1168 }
1169
1170 pub fn is_null(self) -> Expr<bool> {
1171 Expr::new(ExprNode::IsNull {
1172 expr: Box::new(self.node),
1173 negated: false,
1174 })
1175 }
1176
1177 pub fn is_not_null(self) -> Expr<bool> {
1178 Expr::new(ExprNode::IsNull {
1179 expr: Box::new(self.node),
1180 negated: true,
1181 })
1182 }
1183
1184 pub fn in_<I, V>(self, values: I) -> Expr<bool>
1185 where
1186 I: IntoIterator<Item = V>,
1187 V: ColumnValue<T>,
1188 {
1189 let mut binds = Vec::new();
1190 for value in values {
1191 if let Some(value) = value.into_value() {
1192 binds.push(value);
1193 }
1194 }
1195 Expr::new(ExprNode::In {
1196 expr: Box::new(self.node),
1197 values: binds,
1198 })
1199 }
1200}
1201
1202impl<Kind> Expr<bool, Kind> {
1203 pub fn and(self, other: Expr<bool>) -> Expr<bool> {
1204 Expr::new(ExprNode::Bool {
1205 left: Box::new(self.node),
1206 op: BoolOp::And,
1207 right: Box::new(other.node),
1208 })
1209 }
1210
1211 pub fn or(self, other: Expr<bool>) -> Expr<bool> {
1212 Expr::new(ExprNode::Bool {
1213 left: Box::new(self.node),
1214 op: BoolOp::Or,
1215 right: Box::new(other.node),
1216 })
1217 }
1218
1219 pub fn not(self) -> Expr<bool> {
1220 Expr::new(ExprNode::Unary {
1221 op: UnaryOp::Not,
1222 expr: Box::new(self.node),
1223 })
1224 }
1225}
1226
1227impl<M, T> Column<M, T>
1228where
1229 T: 'static,
1230{
1231 pub fn eq<V>(self, value: V) -> Expr<bool>
1232 where
1233 V: ColumnValue<T>,
1234 {
1235 match value.into_value() {
1236 Some(Value::Null) => Expr::new(ExprNode::IsNull {
1237 expr: Box::new(ExprNode::Column(self.as_ref())),
1238 negated: false,
1239 }),
1240 Some(value) => Expr::new(ExprNode::Binary {
1241 left: Box::new(ExprNode::Column(self.as_ref())),
1242 op: BinaryOp::Eq,
1243 right: Box::new(ExprNode::Value(value)),
1244 }),
1245 None => Expr::new(ExprNode::IsNull {
1246 expr: Box::new(ExprNode::Column(self.as_ref())),
1247 negated: false,
1248 }),
1249 }
1250 }
1251
1252 pub fn eq_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
1253 Expr::new(ExprNode::Binary {
1254 left: Box::new(ExprNode::Column(self.as_ref())),
1255 op: BinaryOp::Eq,
1256 right: Box::new(ExprNode::Column(other.as_ref())),
1257 })
1258 }
1259
1260 pub fn ne_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
1261 Expr::new(ExprNode::Binary {
1262 left: Box::new(ExprNode::Column(self.as_ref())),
1263 op: BinaryOp::Ne,
1264 right: Box::new(ExprNode::Column(other.as_ref())),
1265 })
1266 }
1267
1268 pub fn is_distinct_from_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
1269 Expr::new(ExprNode::Binary {
1270 left: Box::new(ExprNode::Column(self.as_ref())),
1271 op: BinaryOp::IsDistinctFrom,
1272 right: Box::new(ExprNode::Column(other.as_ref())),
1273 })
1274 }
1275
1276 pub fn is_not_distinct_from_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
1277 Expr::new(ExprNode::Binary {
1278 left: Box::new(ExprNode::Column(self.as_ref())),
1279 op: BinaryOp::IsNotDistinctFrom,
1280 right: Box::new(ExprNode::Column(other.as_ref())),
1281 })
1282 }
1283
1284 pub fn ne<V>(self, value: V) -> Expr<bool>
1285 where
1286 V: ColumnValue<T>,
1287 {
1288 match value.into_value() {
1289 Some(Value::Null) => Expr::new(ExprNode::IsNull {
1290 expr: Box::new(ExprNode::Column(self.as_ref())),
1291 negated: true,
1292 }),
1293 Some(value) => Expr::new(ExprNode::Binary {
1294 left: Box::new(ExprNode::Column(self.as_ref())),
1295 op: BinaryOp::Ne,
1296 right: Box::new(ExprNode::Value(value)),
1297 }),
1298 None => Expr::new(ExprNode::IsNull {
1299 expr: Box::new(ExprNode::Column(self.as_ref())),
1300 negated: true,
1301 }),
1302 }
1303 }
1304
1305 pub fn lt<V, Marker>(self, value: V) -> Expr<bool>
1306 where
1307 V: ComparisonValue<T, Marker>,
1308 {
1309 Expr::new(ExprNode::Binary {
1310 left: Box::new(ExprNode::Column(self.as_ref())),
1311 op: BinaryOp::Lt,
1312 right: Box::new(value.into_comparison_expr().node),
1313 })
1314 }
1315
1316 pub fn lt_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
1317 Expr::new(ExprNode::Binary {
1318 left: Box::new(ExprNode::Column(self.as_ref())),
1319 op: BinaryOp::Lt,
1320 right: Box::new(ExprNode::Column(other.as_ref())),
1321 })
1322 }
1323
1324 pub fn le<V, Marker>(self, value: V) -> Expr<bool>
1325 where
1326 V: ComparisonValue<T, Marker>,
1327 {
1328 Expr::new(ExprNode::Binary {
1329 left: Box::new(ExprNode::Column(self.as_ref())),
1330 op: BinaryOp::Le,
1331 right: Box::new(value.into_comparison_expr().node),
1332 })
1333 }
1334
1335 pub fn le_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
1336 Expr::new(ExprNode::Binary {
1337 left: Box::new(ExprNode::Column(self.as_ref())),
1338 op: BinaryOp::Le,
1339 right: Box::new(ExprNode::Column(other.as_ref())),
1340 })
1341 }
1342
1343 pub fn gt<V, Marker>(self, value: V) -> Expr<bool>
1344 where
1345 V: ComparisonValue<T, Marker>,
1346 {
1347 Expr::new(ExprNode::Binary {
1348 left: Box::new(ExprNode::Column(self.as_ref())),
1349 op: BinaryOp::Gt,
1350 right: Box::new(value.into_comparison_expr().node),
1351 })
1352 }
1353
1354 pub fn gt_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
1355 Expr::new(ExprNode::Binary {
1356 left: Box::new(ExprNode::Column(self.as_ref())),
1357 op: BinaryOp::Gt,
1358 right: Box::new(ExprNode::Column(other.as_ref())),
1359 })
1360 }
1361
1362 pub fn ge<V, Marker>(self, value: V) -> Expr<bool>
1363 where
1364 V: ComparisonValue<T, Marker>,
1365 {
1366 Expr::new(ExprNode::Binary {
1367 left: Box::new(ExprNode::Column(self.as_ref())),
1368 op: BinaryOp::Ge,
1369 right: Box::new(value.into_comparison_expr().node),
1370 })
1371 }
1372
1373 pub fn ge_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
1374 Expr::new(ExprNode::Binary {
1375 left: Box::new(ExprNode::Column(self.as_ref())),
1376 op: BinaryOp::Ge,
1377 right: Box::new(ExprNode::Column(other.as_ref())),
1378 })
1379 }
1380
1381 pub fn between<L, U>(self, low: L, high: U) -> Expr<bool>
1382 where
1383 L: Into<Value>,
1384 U: Into<Value>,
1385 {
1386 let left = ExprNode::Binary {
1387 left: Box::new(ExprNode::Column(self.as_ref())),
1388 op: BinaryOp::Ge,
1389 right: Box::new(ExprNode::Value(low.into())),
1390 };
1391 let right = ExprNode::Binary {
1392 left: Box::new(ExprNode::Column(self.as_ref())),
1393 op: BinaryOp::Le,
1394 right: Box::new(ExprNode::Value(high.into())),
1395 };
1396 Expr::new(ExprNode::Bool {
1397 left: Box::new(left),
1398 op: BoolOp::And,
1399 right: Box::new(right),
1400 })
1401 }
1402
1403 pub fn like<V>(self, pattern: V) -> Expr<bool>
1404 where
1405 V: Into<Value>,
1406 {
1407 Expr::new(ExprNode::Like {
1408 expr: Box::new(ExprNode::Column(self.as_ref())),
1409 pattern: pattern.into(),
1410 case_insensitive: false,
1411 })
1412 }
1413
1414 pub fn ilike<V>(self, pattern: V) -> Expr<bool>
1415 where
1416 V: Into<Value>,
1417 {
1418 Expr::new(ExprNode::Like {
1419 expr: Box::new(ExprNode::Column(self.as_ref())),
1420 pattern: pattern.into(),
1421 case_insensitive: true,
1422 })
1423 }
1424
1425 pub fn in_<I, V>(self, values: I) -> Expr<bool>
1426 where
1427 I: IntoIterator<Item = V>,
1428 V: Into<Value>,
1429 {
1430 Expr::new(ExprNode::In {
1431 expr: Box::new(ExprNode::Column(self.as_ref())),
1432 values: values.into_iter().map(Into::into).collect(),
1433 })
1434 }
1435
1436 pub fn is_null(self) -> Expr<bool> {
1437 Expr::new(ExprNode::IsNull {
1438 expr: Box::new(ExprNode::Column(self.as_ref())),
1439 negated: false,
1440 })
1441 }
1442
1443 pub fn is_not_null(self) -> Expr<bool> {
1444 Expr::new(ExprNode::IsNull {
1445 expr: Box::new(ExprNode::Column(self.as_ref())),
1446 negated: true,
1447 })
1448 }
1449}
1450
1451#[derive(Debug, Clone, Copy)]
1452pub enum ConditionKind {
1453 Any,
1454 All,
1455}
1456
1457#[derive(Debug, Clone)]
1458pub struct Condition {
1459 kind: ConditionKind,
1460 exprs: Vec<Expr<bool>>,
1461}
1462
1463impl Condition {
1464 pub fn any() -> Self {
1465 Self {
1466 kind: ConditionKind::Any,
1467 exprs: Vec::new(),
1468 }
1469 }
1470
1471 pub fn all() -> Self {
1472 Self {
1473 kind: ConditionKind::All,
1474 exprs: Vec::new(),
1475 }
1476 }
1477
1478 pub fn add(mut self, expr: Expr<bool>) -> Self {
1479 self.exprs.push(expr);
1480 self
1481 }
1482
1483 pub fn into_expr(self) -> Option<Expr<bool>> {
1484 let mut iter = self.exprs.into_iter();
1485 let first = iter.next()?;
1486 Some(iter.fold(first, |acc, expr| match self.kind {
1487 ConditionKind::Any => acc.or(expr),
1488 ConditionKind::All => acc.and(expr),
1489 }))
1490 }
1491}