etanol_databases/sql/query/
_where.rs

1use crate::Value;
2
3#[derive(Debug, Clone)]
4pub enum ModelWhere<T: Value> {
5    Equal(&'static str, T),
6    NotEqual(&'static str, T),
7    GreaterThan(&'static str, T),
8    GreaterThanOrEqual(&'static str, T),
9    LessThan(&'static str, T),
10    LessThanOrEqual(&'static str, T),
11    Contains(&'static str, T),
12}
13
14impl<T: Value> ModelWhere<T> {
15    pub fn sql(&self, position: usize) -> String {
16        match self {
17            ModelWhere::Equal(name, ..) => format!("{} = ?{}", name, position),
18            ModelWhere::NotEqual(name, ..) => format!("{} != ?{}", name, position),
19            ModelWhere::GreaterThan(name, ..) => format!("{} > ?{}", name, position),
20            ModelWhere::GreaterThanOrEqual(name, ..) => format!("{} >= ?{}", name, position),
21            ModelWhere::LessThan(name, ..) => format!("{} < ?{}", name, position),
22            ModelWhere::LessThanOrEqual(name, ..) => format!("{} <= ?{}", name, position),
23            ModelWhere::Contains(name, ..) => format!("{} LIKE ?{}", name, position),
24        }
25    }
26
27    pub fn key(&self) -> String {
28        match self {
29            ModelWhere::Equal(key, ..)
30            | ModelWhere::NotEqual(key, ..)
31            | ModelWhere::GreaterThan(key, ..)
32            | ModelWhere::GreaterThanOrEqual(key, ..)
33            | ModelWhere::LessThan(key, ..)
34            | ModelWhere::LessThanOrEqual(key, ..)
35            | ModelWhere::Contains(key, ..) => key.to_string(),
36        }
37    }
38
39    pub fn value(&self) -> String {
40        match self {
41            ModelWhere::Equal(_, v)
42            | ModelWhere::NotEqual(_, v)
43            | ModelWhere::GreaterThan(_, v)
44            | ModelWhere::GreaterThanOrEqual(_, v)
45            | ModelWhere::LessThan(_, v)
46            | ModelWhere::LessThanOrEqual(_, v) => v.toValue(None),
47            ModelWhere::Contains(_, v) => {
48                let value = v.toValue(None);
49
50                if &value != "None" {
51                    return format!("%{}%", value);
52                }
53                value
54            }
55        }
56    }
57}
58
59#[derive(Debug, Clone)]
60pub struct WhereQuery {
61    pub fields: Vec<String>,
62    pub table: String,
63    pub values: Vec<String>,
64}
65
66impl WhereQuery {
67    pub fn new(table: String) -> Self {
68        Self {
69            table,
70            fields: vec![],
71            values: vec![],
72        }
73    }
74
75    pub fn field<T: Value>(&mut self, value: ModelWhere<T>) -> &mut Self {
76        self.fields.push(value.sql(self.values.len() + 1));
77        self.values.push(value.value());
78
79        self
80    }
81
82    pub fn params(&self) -> Vec<String> {
83        if self.values.len() > 0 {
84            let mut params = vec![];
85
86            for value in self.values.clone() {
87                params.push(value);
88            }
89
90            return params;
91        }
92
93        return vec![];
94    }
95
96    pub fn sql(&self) -> String {
97        let mut sql = String::new();
98
99        if self.fields.len() > 0 {
100            sql = format!("WHERE ");
101
102            let mut wheres = vec![];
103
104            for value in self.fields.clone() {
105                wheres.push(value);
106            }
107
108            sql.push_str(&wheres.join(" AND "));
109        }
110
111        return sql;
112    }
113}