mydb_sqlx/conditions/
query.rs1
2#[warn(non_snake_case)]
3pub fn query_wrapper () -> QueryWrapper {
4 QueryWrapper::new()
5}
6
7#[derive(Debug, Clone, Default)]
8pub struct QueryWrapper {
9 wrapper: Vec<String>,
10}
11
12impl QueryWrapper {
13 pub fn new() -> Self {
14 Self {
15 wrapper: vec![]
16 }
17 }
18
19 pub fn like<T>(&mut self, key: &'static str, value: Option<T>) -> &mut Self
20 where
21 T: std::fmt::Display,
22 {
23 if let Some(v) = value {
24 self.wrapper.push(format!("{} LIKE '%{}%'", key, v));
25 }
26 self
27 }
28
29 pub fn left_like<T>(&mut self, key: &'static str, value: Option<T>) -> &mut Self
30 where
31 T: std::fmt::Display,
32 {
33 if let Some(v) = value {
34 self.wrapper.push(format!("{} LIKE '%{}'", key, v));
35 }
36 self
37 }
38
39 pub fn right_like<T>(&mut self, key: &'static str, value: Option<T>) -> &mut Self
40 where
41 T: std::fmt::Display,
42 {
43 if let Some(v) = value {
44 self.wrapper.push(format!("{} LIKE '{}%'", key, v));
45 }
46 self
47 }
48
49 pub fn eq<T>(&mut self, key: &'static str, value: Option<T>) -> &mut Self
50 where
51 T: std::fmt::Display,
52 {
53 if let Some(v) = value {
54 self.wrapper.push(format!("{} = '{}'", key, v));
55 }
56 self
57 }
58
59 pub fn ne<T>(&mut self, key: &'static str, value: Option<T>) -> &mut Self
60 where
61 T: std::fmt::Display,
62 {
63 if let Some(v) = value {
64 self.wrapper.push(format!("{} != '{}'", key, v));
65 }
66 self
67 }
68
69 pub fn gt<T>(&mut self, key: &'static str, value: Option<T>) -> &mut Self
70 where
71 T: std::fmt::Display,
72 {
73 if let Some(v) = value {
74 self.wrapper.push(format!("{} > '{}'", key, v));
75 }
76 self
77 }
78
79 pub fn lt<T>(&mut self, key: &'static str, value: Option<T>) -> &mut Self
80 where
81 T: std::fmt::Display,
82 {
83 if let Some(v) = value {
84 self.wrapper.push(format!("{} < '{}'", key, v));
85 }
86 self
87 }
88
89 pub fn ge<T>(&mut self, key: &'static str, value: Option<T>) -> &mut Self
90 where
91 T: std::fmt::Display,
92 {
93 if let Some(v) = value {
94 self.wrapper.push(format!("{} >= '{}'", key, v));
95 }
96 self
97 }
98
99 pub fn le<T>(&mut self, key: &'static str, value: Option<T>) -> &mut Self
100 where
101 T: std::fmt::Display,
102 {
103 if let Some(v) = value {
104 self.wrapper.push(format!("{} <= '{}'", key, v));
105 }
106 self
107 }
108
109 pub fn range<T>(
110 &mut self,
111 key: &'static str,
112 s: Option<T>,
113 e: Option<T>,
114 ) -> &mut Self
115 where
116 T: std::fmt::Display,
117 {
118 if let Some(v) = s {
119 self.wrapper.push(format!("{} >= '{}'", key, v));
120 }
121 if let Some(v) = e {
122 self.wrapper.push(format!("{} <= '{}'", key, v));
123 }
124 self
125 }
126
127 pub fn build(&mut self) -> String {
128 self.wrapper.join(" AND ")
129 }
130}