Skip to main content

systemprompt_models/repository/
query_builder.rs

1#[derive(Debug)]
2pub struct WhereClause {
3    conditions: Vec<String>,
4    params: Vec<String>,
5}
6
7impl WhereClause {
8    pub const fn new() -> Self {
9        Self {
10            conditions: vec![],
11            params: vec![],
12        }
13    }
14
15    pub fn eq(mut self, field: &str, value: impl Into<String>) -> Self {
16        self.conditions.push(format!("{field} = ?"));
17        self.params.push(value.into());
18        self
19    }
20
21    pub fn not_null(mut self, field: &str) -> Self {
22        self.conditions.push(format!("{field} IS NOT NULL"));
23        self
24    }
25
26    pub fn null(mut self, field: &str) -> Self {
27        self.conditions.push(format!("{field} IS NULL"));
28        self
29    }
30
31    pub fn like(mut self, field: &str, pattern: impl Into<String>) -> Self {
32        self.conditions.push(format!("{field} LIKE ?"));
33        self.params.push(pattern.into());
34        self
35    }
36
37    pub fn in_list(mut self, field: &str, values: Vec<String>) -> Self {
38        let placeholders = values.iter().map(|_| "?").collect::<Vec<_>>().join(", ");
39        self.conditions.push(format!("{field} IN ({placeholders})"));
40        self.params.extend(values);
41        self
42    }
43
44    pub fn build(&self) -> (String, Vec<String>) {
45        let clause = if self.conditions.is_empty() {
46            String::new()
47        } else {
48            format!("WHERE {}", self.conditions.join(" AND "))
49        };
50        (clause, self.params.clone())
51    }
52}
53
54impl Default for WhereClause {
55    fn default() -> Self {
56        Self::new()
57    }
58}