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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use crate::{
behavior::BuilderInner,
fmt,
structure::{UpdateBuilder, UpdateClause},
};
impl<'a> UpdateBuilder<'a> {
/// Gets the current state of the UpdateBuilder and returns it as string
pub fn as_string(&self) -> String {
let fmts = fmt::Formatter::one_line();
self.concat(&fmts)
}
/// Prints the current state of the UpdateBuilder into console output in a more ease to read version.
/// This method is useful to debug complex queries or just to print the generated SQL while you type
/// ```
/// use sql_query_builder::UpdateBuilder;
///
/// let update_query = UpdateBuilder::new()
/// .update("users")
/// .set("login = 'foo'")
/// .debug()
/// .set("name = 'Foo'")
/// .as_string();
/// ```
///
/// Output
///
/// ```sql
/// UPDATE users
/// SET login = 'foo'
/// ```
pub fn debug(self) -> Self {
let fmts = fmt::Formatter::multi_line();
println!("{}", fmt::colorize(self.concat(&fmts)));
self
}
/// Create UpdateBuilder's instance
pub fn new() -> Self {
Self::default()
}
/// Prints the current state of the UpdateBuilder into console output similar to debug method,
/// the difference is that this method prints in one line.
pub fn print(self) -> Self {
let fmts = fmt::Formatter::one_line();
println!("{}", fmt::colorize(self.concat(&fmts)));
self
}
/// Adds at the beginning a raw SQL query.
///
/// ```
/// use sql_query_builder::UpdateBuilder;
///
/// let raw_query = "update users";
/// let update_query = UpdateBuilder::new()
/// .raw(raw_query)
/// .set("login = 'foo'")
/// .as_string();
/// ```
///
/// Output
///
/// ```sql
/// update users
/// SET login = 'foo'
/// ```
pub fn raw(mut self, raw_sql: &'a str) -> Self {
self._raw.push(raw_sql.to_owned());
self
}
/// Adds a raw SQL query after a specified clause.
///
/// ```
/// use sql_query_builder::{UpdateClause, UpdateBuilder};
///
/// let raw = "set name = 'Foo'";
/// let update_query = UpdateBuilder::new()
/// .update("users")
/// .raw_after(UpdateClause::Update, raw)
/// .as_string();
/// ```
///
/// Output
///
/// ```sql
/// UPDATE users
/// set name = 'Foo'
/// ```
pub fn raw_after(mut self, clause: UpdateClause, raw_sql: &'a str) -> Self {
self._raw_after.push((clause, raw_sql.to_owned()));
self
}
/// Adds a raw SQL query before a specified clause.
///
/// ```
/// use sql_query_builder::{UpdateClause, UpdateBuilder};
///
/// let raw = "update users";
/// let update_query = UpdateBuilder::new()
/// .raw_before(UpdateClause::Set, raw)
/// .set("name = 'Bar'")
/// .as_string();
/// ```
///
/// Output
///
/// ```sql
/// update users
/// SET name = 'Bar'
/// ```
pub fn raw_before(mut self, clause: UpdateClause, raw_sql: &'a str) -> Self {
self._raw_before.push((clause, raw_sql.to_owned()));
self
}
/// The set clause
pub fn set(mut self, value: &'a str) -> Self {
self._set.push(value.to_owned());
self
}
/// The update clause. This method overrides the previous value
///
/// ```
/// use sql_query_builder::UpdateBuilder;
///
/// let update = UpdateBuilder::new()
/// .update("orders");
///
/// let update = UpdateBuilder::new()
/// .update("address")
/// .update("orders");
/// ```
pub fn update(mut self, table_name: &'a str) -> Self {
self._update = table_name;
self
}
/// The where by clause
pub fn where_clause(mut self, condition: &'a str) -> Self {
self._where.push(condition.to_owned());
self
}
}
impl BuilderInner<'_, UpdateClause> for UpdateBuilder<'_> {
fn concat(&self, fmts: &fmt::Formatter) -> String {
let mut query = "".to_owned();
query = self.concat_raw(query, &fmts);
query = self.concat_update(query, &fmts);
query = self.concat_set(query, &fmts);
query = self.concat_where(query, &fmts);
query.trim_end().to_owned()
}
fn raws(&self) -> &Vec<String> {
&self._raw
}
fn raw_after(&self) -> &Vec<(UpdateClause, String)> {
&self._raw_after
}
fn raw_before(&self) -> &Vec<(UpdateClause, String)> {
&self._raw_before
}
}
impl UpdateBuilder<'_> {
fn concat_set(&self, query: String, fmts: &fmt::Formatter) -> String {
let fmt::Formatter { comma, lb, space, .. } = fmts;
let sql = if self._set.is_empty() == false {
let values = self._set.join(comma);
format!("SET{space}{values}{space}{lb}")
} else {
"".to_owned()
};
self.concat_raw_before_after(UpdateClause::Set, query, fmts, sql)
}
fn concat_update(&self, query: String, fmts: &fmt::Formatter) -> String {
let fmt::Formatter { lb, space, .. } = fmts;
let sql = if self._update.is_empty() == false {
let table_name = self._update;
format!("UPDATE{space}{table_name}{space}{lb}")
} else {
"".to_owned()
};
self.concat_raw_before_after(UpdateClause::Update, query, fmts, sql)
}
fn concat_where(&self, query: String, fmts: &fmt::Formatter) -> String {
let fmt::Formatter { lb, space, .. } = fmts;
let sql = if self._where.is_empty() == false {
let conditions = self._where.join(" AND ");
format!("WHERE {conditions}{space}{lb}")
} else {
"".to_owned()
};
self.concat_raw_before_after(UpdateClause::Where, query, fmts, sql)
}
}
impl<'a> std::fmt::Display for UpdateBuilder<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_string())
}
}
impl<'a> std::fmt::Debug for UpdateBuilder<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let fmts = fmt::Formatter::multi_line();
write!(f, "{}", fmt::colorize(self.concat(&fmts)))
}
}