Skip to main content

fletch_orm/
filter.rs

1//! Filter types for building WHERE clauses.
2
3use crate::value::Value;
4
5/// Comparison operators for filter expressions.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum Op {
8    /// Equal (`=`)
9    Eq,
10    /// Not equal (`!=` / `<>`)
11    Ne,
12    /// Less than (`<`)
13    Lt,
14    /// Less than or equal (`<=`)
15    Le,
16    /// Greater than (`>`)
17    Gt,
18    /// Greater than or equal (`>=`)
19    Ge,
20    /// `LIKE` pattern match
21    Like,
22    /// `IN` a list of values
23    In,
24    /// `IS NULL`
25    IsNull,
26    /// `IS NOT NULL`
27    IsNotNull,
28}
29
30/// A single filter condition: column, operator, and bound value(s).
31#[derive(Debug, Clone)]
32pub struct Filter {
33    pub(crate) column: String,
34    pub(crate) op: Op,
35    pub(crate) values: Vec<Value>,
36}
37
38impl Filter {
39    /// Create a filter for a single-value operator (Eq, Ne, Lt, Le, Gt, Ge, Like).
40    pub fn new(column: impl Into<String>, op: Op, value: impl Into<Value>) -> Self {
41        Self {
42            column: column.into(),
43            op,
44            values: vec![value.into()],
45        }
46    }
47
48    /// Create an `IN` filter with multiple values.
49    pub fn in_list(column: impl Into<String>, values: Vec<Value>) -> Self {
50        Self {
51            column: column.into(),
52            op: Op::In,
53            values,
54        }
55    }
56
57    /// Create an `IS NULL` filter.
58    pub fn is_null(column: impl Into<String>) -> Self {
59        Self {
60            column: column.into(),
61            op: Op::IsNull,
62            values: vec![],
63        }
64    }
65
66    /// Create an `IS NOT NULL` filter.
67    pub fn is_not_null(column: impl Into<String>) -> Self {
68        Self {
69            column: column.into(),
70            op: Op::IsNotNull,
71            values: vec![],
72        }
73    }
74}