elif_orm/query/
where_clause.rs

1//! Query Builder WHERE clause operations
2
3use serde_json::Value;
4use super::builder::QueryBuilder;
5use super::types::*;
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>>(mut self, column: &str, operator: &str, value: T) -> Self {
101        let query_operator = match operator {
102            "=" => QueryOperator::Equal,
103            "!=" | "<>" => QueryOperator::NotEqual,
104            ">" => QueryOperator::GreaterThan,
105            ">=" => QueryOperator::GreaterThanOrEqual,
106            "<" => QueryOperator::LessThan,
107            "<=" => QueryOperator::LessThanOrEqual,
108            "LIKE" => QueryOperator::Like,
109            "NOT LIKE" => QueryOperator::NotLike,
110            _ => QueryOperator::Equal, // Default fallback
111        };
112        
113        self.where_conditions.push(WhereCondition {
114            column: column.to_string(),
115            operator: query_operator,
116            value: Some(value.into()),
117            values: Vec::new(),
118        });
119        self
120    }
121
122    /// Add WHERE condition with IN
123    pub fn where_in<T: Into<Value>>(mut self, column: &str, values: Vec<T>) -> Self {
124        self.where_conditions.push(WhereCondition {
125            column: column.to_string(),
126            operator: QueryOperator::In,
127            value: None,
128            values: values.into_iter().map(|v| v.into()).collect(),
129        });
130        self
131    }
132
133    /// Add WHERE condition with NOT IN
134    pub fn where_not_in<T: Into<Value>>(mut self, column: &str, values: Vec<T>) -> Self {
135        self.where_conditions.push(WhereCondition {
136            column: column.to_string(),
137            operator: QueryOperator::NotIn,
138            value: None,
139            values: values.into_iter().map(|v| v.into()).collect(),
140        });
141        self
142    }
143
144    /// Add WHERE condition with IS NULL
145    pub fn where_null(mut self, column: &str) -> Self {
146        self.where_conditions.push(WhereCondition {
147            column: column.to_string(),
148            operator: QueryOperator::IsNull,
149            value: None,
150            values: Vec::new(),
151        });
152        self
153    }
154
155    /// Add WHERE condition with IS NOT NULL
156    pub fn where_not_null(mut self, column: &str) -> Self {
157        self.where_conditions.push(WhereCondition {
158            column: column.to_string(),
159            operator: QueryOperator::IsNotNull,
160            value: None,
161            values: Vec::new(),
162        });
163        self
164    }
165
166    /// Add WHERE condition with BETWEEN
167    pub fn where_between<T: Into<Value>>(mut self, column: &str, start: T, end: T) -> Self {
168        self.where_conditions.push(WhereCondition {
169            column: column.to_string(),
170            operator: QueryOperator::Between,
171            value: None,
172            values: vec![start.into(), end.into()],
173        });
174        self
175    }
176
177    /// Add raw WHERE condition for complex cases
178    pub fn where_raw(mut self, raw_condition: &str) -> Self {
179        self.where_conditions.push(WhereCondition {
180            column: "RAW".to_string(),
181            operator: QueryOperator::Equal,
182            value: Some(Value::String(raw_condition.to_string())),
183            values: Vec::new(),
184        });
185        self
186    }
187
188    /// Add a subquery in the WHERE clause
189    pub fn where_subquery<T>(mut self, column: &str, operator: QueryOperator, subquery: QueryBuilder<T>) -> Self {
190        let subquery_sql = subquery.to_sql();
191        let formatted_value = format!("({})", subquery_sql);
192        
193        self.where_conditions.push(WhereCondition {
194            column: column.to_string(),
195            operator,
196            value: Some(Value::String(formatted_value)),
197            values: Vec::new(),
198        });
199        self
200    }
201
202    /// Add EXISTS subquery condition
203    pub fn where_exists<T>(mut self, subquery: QueryBuilder<T>) -> Self {
204        self.where_conditions.push(WhereCondition {
205            column: "EXISTS".to_string(),
206            operator: QueryOperator::Equal,
207            value: Some(Value::String(format!("({})", subquery.to_sql()))),
208            values: Vec::new(),
209        });
210        self
211    }
212
213    /// Add NOT EXISTS subquery condition
214    pub fn where_not_exists<T>(mut self, subquery: QueryBuilder<T>) -> Self {
215        self.where_conditions.push(WhereCondition {
216            column: "NOT EXISTS".to_string(),
217            operator: QueryOperator::Equal,
218            value: Some(Value::String(format!("({})", subquery.to_sql()))),
219            values: Vec::new(),
220        });
221        self
222    }
223
224}