lua_sql_builder/mysql/
where_.rs

1#[allow(dead_code)]
2pub struct Where {
3    statement: String,
4    combined_by: Combiner,
5}
6
7#[allow(dead_code)]
8impl Where {
9    pub fn new(combined_by: Combiner) -> Where {
10        Where {
11            statement: String::new(),
12            combined_by,
13        }
14    }
15
16    pub fn equal_to(&mut self, field: &str, value: &str) -> &mut Where {
17        self.add_comparative_predicate("=", field, value)
18    }
19
20    pub fn not_equal_to(&mut self, field: &str, value: &str) -> &mut Where {
21        self.add_comparative_predicate("!=", field, value)
22    }
23
24    pub fn greater_than(&mut self, field: &str, value: &str) -> &mut Where {
25        self.add_comparative_predicate(">", field, value)
26    }
27
28    pub fn greater_than_equal(&mut self, field: &str, value: &str) -> &mut Where {
29        self.add_comparative_predicate(">=", field, value)
30    }
31
32    pub fn less_than(&mut self, field: &str, value: &str) -> &mut Where {
33        self.add_comparative_predicate("<", field, value)
34    }
35
36    pub fn less_than_equal(&mut self, field: &str, value: &str) -> &mut Where {
37        self.add_comparative_predicate("<=", field, value)
38    }
39
40    pub fn is_null(&mut self, field: &str) -> &mut Where {
41        self.add_self_comparative_predicate("ISNULL", field);
42        self
43    }
44
45    pub fn is_not_null(&mut self, field: &str) -> &mut Where {
46        self.add_self_comparative_predicate("IS NOT NULL", field);
47        self
48    }
49
50    pub fn in_(&mut self, field: &str, fields: Vec<&str>) -> &mut Where {
51        self.add_multiple_values_comparative_predicate("IN", field, fields);
52        self
53    }
54
55    pub fn not_in(&mut self, field: &str, fields: Vec<&str>) -> &mut Where {
56        self.add_multiple_values_comparative_predicate("NOT IN", field, fields);
57        self
58    }
59
60    pub fn like(&mut self, field: &str, value: &str) -> &mut Where {
61        self.add_comparative_predicate("LIKE", field, value)
62    }
63
64    pub fn not_like(&mut self, field: &str, value: &str) -> &mut Where {
65        self.add_comparative_predicate("NOT LIKE", field, value)
66    }
67
68    pub fn build(&self) -> String {
69        if self.statement.len() > 0 {
70            return "WHERE".to_string() + &self.statement;
71        }
72        "".to_string()
73    }
74
75    fn add_combiner(&mut self) {
76        let combined = match self.combined_by {
77            Combiner::And => "AND",
78            Combiner::Or => "OR",
79        };
80
81        if self.statement.len() > 0 {
82            self.statement.push_str(&format!(" {}", combined))
83        }
84    }
85
86    fn add_comparative_predicate(
87        &mut self,
88        operator: &str,
89        field: &str,
90        value: &str,
91    ) -> &mut Where {
92        self.add_combiner();
93        if value.parse::<f64>().is_ok() {
94            self.statement
95                .push_str(&format!(" {} {} {}", field, operator, value));
96        } else {
97            self.statement
98                .push_str(&format!(" {} {} '{}'", field, operator, value));
99        }
100        self
101    }
102
103    fn add_self_comparative_predicate(&mut self, operator: &str, field: &str) -> &mut Where {
104        self.add_combiner();
105        self.statement.push_str(&format!(" {} {}", field, operator));
106        self
107    }
108
109    fn add_multiple_values_comparative_predicate(
110        &mut self,
111        operator: &str,
112        field: &str,
113        fields: Vec<&str>,
114    ) -> &mut Where {
115        self.add_combiner();
116
117        let values = "'".to_owned() + &fields.join("', '") + "'";
118
119        self.statement
120            .push_str(&format!(" {} {} ({})", field, operator, values));
121        self
122    }
123}
124
125#[allow(dead_code)]
126pub enum Combiner {
127    And,
128    Or,
129}