1use std::marker::PhantomData;
2use std::ops::{Add, Sub};
3
4use crate::schema::{Column, ColumnRef};
5use crate::types::{PgInterval, PgVector};
6
7#[derive(Debug, Clone, PartialEq)]
8pub enum Value {
9 Null,
10 Bool(bool),
11 I16(i16),
12 I32(i32),
13 I64(i64),
14 F32(f32),
15 F64(f64),
16 String(String),
17 Array(Vec<String>),
18 Json(serde_json::Value),
19 Uuid(uuid::Uuid),
20 DateTime(chrono::NaiveDateTime),
21 DateTimeUtc(chrono::DateTime<chrono::Utc>),
22 Date(chrono::NaiveDate),
23 Time(chrono::NaiveTime),
24 Interval(PgInterval),
25 Vector(Vec<f32>),
26 Enum { type_name: &'static str, value: String },
27}
28
29pub trait ColumnValue<T> {
30 fn into_value(self) -> Option<Value>;
31}
32
33impl<T> ColumnValue<T> for T
34where
35 T: Into<Value>,
36{
37 fn into_value(self) -> Option<Value> {
38 Some(self.into())
39 }
40}
41
42impl<T> ColumnValue<T> for Option<T>
43where
44 T: Into<Value>,
45{
46 fn into_value(self) -> Option<Value> {
47 self.map(Into::into)
48 }
49}
50
51impl ColumnValue<String> for &str {
52 fn into_value(self) -> Option<Value> {
53 Some(Value::String(self.to_string()))
54 }
55}
56
57impl<T> ColumnValue<T> for &T
58where
59 T: Clone + Into<Value>,
60{
61 fn into_value(self) -> Option<Value> {
62 Some(self.clone().into())
63 }
64}
65
66impl From<bool> for Value {
67 fn from(value: bool) -> Self {
68 Self::Bool(value)
69 }
70}
71
72impl From<i16> for Value {
73 fn from(value: i16) -> Self {
74 Self::I16(value)
75 }
76}
77
78impl From<i32> for Value {
79 fn from(value: i32) -> Self {
80 Self::I32(value)
81 }
82}
83
84impl From<i64> for Value {
85 fn from(value: i64) -> Self {
86 Self::I64(value)
87 }
88}
89
90impl From<f32> for Value {
91 fn from(value: f32) -> Self {
92 Self::F32(value)
93 }
94}
95
96impl From<f64> for Value {
97 fn from(value: f64) -> Self {
98 Self::F64(value)
99 }
100}
101
102impl From<String> for Value {
103 fn from(value: String) -> Self {
104 Self::String(value)
105 }
106}
107
108impl From<&str> for Value {
109 fn from(value: &str) -> Self {
110 Self::String(value.to_string())
111 }
112}
113
114impl From<Vec<String>> for Value {
115 fn from(value: Vec<String>) -> Self {
116 Self::Array(value)
117 }
118}
119
120impl From<serde_json::Value> for Value {
121 fn from(value: serde_json::Value) -> Self {
122 Self::Json(value)
123 }
124}
125
126impl From<uuid::Uuid> for Value {
127 fn from(value: uuid::Uuid) -> Self {
128 Self::Uuid(value)
129 }
130}
131
132impl From<chrono::NaiveDateTime> for Value {
133 fn from(value: chrono::NaiveDateTime) -> Self {
134 Self::DateTime(value)
135 }
136}
137
138impl From<chrono::DateTime<chrono::Utc>> for Value {
139 fn from(value: chrono::DateTime<chrono::Utc>) -> Self {
140 Self::DateTimeUtc(value)
141 }
142}
143
144impl From<chrono::NaiveDate> for Value {
145 fn from(value: chrono::NaiveDate) -> Self {
146 Self::Date(value)
147 }
148}
149
150impl From<chrono::NaiveTime> for Value {
151 fn from(value: chrono::NaiveTime) -> Self {
152 Self::Time(value)
153 }
154}
155
156impl From<PgInterval> for Value {
157 fn from(value: PgInterval) -> Self {
158 Self::Interval(value)
159 }
160}
161
162impl<const N: usize> From<PgVector<N>> for Value {
163 fn from(value: PgVector<N>) -> Self {
164 Self::Vector(value.to_vec())
165 }
166}
167
168impl<T> From<Option<T>> for Value
169where
170 T: Into<Value>,
171{
172 fn from(value: Option<T>) -> Self {
173 match value {
174 Some(v) => v.into(),
175 None => Self::Null,
176 }
177 }
178}
179
180#[derive(Debug, Clone, Copy)]
181pub enum BinaryOp {
182 Add,
183 Sub,
184 Eq,
185 Ne,
186 IsDistinctFrom,
187 IsNotDistinctFrom,
188 Lt,
189 Le,
190 Gt,
191 Ge,
192}
193
194#[derive(Debug, Clone, Copy)]
195pub enum BoolOp {
196 And,
197 Or,
198}
199
200#[derive(Debug, Clone, Copy)]
201pub enum UnaryOp {
202 Not,
203}
204
205#[derive(Debug, Clone, Copy)]
206pub enum VectorBinaryOp {
207 L2Distance,
208 CosineDistance,
209 InnerProductDistance,
210 L1Distance,
211}
212
213#[derive(Debug, Clone, Copy)]
214pub enum IntervalField {
215 Days,
216 Hours,
217 Minutes,
218 Seconds,
219}
220
221#[derive(Debug, Clone)]
222pub enum ExprNode {
223 Column(ColumnRef),
224 Value(Value),
225 Func {
226 name: &'static str,
227 args: Vec<ExprNode>,
228 },
229 VectorBinary {
230 left: Box<ExprNode>,
231 op: VectorBinaryOp,
232 right: Box<ExprNode>,
233 },
234 MakeInterval {
235 field: IntervalField,
236 value: Box<ExprNode>,
237 },
238 Binary {
239 left: Box<ExprNode>,
240 op: BinaryOp,
241 right: Box<ExprNode>,
242 },
243 Bool {
244 left: Box<ExprNode>,
245 op: BoolOp,
246 right: Box<ExprNode>,
247 },
248 Unary {
249 op: UnaryOp,
250 expr: Box<ExprNode>,
251 },
252 In {
253 expr: Box<ExprNode>,
254 values: Vec<Value>,
255 },
256 IsNull {
257 expr: Box<ExprNode>,
258 negated: bool,
259 },
260 Like {
261 expr: Box<ExprNode>,
262 pattern: Value,
263 case_insensitive: bool,
264 },
265}
266
267#[derive(Debug, Clone)]
268pub struct Expr<T> {
269 pub node: ExprNode,
270 _marker: PhantomData<T>,
271}
272
273impl<T> Expr<T> {
274 pub fn new(node: ExprNode) -> Self {
275 Self {
276 node,
277 _marker: PhantomData,
278 }
279 }
280}
281
282pub trait IntoExpr<T> {
283 fn into_expr(self) -> Expr<T>;
284}
285
286pub trait ExprOperand {
287 type Value;
288
289 fn into_operand_expr(self) -> Expr<Self::Value>;
290}
291
292pub trait SqlAdd<Rhs> {
293 type Output;
294}
295
296pub trait SqlSub<Rhs> {
297 type Output;
298}
299
300impl<T> IntoExpr<T> for Expr<T> {
301 fn into_expr(self) -> Expr<T> {
302 self
303 }
304}
305
306impl<T> ExprOperand for Expr<T> {
307 type Value = T;
308
309 fn into_operand_expr(self) -> Expr<Self::Value> {
310 self
311 }
312}
313
314impl<M, T> IntoExpr<T> for Column<M, T> {
315 fn into_expr(self) -> Expr<T> {
316 Expr::new(ExprNode::Column(self.as_ref()))
317 }
318}
319
320impl<M, T> ExprOperand for Column<M, T> {
321 type Value = T;
322
323 fn into_operand_expr(self) -> Expr<Self::Value> {
324 self.into_expr()
325 }
326}
327
328impl IntoExpr<String> for String {
329 fn into_expr(self) -> Expr<String> {
330 Expr::new(ExprNode::Value(Value::String(self)))
331 }
332}
333
334impl ExprOperand for String {
335 type Value = String;
336
337 fn into_operand_expr(self) -> Expr<Self::Value> {
338 self.into_expr()
339 }
340}
341
342impl IntoExpr<String> for &str {
343 fn into_expr(self) -> Expr<String> {
344 Expr::new(ExprNode::Value(Value::String(self.to_string())))
345 }
346}
347
348impl ExprOperand for &str {
349 type Value = String;
350
351 fn into_operand_expr(self) -> Expr<Self::Value> {
352 self.into_expr()
353 }
354}
355
356impl IntoExpr<bool> for bool {
357 fn into_expr(self) -> Expr<bool> {
358 Expr::new(ExprNode::Value(Value::Bool(self)))
359 }
360}
361
362impl ExprOperand for bool {
363 type Value = bool;
364
365 fn into_operand_expr(self) -> Expr<Self::Value> {
366 self.into_expr()
367 }
368}
369
370impl IntoExpr<i16> for i16 {
371 fn into_expr(self) -> Expr<i16> {
372 Expr::new(ExprNode::Value(Value::I16(self)))
373 }
374}
375
376impl ExprOperand for i16 {
377 type Value = i16;
378
379 fn into_operand_expr(self) -> Expr<Self::Value> {
380 self.into_expr()
381 }
382}
383
384impl IntoExpr<i32> for i32 {
385 fn into_expr(self) -> Expr<i32> {
386 Expr::new(ExprNode::Value(Value::I32(self)))
387 }
388}
389
390impl ExprOperand for i32 {
391 type Value = i32;
392
393 fn into_operand_expr(self) -> Expr<Self::Value> {
394 self.into_expr()
395 }
396}
397
398impl IntoExpr<i64> for i64 {
399 fn into_expr(self) -> Expr<i64> {
400 Expr::new(ExprNode::Value(Value::I64(self)))
401 }
402}
403
404impl ExprOperand for i64 {
405 type Value = i64;
406
407 fn into_operand_expr(self) -> Expr<Self::Value> {
408 self.into_expr()
409 }
410}
411
412impl IntoExpr<f32> for f32 {
413 fn into_expr(self) -> Expr<f32> {
414 Expr::new(ExprNode::Value(Value::F32(self)))
415 }
416}
417
418impl ExprOperand for f32 {
419 type Value = f32;
420
421 fn into_operand_expr(self) -> Expr<Self::Value> {
422 self.into_expr()
423 }
424}
425
426impl IntoExpr<f64> for f64 {
427 fn into_expr(self) -> Expr<f64> {
428 Expr::new(ExprNode::Value(Value::F64(self)))
429 }
430}
431
432impl ExprOperand for f64 {
433 type Value = f64;
434
435 fn into_operand_expr(self) -> Expr<Self::Value> {
436 self.into_expr()
437 }
438}
439
440impl IntoExpr<uuid::Uuid> for uuid::Uuid {
441 fn into_expr(self) -> Expr<uuid::Uuid> {
442 Expr::new(ExprNode::Value(Value::Uuid(self)))
443 }
444}
445
446impl ExprOperand for uuid::Uuid {
447 type Value = uuid::Uuid;
448
449 fn into_operand_expr(self) -> Expr<Self::Value> {
450 self.into_expr()
451 }
452}
453
454impl IntoExpr<chrono::NaiveDateTime> for chrono::NaiveDateTime {
455 fn into_expr(self) -> Expr<chrono::NaiveDateTime> {
456 Expr::new(ExprNode::Value(Value::DateTime(self)))
457 }
458}
459
460impl ExprOperand for chrono::NaiveDateTime {
461 type Value = chrono::NaiveDateTime;
462
463 fn into_operand_expr(self) -> Expr<Self::Value> {
464 self.into_expr()
465 }
466}
467
468impl IntoExpr<chrono::DateTime<chrono::Utc>> for chrono::DateTime<chrono::Utc> {
469 fn into_expr(self) -> Expr<chrono::DateTime<chrono::Utc>> {
470 Expr::new(ExprNode::Value(Value::DateTimeUtc(self)))
471 }
472}
473
474impl ExprOperand for chrono::DateTime<chrono::Utc> {
475 type Value = chrono::DateTime<chrono::Utc>;
476
477 fn into_operand_expr(self) -> Expr<Self::Value> {
478 self.into_expr()
479 }
480}
481
482impl IntoExpr<chrono::NaiveDate> for chrono::NaiveDate {
483 fn into_expr(self) -> Expr<chrono::NaiveDate> {
484 Expr::new(ExprNode::Value(Value::Date(self)))
485 }
486}
487
488impl ExprOperand for chrono::NaiveDate {
489 type Value = chrono::NaiveDate;
490
491 fn into_operand_expr(self) -> Expr<Self::Value> {
492 self.into_expr()
493 }
494}
495
496impl IntoExpr<chrono::NaiveTime> for chrono::NaiveTime {
497 fn into_expr(self) -> Expr<chrono::NaiveTime> {
498 Expr::new(ExprNode::Value(Value::Time(self)))
499 }
500}
501
502impl ExprOperand for chrono::NaiveTime {
503 type Value = chrono::NaiveTime;
504
505 fn into_operand_expr(self) -> Expr<Self::Value> {
506 self.into_expr()
507 }
508}
509
510impl IntoExpr<PgInterval> for PgInterval {
511 fn into_expr(self) -> Expr<PgInterval> {
512 Expr::new(ExprNode::Value(Value::Interval(self)))
513 }
514}
515
516impl ExprOperand for PgInterval {
517 type Value = PgInterval;
518
519 fn into_operand_expr(self) -> Expr<Self::Value> {
520 self.into_expr()
521 }
522}
523
524impl IntoExpr<Vec<String>> for Vec<String> {
525 fn into_expr(self) -> Expr<Vec<String>> {
526 Expr::new(ExprNode::Value(Value::Array(self)))
527 }
528}
529
530impl ExprOperand for Vec<String> {
531 type Value = Vec<String>;
532
533 fn into_operand_expr(self) -> Expr<Self::Value> {
534 self.into_expr()
535 }
536}
537
538impl IntoExpr<serde_json::Value> for serde_json::Value {
539 fn into_expr(self) -> Expr<serde_json::Value> {
540 Expr::new(ExprNode::Value(Value::Json(self)))
541 }
542}
543
544impl ExprOperand for serde_json::Value {
545 type Value = serde_json::Value;
546
547 fn into_operand_expr(self) -> Expr<Self::Value> {
548 self.into_expr()
549 }
550}
551
552impl<const N: usize> IntoExpr<PgVector<N>> for PgVector<N> {
553 fn into_expr(self) -> Expr<PgVector<N>> {
554 Expr::new(ExprNode::Value(Value::from(self)))
555 }
556}
557
558impl<const N: usize> ExprOperand for PgVector<N> {
559 type Value = PgVector<N>;
560
561 fn into_operand_expr(self) -> Expr<Self::Value> {
562 self.into_expr()
563 }
564}
565
566macro_rules! impl_numeric_arithmetic {
567 ($($ty:ty),* $(,)?) => {
568 $(
569 impl SqlAdd<$ty> for $ty {
570 type Output = $ty;
571 }
572
573 impl SqlSub<$ty> for $ty {
574 type Output = $ty;
575 }
576 )*
577 };
578}
579
580impl SqlAdd<i16> for i16 {
581 type Output = i32;
582}
583
584impl SqlSub<i16> for i16 {
585 type Output = i32;
586}
587
588impl_numeric_arithmetic!(i32, i64, f32, f64);
589
590impl SqlAdd<PgInterval> for chrono::NaiveDateTime {
591 type Output = chrono::NaiveDateTime;
592}
593
594impl SqlSub<PgInterval> for chrono::NaiveDateTime {
595 type Output = chrono::NaiveDateTime;
596}
597
598impl SqlAdd<PgInterval> for chrono::DateTime<chrono::Utc> {
599 type Output = chrono::DateTime<chrono::Utc>;
600}
601
602impl SqlSub<PgInterval> for chrono::DateTime<chrono::Utc> {
603 type Output = chrono::DateTime<chrono::Utc>;
604}
605
606fn arithmetic_expr<Out>(left: ExprNode, op: BinaryOp, right: ExprNode) -> Expr<Out> {
607 Expr::new(ExprNode::Binary {
608 left: Box::new(left),
609 op,
610 right: Box::new(right),
611 })
612}
613
614impl<Lhs, RhsExpr> Add<RhsExpr> for Expr<Lhs>
615where
616 RhsExpr: ExprOperand,
617 Lhs: SqlAdd<RhsExpr::Value>,
618{
619 type Output = Expr<<Lhs as SqlAdd<RhsExpr::Value>>::Output>;
620
621 fn add(self, rhs: RhsExpr) -> Self::Output {
622 arithmetic_expr(self.node, BinaryOp::Add, rhs.into_operand_expr().node)
623 }
624}
625
626impl<Lhs, RhsExpr> Sub<RhsExpr> for Expr<Lhs>
627where
628 RhsExpr: ExprOperand,
629 Lhs: SqlSub<RhsExpr::Value>,
630{
631 type Output = Expr<<Lhs as SqlSub<RhsExpr::Value>>::Output>;
632
633 fn sub(self, rhs: RhsExpr) -> Self::Output {
634 arithmetic_expr(self.node, BinaryOp::Sub, rhs.into_operand_expr().node)
635 }
636}
637
638impl<M, Lhs, RhsExpr> Add<RhsExpr> for Column<M, Lhs>
639where
640 RhsExpr: ExprOperand,
641 Lhs: SqlAdd<RhsExpr::Value>,
642{
643 type Output = Expr<<Lhs as SqlAdd<RhsExpr::Value>>::Output>;
644
645 fn add(self, rhs: RhsExpr) -> Self::Output {
646 arithmetic_expr(ExprNode::Column(self.as_ref()), BinaryOp::Add, rhs.into_operand_expr().node)
647 }
648}
649
650impl<M, Lhs, RhsExpr> Sub<RhsExpr> for Column<M, Lhs>
651where
652 RhsExpr: ExprOperand,
653 Lhs: SqlSub<RhsExpr::Value>,
654{
655 type Output = Expr<<Lhs as SqlSub<RhsExpr::Value>>::Output>;
656
657 fn sub(self, rhs: RhsExpr) -> Self::Output {
658 arithmetic_expr(ExprNode::Column(self.as_ref()), BinaryOp::Sub, rhs.into_operand_expr().node)
659 }
660}
661
662impl<T> Expr<T>
663where
664 T: 'static,
665{
666 pub fn eq<V>(self, value: V) -> Expr<bool>
667 where
668 V: ColumnValue<T>,
669 {
670 match value.into_value() {
671 Some(Value::Null) => Expr::new(ExprNode::IsNull {
672 expr: Box::new(self.node),
673 negated: false,
674 }),
675 Some(value) => Expr::new(ExprNode::Binary {
676 left: Box::new(self.node),
677 op: BinaryOp::Eq,
678 right: Box::new(ExprNode::Value(value)),
679 }),
680 None => Expr::new(ExprNode::IsNull {
681 expr: Box::new(self.node),
682 negated: false,
683 }),
684 }
685 }
686
687 pub fn eq_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
688 Expr::new(ExprNode::Binary {
689 left: Box::new(self.node),
690 op: BinaryOp::Eq,
691 right: Box::new(ExprNode::Column(other.as_ref())),
692 })
693 }
694
695 pub fn ne<V>(self, value: V) -> Expr<bool>
696 where
697 V: ColumnValue<T>,
698 {
699 match value.into_value() {
700 Some(Value::Null) => Expr::new(ExprNode::IsNull {
701 expr: Box::new(self.node),
702 negated: true,
703 }),
704 Some(value) => Expr::new(ExprNode::Binary {
705 left: Box::new(self.node),
706 op: BinaryOp::Ne,
707 right: Box::new(ExprNode::Value(value)),
708 }),
709 None => Expr::new(ExprNode::IsNull {
710 expr: Box::new(self.node),
711 negated: true,
712 }),
713 }
714 }
715
716 pub fn ne_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
717 Expr::new(ExprNode::Binary {
718 left: Box::new(self.node),
719 op: BinaryOp::Ne,
720 right: Box::new(ExprNode::Column(other.as_ref())),
721 })
722 }
723
724 pub fn is_distinct_from_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
725 Expr::new(ExprNode::Binary {
726 left: Box::new(self.node),
727 op: BinaryOp::IsDistinctFrom,
728 right: Box::new(ExprNode::Column(other.as_ref())),
729 })
730 }
731
732 pub fn is_not_distinct_from_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
733 Expr::new(ExprNode::Binary {
734 left: Box::new(self.node),
735 op: BinaryOp::IsNotDistinctFrom,
736 right: Box::new(ExprNode::Column(other.as_ref())),
737 })
738 }
739
740 pub fn lt<V>(self, value: V) -> Expr<bool>
741 where
742 V: ColumnValue<T>,
743 {
744 Expr::new(ExprNode::Binary {
745 left: Box::new(self.node),
746 op: BinaryOp::Lt,
747 right: Box::new(ExprNode::Value(value.into_value().unwrap_or(Value::Null))),
748 })
749 }
750
751 pub fn lt_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
752 Expr::new(ExprNode::Binary {
753 left: Box::new(self.node),
754 op: BinaryOp::Lt,
755 right: Box::new(ExprNode::Column(other.as_ref())),
756 })
757 }
758
759 pub fn le<V>(self, value: V) -> Expr<bool>
760 where
761 V: ColumnValue<T>,
762 {
763 Expr::new(ExprNode::Binary {
764 left: Box::new(self.node),
765 op: BinaryOp::Le,
766 right: Box::new(ExprNode::Value(value.into_value().unwrap_or(Value::Null))),
767 })
768 }
769
770 pub fn le_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
771 Expr::new(ExprNode::Binary {
772 left: Box::new(self.node),
773 op: BinaryOp::Le,
774 right: Box::new(ExprNode::Column(other.as_ref())),
775 })
776 }
777
778 pub fn gt<V>(self, value: V) -> Expr<bool>
779 where
780 V: ColumnValue<T>,
781 {
782 Expr::new(ExprNode::Binary {
783 left: Box::new(self.node),
784 op: BinaryOp::Gt,
785 right: Box::new(ExprNode::Value(value.into_value().unwrap_or(Value::Null))),
786 })
787 }
788
789 pub fn gt_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
790 Expr::new(ExprNode::Binary {
791 left: Box::new(self.node),
792 op: BinaryOp::Gt,
793 right: Box::new(ExprNode::Column(other.as_ref())),
794 })
795 }
796
797 pub fn ge<V>(self, value: V) -> Expr<bool>
798 where
799 V: ColumnValue<T>,
800 {
801 Expr::new(ExprNode::Binary {
802 left: Box::new(self.node),
803 op: BinaryOp::Ge,
804 right: Box::new(ExprNode::Value(value.into_value().unwrap_or(Value::Null))),
805 })
806 }
807
808 pub fn ge_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
809 Expr::new(ExprNode::Binary {
810 left: Box::new(self.node),
811 op: BinaryOp::Ge,
812 right: Box::new(ExprNode::Column(other.as_ref())),
813 })
814 }
815
816 pub fn between<L, U>(self, low: L, high: U) -> Expr<bool>
817 where
818 L: ColumnValue<T>,
819 U: ColumnValue<T>,
820 {
821 let low_value = low.into_value().unwrap_or(Value::Null);
822 let high_value = high.into_value().unwrap_or(Value::Null);
823 let node = self.node;
824 let left = ExprNode::Binary {
825 left: Box::new(node.clone()),
826 op: BinaryOp::Ge,
827 right: Box::new(ExprNode::Value(low_value)),
828 };
829 let right = ExprNode::Binary {
830 left: Box::new(node),
831 op: BinaryOp::Le,
832 right: Box::new(ExprNode::Value(high_value)),
833 };
834 Expr::new(ExprNode::Bool {
835 left: Box::new(left),
836 op: BoolOp::And,
837 right: Box::new(right),
838 })
839 }
840
841 pub fn like<V>(self, pattern: V) -> Expr<bool>
842 where
843 V: ColumnValue<T>,
844 {
845 Expr::new(ExprNode::Like {
846 expr: Box::new(self.node),
847 pattern: pattern.into_value().unwrap_or(Value::Null),
848 case_insensitive: false,
849 })
850 }
851
852 pub fn ilike<V>(self, pattern: V) -> Expr<bool>
853 where
854 V: ColumnValue<T>,
855 {
856 Expr::new(ExprNode::Like {
857 expr: Box::new(self.node),
858 pattern: pattern.into_value().unwrap_or(Value::Null),
859 case_insensitive: true,
860 })
861 }
862
863 pub fn is_null(self) -> Expr<bool> {
864 Expr::new(ExprNode::IsNull {
865 expr: Box::new(self.node),
866 negated: false,
867 })
868 }
869
870 pub fn is_not_null(self) -> Expr<bool> {
871 Expr::new(ExprNode::IsNull {
872 expr: Box::new(self.node),
873 negated: true,
874 })
875 }
876
877 pub fn in_<I, V>(self, values: I) -> Expr<bool>
878 where
879 I: IntoIterator<Item = V>,
880 V: ColumnValue<T>,
881 {
882 let mut binds = Vec::new();
883 for value in values {
884 if let Some(value) = value.into_value() {
885 binds.push(value);
886 }
887 }
888 Expr::new(ExprNode::In {
889 expr: Box::new(self.node),
890 values: binds,
891 })
892 }
893}
894
895impl Expr<bool> {
896 pub fn and(self, other: Expr<bool>) -> Expr<bool> {
897 Expr::new(ExprNode::Bool {
898 left: Box::new(self.node),
899 op: BoolOp::And,
900 right: Box::new(other.node),
901 })
902 }
903
904 pub fn or(self, other: Expr<bool>) -> Expr<bool> {
905 Expr::new(ExprNode::Bool {
906 left: Box::new(self.node),
907 op: BoolOp::Or,
908 right: Box::new(other.node),
909 })
910 }
911
912 pub fn not(self) -> Expr<bool> {
913 Expr::new(ExprNode::Unary {
914 op: UnaryOp::Not,
915 expr: Box::new(self.node),
916 })
917 }
918}
919
920impl<M, T> Column<M, T>
921where
922 T: 'static,
923{
924 pub fn eq<V>(self, value: V) -> Expr<bool>
925 where
926 V: ColumnValue<T>,
927 {
928 match value.into_value() {
929 Some(Value::Null) => Expr::new(ExprNode::IsNull {
930 expr: Box::new(ExprNode::Column(self.as_ref())),
931 negated: false,
932 }),
933 Some(value) => Expr::new(ExprNode::Binary {
934 left: Box::new(ExprNode::Column(self.as_ref())),
935 op: BinaryOp::Eq,
936 right: Box::new(ExprNode::Value(value)),
937 }),
938 None => Expr::new(ExprNode::IsNull {
939 expr: Box::new(ExprNode::Column(self.as_ref())),
940 negated: false,
941 }),
942 }
943 }
944
945 pub fn eq_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
946 Expr::new(ExprNode::Binary {
947 left: Box::new(ExprNode::Column(self.as_ref())),
948 op: BinaryOp::Eq,
949 right: Box::new(ExprNode::Column(other.as_ref())),
950 })
951 }
952
953 pub fn ne_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
954 Expr::new(ExprNode::Binary {
955 left: Box::new(ExprNode::Column(self.as_ref())),
956 op: BinaryOp::Ne,
957 right: Box::new(ExprNode::Column(other.as_ref())),
958 })
959 }
960
961 pub fn is_distinct_from_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
962 Expr::new(ExprNode::Binary {
963 left: Box::new(ExprNode::Column(self.as_ref())),
964 op: BinaryOp::IsDistinctFrom,
965 right: Box::new(ExprNode::Column(other.as_ref())),
966 })
967 }
968
969 pub fn is_not_distinct_from_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
970 Expr::new(ExprNode::Binary {
971 left: Box::new(ExprNode::Column(self.as_ref())),
972 op: BinaryOp::IsNotDistinctFrom,
973 right: Box::new(ExprNode::Column(other.as_ref())),
974 })
975 }
976
977 pub fn ne<V>(self, value: V) -> Expr<bool>
978 where
979 V: ColumnValue<T>,
980 {
981 match value.into_value() {
982 Some(Value::Null) => Expr::new(ExprNode::IsNull {
983 expr: Box::new(ExprNode::Column(self.as_ref())),
984 negated: true,
985 }),
986 Some(value) => Expr::new(ExprNode::Binary {
987 left: Box::new(ExprNode::Column(self.as_ref())),
988 op: BinaryOp::Ne,
989 right: Box::new(ExprNode::Value(value)),
990 }),
991 None => Expr::new(ExprNode::IsNull {
992 expr: Box::new(ExprNode::Column(self.as_ref())),
993 negated: true,
994 }),
995 }
996 }
997
998 pub fn lt<V>(self, value: V) -> Expr<bool>
999 where
1000 V: Into<Value>,
1001 {
1002 Expr::new(ExprNode::Binary {
1003 left: Box::new(ExprNode::Column(self.as_ref())),
1004 op: BinaryOp::Lt,
1005 right: Box::new(ExprNode::Value(value.into())),
1006 })
1007 }
1008
1009 pub fn lt_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
1010 Expr::new(ExprNode::Binary {
1011 left: Box::new(ExprNode::Column(self.as_ref())),
1012 op: BinaryOp::Lt,
1013 right: Box::new(ExprNode::Column(other.as_ref())),
1014 })
1015 }
1016
1017 pub fn le<V>(self, value: V) -> Expr<bool>
1018 where
1019 V: Into<Value>,
1020 {
1021 Expr::new(ExprNode::Binary {
1022 left: Box::new(ExprNode::Column(self.as_ref())),
1023 op: BinaryOp::Le,
1024 right: Box::new(ExprNode::Value(value.into())),
1025 })
1026 }
1027
1028 pub fn le_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
1029 Expr::new(ExprNode::Binary {
1030 left: Box::new(ExprNode::Column(self.as_ref())),
1031 op: BinaryOp::Le,
1032 right: Box::new(ExprNode::Column(other.as_ref())),
1033 })
1034 }
1035
1036 pub fn gt<V>(self, value: V) -> Expr<bool>
1037 where
1038 V: Into<Value>,
1039 {
1040 Expr::new(ExprNode::Binary {
1041 left: Box::new(ExprNode::Column(self.as_ref())),
1042 op: BinaryOp::Gt,
1043 right: Box::new(ExprNode::Value(value.into())),
1044 })
1045 }
1046
1047 pub fn gt_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
1048 Expr::new(ExprNode::Binary {
1049 left: Box::new(ExprNode::Column(self.as_ref())),
1050 op: BinaryOp::Gt,
1051 right: Box::new(ExprNode::Column(other.as_ref())),
1052 })
1053 }
1054
1055 pub fn ge<V>(self, value: V) -> Expr<bool>
1056 where
1057 V: Into<Value>,
1058 {
1059 Expr::new(ExprNode::Binary {
1060 left: Box::new(ExprNode::Column(self.as_ref())),
1061 op: BinaryOp::Ge,
1062 right: Box::new(ExprNode::Value(value.into())),
1063 })
1064 }
1065
1066 pub fn ge_col<M2>(self, other: Column<M2, T>) -> Expr<bool> {
1067 Expr::new(ExprNode::Binary {
1068 left: Box::new(ExprNode::Column(self.as_ref())),
1069 op: BinaryOp::Ge,
1070 right: Box::new(ExprNode::Column(other.as_ref())),
1071 })
1072 }
1073
1074 pub fn between<L, U>(self, low: L, high: U) -> Expr<bool>
1075 where
1076 L: Into<Value>,
1077 U: Into<Value>,
1078 {
1079 let left = ExprNode::Binary {
1080 left: Box::new(ExprNode::Column(self.as_ref())),
1081 op: BinaryOp::Ge,
1082 right: Box::new(ExprNode::Value(low.into())),
1083 };
1084 let right = ExprNode::Binary {
1085 left: Box::new(ExprNode::Column(self.as_ref())),
1086 op: BinaryOp::Le,
1087 right: Box::new(ExprNode::Value(high.into())),
1088 };
1089 Expr::new(ExprNode::Bool {
1090 left: Box::new(left),
1091 op: BoolOp::And,
1092 right: Box::new(right),
1093 })
1094 }
1095
1096 pub fn like<V>(self, pattern: V) -> Expr<bool>
1097 where
1098 V: Into<Value>,
1099 {
1100 Expr::new(ExprNode::Like {
1101 expr: Box::new(ExprNode::Column(self.as_ref())),
1102 pattern: pattern.into(),
1103 case_insensitive: false,
1104 })
1105 }
1106
1107 pub fn ilike<V>(self, pattern: V) -> Expr<bool>
1108 where
1109 V: Into<Value>,
1110 {
1111 Expr::new(ExprNode::Like {
1112 expr: Box::new(ExprNode::Column(self.as_ref())),
1113 pattern: pattern.into(),
1114 case_insensitive: true,
1115 })
1116 }
1117
1118 pub fn in_<I, V>(self, values: I) -> Expr<bool>
1119 where
1120 I: IntoIterator<Item = V>,
1121 V: Into<Value>,
1122 {
1123 Expr::new(ExprNode::In {
1124 expr: Box::new(ExprNode::Column(self.as_ref())),
1125 values: values.into_iter().map(Into::into).collect(),
1126 })
1127 }
1128
1129 pub fn is_null(self) -> Expr<bool> {
1130 Expr::new(ExprNode::IsNull {
1131 expr: Box::new(ExprNode::Column(self.as_ref())),
1132 negated: false,
1133 })
1134 }
1135
1136 pub fn is_not_null(self) -> Expr<bool> {
1137 Expr::new(ExprNode::IsNull {
1138 expr: Box::new(ExprNode::Column(self.as_ref())),
1139 negated: true,
1140 })
1141 }
1142}
1143
1144#[derive(Debug, Clone, Copy)]
1145pub enum ConditionKind {
1146 Any,
1147 All,
1148}
1149
1150#[derive(Debug, Clone)]
1151pub struct Condition {
1152 kind: ConditionKind,
1153 exprs: Vec<Expr<bool>>,
1154}
1155
1156impl Condition {
1157 pub fn any() -> Self {
1158 Self {
1159 kind: ConditionKind::Any,
1160 exprs: Vec::new(),
1161 }
1162 }
1163
1164 pub fn all() -> Self {
1165 Self {
1166 kind: ConditionKind::All,
1167 exprs: Vec::new(),
1168 }
1169 }
1170
1171 pub fn add(mut self, expr: Expr<bool>) -> Self {
1172 self.exprs.push(expr);
1173 self
1174 }
1175
1176 pub fn into_expr(self) -> Option<Expr<bool>> {
1177 let mut iter = self.exprs.into_iter();
1178 let first = iter.next()?;
1179 Some(iter.fold(first, |acc, expr| match self.kind {
1180 ConditionKind::Any => acc.or(expr),
1181 ConditionKind::All => acc.and(expr),
1182 }))
1183 }
1184}