1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
pub struct Builder<'a>(crate::Where<'a>);

impl<'a> Builder<'a> {
    pub fn new() -> Self {
        Self(crate::Where::new())
    }

    pub fn build(self) -> crate::Where<'a> {
        self.0
    }

    pub fn r#in(mut self, element: &str, params: Vec<&'a dyn crate::ToSql>) -> Self {
        self.0 = crate::Where::new_in(element, params);

        self
    }

    pub fn not_in(mut self, element: &str, params: Vec<&'a dyn crate::ToSql>) -> Self {
        self.0 = crate::Where::new_not_in(element, params);

        self
    }

    pub fn group_condition(
        mut self,
        element: &str,
        operation: &str,
        params: Vec<&'a dyn crate::ToSql>,
    ) -> Self {
        self.0 = crate::Where::new_group_condition(element, operation, params);

        self
    }

    pub fn and_where(mut self, element: &str, params: Vec<&'a dyn crate::ToSql>) -> Self {
        self.0.and_where(element, params);

        self
    }

    pub fn or_where(mut self, element: &str, params: Vec<&'a dyn crate::ToSql>) -> Self {
        self.0.or_where(element, params);

        self
    }

    pub fn add_where(
        mut self,
        element: &str,
        params: Vec<&'a dyn crate::ToSql>,
        operator: &str,
    ) -> Self {
        self.0.add_where(element, params, operator);

        self
    }
}

impl<'a> Default for Builder<'a> {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod test {
    #[test]
    fn builder() {
        let w = crate::Where::builder()
            .and_where("a", vec![&1])
            .and_where("b", Vec::new())
            .or_where("c", vec![&2, &3])
            .or_where("d", vec![&4])
            .add_where("e", Vec::new(), "like")
            .build();

        assert_eq!(w.to_string(), "(((a and b) or c or d) like e)");
        assert_eq!(
            w.params()
                .iter()
                .map(|x| x.to_sql().unwrap())
                .collect::<Vec<_>>(),
            vec![
                Some(vec![b'1', 0]),
                Some(vec![b'2', 0]),
                Some(vec![b'3', 0]),
                Some(vec![b'4', 0])
            ],
        );
    }
}