elif_orm/query/
where_clause.rs

1//! Query Builder WHERE clause operations
2
3use super::builder::QueryBuilder;
4use super::types::*;
5use serde_json::Value;
6
7impl<M> QueryBuilder<M> {
8    /// Add WHERE condition with equality
9    pub fn where_eq<T>(mut self, column: &str, value: T) -> Self
10    where
11        T: Into<Value>,
12    {
13        self.where_conditions.push(WhereCondition {
14            column: column.to_string(),
15            operator: QueryOperator::Equal,
16            value: Some(value.into()),
17            values: Vec::new(),
18        });
19        self
20    }
21
22    /// Add WHERE condition with not equal
23    pub fn where_ne<T: Into<Value>>(mut self, column: &str, value: T) -> Self {
24        self.where_conditions.push(WhereCondition {
25            column: column.to_string(),
26            operator: QueryOperator::NotEqual,
27            value: Some(value.into()),
28            values: Vec::new(),
29        });
30        self
31    }
32
33    /// Add WHERE condition with greater than
34    pub fn where_gt<T: Into<Value>>(mut self, column: &str, value: T) -> Self {
35        self.where_conditions.push(WhereCondition {
36            column: column.to_string(),
37            operator: QueryOperator::GreaterThan,
38            value: Some(value.into()),
39            values: Vec::new(),
40        });
41        self
42    }
43
44    /// Add WHERE condition with greater than or equal
45    pub fn where_gte<T: Into<Value>>(mut self, column: &str, value: T) -> Self {
46        self.where_conditions.push(WhereCondition {
47            column: column.to_string(),
48            operator: QueryOperator::GreaterThanOrEqual,
49            value: Some(value.into()),
50            values: Vec::new(),
51        });
52        self
53    }
54
55    /// Add WHERE condition with less than
56    pub fn where_lt<T: Into<Value>>(mut self, column: &str, value: T) -> Self {
57        self.where_conditions.push(WhereCondition {
58            column: column.to_string(),
59            operator: QueryOperator::LessThan,
60            value: Some(value.into()),
61            values: Vec::new(),
62        });
63        self
64    }
65
66    /// Add WHERE condition with less than or equal
67    pub fn where_lte<T: Into<Value>>(mut self, column: &str, value: T) -> Self {
68        self.where_conditions.push(WhereCondition {
69            column: column.to_string(),
70            operator: QueryOperator::LessThanOrEqual,
71            value: Some(value.into()),
72            values: Vec::new(),
73        });
74        self
75    }
76
77    /// Add WHERE condition with LIKE
78    pub fn where_like(mut self, column: &str, pattern: &str) -> Self {
79        self.where_conditions.push(WhereCondition {
80            column: column.to_string(),
81            operator: QueryOperator::Like,
82            value: Some(Value::String(pattern.to_string())),
83            values: Vec::new(),
84        });
85        self
86    }
87
88    /// Add WHERE condition with NOT LIKE
89    pub fn where_not_like(mut self, column: &str, pattern: &str) -> Self {
90        self.where_conditions.push(WhereCondition {
91            column: column.to_string(),
92            operator: QueryOperator::NotLike,
93            value: Some(Value::String(pattern.to_string())),
94            values: Vec::new(),
95        });
96        self
97    }
98
99    /// Add WHERE condition with custom operator
100    pub fn where_condition<T: Into<Value>>(
101        mut self,
102        column: &str,
103        operator: &str,
104        value: T,
105    ) -> Self {
106        let query_operator = match operator {
107            "=" => QueryOperator::Equal,
108            "!=" | "<>" => QueryOperator::NotEqual,
109            ">" => QueryOperator::GreaterThan,
110            ">=" => QueryOperator::GreaterThanOrEqual,
111            "<" => QueryOperator::LessThan,
112            "<=" => QueryOperator::LessThanOrEqual,
113            "LIKE" => QueryOperator::Like,
114            "NOT LIKE" => QueryOperator::NotLike,
115            _ => QueryOperator::Equal, // Default fallback
116        };
117
118        self.where_conditions.push(WhereCondition {
119            column: column.to_string(),
120            operator: query_operator,
121            value: Some(value.into()),
122            values: Vec::new(),
123        });
124        self
125    }
126
127    /// Add WHERE condition with IN
128    pub fn where_in<T: Into<Value>>(mut self, column: &str, values: Vec<T>) -> Self {
129        self.where_conditions.push(WhereCondition {
130            column: column.to_string(),
131            operator: QueryOperator::In,
132            value: None,
133            values: values.into_iter().map(|v| v.into()).collect(),
134        });
135        self
136    }
137
138    /// Add WHERE condition with NOT IN
139    pub fn where_not_in<T: Into<Value>>(mut self, column: &str, values: Vec<T>) -> Self {
140        self.where_conditions.push(WhereCondition {
141            column: column.to_string(),
142            operator: QueryOperator::NotIn,
143            value: None,
144            values: values.into_iter().map(|v| v.into()).collect(),
145        });
146        self
147    }
148
149    /// Add WHERE condition with IS NULL
150    pub fn where_null(mut self, column: &str) -> Self {
151        self.where_conditions.push(WhereCondition {
152            column: column.to_string(),
153            operator: QueryOperator::IsNull,
154            value: None,
155            values: Vec::new(),
156        });
157        self
158    }
159
160    /// Add WHERE condition with IS NOT NULL
161    pub fn where_not_null(mut self, column: &str) -> Self {
162        self.where_conditions.push(WhereCondition {
163            column: column.to_string(),
164            operator: QueryOperator::IsNotNull,
165            value: None,
166            values: Vec::new(),
167        });
168        self
169    }
170
171    /// Add WHERE condition with BETWEEN
172    pub fn where_between<T: Into<Value>>(mut self, column: &str, start: T, end: T) -> Self {
173        self.where_conditions.push(WhereCondition {
174            column: column.to_string(),
175            operator: QueryOperator::Between,
176            value: None,
177            values: vec![start.into(), end.into()],
178        });
179        self
180    }
181
182    /// Add raw WHERE condition for complex cases
183    pub fn where_raw(mut self, raw_condition: &str) -> Self {
184        self.where_conditions.push(WhereCondition {
185            column: "RAW".to_string(),
186            operator: QueryOperator::Equal,
187            value: Some(Value::String(raw_condition.to_string())),
188            values: Vec::new(),
189        });
190        self
191    }
192
193    /// Add a subquery in the WHERE clause
194    pub fn where_subquery<T>(
195        mut self,
196        column: &str,
197        operator: QueryOperator,
198        subquery: QueryBuilder<T>,
199    ) -> Self {
200        let subquery_sql = subquery.to_sql();
201        let formatted_value = format!("({})", subquery_sql);
202
203        self.where_conditions.push(WhereCondition {
204            column: column.to_string(),
205            operator,
206            value: Some(Value::String(formatted_value)),
207            values: Vec::new(),
208        });
209        self
210    }
211
212    /// Add EXISTS subquery condition
213    pub fn where_exists<T>(mut self, subquery: QueryBuilder<T>) -> Self {
214        self.where_conditions.push(WhereCondition {
215            column: "EXISTS".to_string(),
216            operator: QueryOperator::Equal,
217            value: Some(Value::String(format!("({})", subquery.to_sql()))),
218            values: Vec::new(),
219        });
220        self
221    }
222
223    /// Add NOT EXISTS subquery condition
224    pub fn where_not_exists<T>(mut self, subquery: QueryBuilder<T>) -> Self {
225        self.where_conditions.push(WhereCondition {
226            column: "NOT EXISTS".to_string(),
227            operator: QueryOperator::Equal,
228            value: Some(Value::String(format!("({})", subquery.to_sql()))),
229            values: Vec::new(),
230        });
231        self
232    }
233}