1use json::{JsonValue, object};
2
3#[cfg(feature = "db-sqlite")]
4pub mod sqlite;
5#[cfg(feature = "db-mysql")]
6pub mod mysql;
7#[cfg(feature = "db-mssql")]
8pub mod mssql;
9#[cfg(feature = "db-pgsql")]
10pub mod pgsql;
11
12pub trait DbMode {
13 fn database_tables(&mut self) -> JsonValue;
15 fn database_create(&mut self, name: &str) -> bool;
17 fn backups(&mut self, _filename: &str) -> bool {
19 false
20 }
21}
22pub struct TableOptions {
23 table_name: String,
24 table_title: String,
25 table_key: String,
26 table_fields: JsonValue,
27 table_unique: Vec<String>,
28 table_index: Vec<Vec<String>>,
29 table_partition: bool,
30 table_partition_columns: JsonValue,
31}
32impl TableOptions {
33 pub fn set_table_name(&mut self, name: &str) {
34 self.table_name = name.to_string()
35 }
36 pub fn set_table_title(&mut self, name: &str) {
37 self.table_title = name.to_string()
38 }
39 pub fn set_table_key(&mut self, name: &str) {
40 self.table_key = name.to_string()
41 }
42 pub fn set_table_fields(&mut self, fields: JsonValue) {
43 self.table_fields = fields;
44 }
45 pub fn set_table_unique(&mut self, unique: Vec<&str>) {
46 self.table_unique = unique.iter().map(|s| s.to_string()).collect();
47 }
48 pub fn set_table_index(&mut self, index: Vec<Vec<&str>>) {
49 self.table_index = index.iter().map(|s| s.iter().map(|s| s.to_string()).collect()).collect();
50 }
51 pub fn set_table_partition(&mut self, index: bool) {
52 self.table_partition = index;
53 }
54 pub fn set_table_partition_columns(&mut self, index: JsonValue) {
55 self.table_partition_columns = index;
56 }
57}
58impl Default for TableOptions {
59 fn default() -> Self {
60 Self {
61 table_name: "".to_string(),
62 table_title: "".to_string(),
63 table_key: "".to_string(),
64 table_fields: JsonValue::Null,
65 table_unique: vec![],
66 table_index: vec![],
67 table_partition: false,
68 table_partition_columns: JsonValue::Null,
69 }
70 }
71}
72pub trait Mode: DbMode {
73 fn table_create(&mut self, options: TableOptions) -> JsonValue;
74 fn table_update(&mut self, options: TableOptions) -> JsonValue;
75 fn table_info(&mut self, table: &str) -> JsonValue;
77 fn table_is_exist(&mut self, name: &str) -> bool;
79 fn table(&mut self, name: &str) -> &mut Self;
81 fn change_table(&mut self, name: &str) -> &mut Self;
83
84 fn autoinc(&mut self) -> &mut Self;
86 fn fetch_sql(&mut self) -> &mut Self;
88 fn order(&mut self, field: &str, by: bool) -> &mut Self;
90 fn group(&mut self, field: &str) -> &mut Self;
92 fn distinct(&mut self) -> &mut Self;
94 fn json(&mut self, field: &str) -> &mut Self;
96 fn field(&mut self, field: &str) -> &mut Self;
98 fn hidden(&mut self, name: &str) -> &mut Self;
100 fn where_and(&mut self, field: &str, compare: &str, value: JsonValue) -> &mut Self;
102 fn where_or(&mut self, field: &str, compare: &str, value: JsonValue) -> &mut Self;
103
104 fn where_column(&mut self, field_a: &str, compare: &str, field_b: &str) -> &mut Self;
106 fn page(&mut self, page: i32, limit: i32) -> &mut Self;
108 fn column(&mut self, field: &str) -> JsonValue;
110 fn count(&mut self) -> JsonValue;
112 fn max(&mut self, field: &str) -> JsonValue;
114 fn min(&mut self, field: &str) -> JsonValue;
116 fn sum(&mut self, field: &str) -> JsonValue;
118 fn avg(&mut self, field: &str) -> JsonValue;
120 fn select(&mut self) -> JsonValue;
122 fn find(&mut self) -> JsonValue;
124 fn value(&mut self, field: &str) -> JsonValue;
126 fn insert(&mut self, data: JsonValue) -> JsonValue;
128 fn insert_all(&mut self, data: JsonValue) -> JsonValue;
130
131 fn update(&mut self, data: JsonValue) -> JsonValue;
133 fn update_all(&mut self, data: JsonValue) -> JsonValue;
135 fn delete(&mut self) -> JsonValue;
137
138 fn transaction(&mut self) -> bool;
140 fn commit(&mut self) -> bool;
142 fn rollback(&mut self) -> bool;
144 fn sql(&mut self, sql: &str) -> Result<JsonValue, String>;
146 fn sql_execute(&mut self, sql: &str) -> Result<JsonValue, String>;
147 fn inc(&mut self, field: &str, num: f64) -> &mut Self;
149 fn dec(&mut self, field: &str, num: f64) -> &mut Self;
151
152 fn buildsql(&mut self) -> String;
154 fn join(&mut self, table: &str, main_fields: &str, right_fields: &str) -> &mut Self;
160
161 fn join_inner(&mut self, table: &str, main_fields: &str, second_fields: &str) -> &mut Self;
167}
168
169#[derive(Clone, Debug)]
170pub struct Params {
171 pub mode: String,
172 pub autoinc: bool,
173 pub table: String,
174 pub where_and: Vec<String>,
175 pub where_or: Vec<String>,
176 pub where_column: String,
177 pub inc_dec: JsonValue,
178 pub page: i32,
179 pub limit: i32,
180 pub fields: JsonValue,
181 pub top: String,
182 pub top2: String,
183 pub order: JsonValue,
184 pub group: JsonValue,
185 pub distinct: bool,
186 pub json: JsonValue,
187 pub sql: bool,
188 pub join: Vec<String>,
189 pub join_inner: Vec<String>,
190 pub join_table: String,
191}
192
193impl Params {
194 pub fn default(mode: &str) -> Self {
195 Self {
196 mode: mode.to_string(),
197 autoinc: false,
198 table: "".to_string(),
199 where_and: vec![],
200 where_or: vec![],
201 where_column: "".to_string(),
202 inc_dec: object! {},
203 page: -1,
204 limit: 10,
205 fields: object! {},
206 top: String::new(),
207 top2: String::new(),
208 order: object! {},
209 group: object! {},
210 distinct: false,
211 json: object! {},
212 sql: false,
213 join: Vec::new(),
214 join_inner: Vec::new(),
215 join_table: "".to_string(),
216 }
217 }
218 pub fn where_sql(&mut self) -> String {
219 let mut where_and_sql = vec![];
220 let mut where_or_sql = vec![];
221 let mut sql = vec![];
222
223 for item in self.where_or.iter() {
224 where_or_sql.push(item.clone());
225 }
226 if !where_or_sql.is_empty() {
227 sql.push(format!(" ( {} ) ", where_or_sql.join(" OR ")));
228 }
229
230 for item in self.where_and.iter() {
231 where_and_sql.push(item.clone());
232 }
233 if !where_and_sql.is_empty() {
234 sql.push(where_and_sql.join(" AND "));
235 }
236
237 if !self.where_column.is_empty() {
238 sql.push(self.where_column.clone());
239 }
240
241 if !sql.is_empty() {
242 return format!("WHERE {}", sql.join(" AND "));
243 }
244 "".to_string()
245 }
246 pub fn page_limit_sql(&mut self) -> String {
247 if self.page == -1 {
248 return "".to_string();
249 }
250 match self.mode.as_str() {
251 "mysql" => {
252 format!("LIMIT {},{}", self.page * self.limit - self.limit, self.limit)
253 }
254 "sqlite" => {
255 format!("LIMIT {} OFFSET {}", self.limit, self.page * self.limit - self.limit)
256 }
257 _ => "".to_string()
258 }
259 }
260 pub fn fields(&mut self) -> String {
261 let mut fields = vec![];
262 for (_, value) in self.fields.entries() {
263 match self.mode.as_str() {
264 "mssql" => {
265 fields.push(format!("{}", value));
266 }
267 _ => {
268 fields.push(format!("{}", value));
269 }
270 }
271 }
272 let fields = {
273 if fields.is_empty() {
274 "*".into()
275 } else {
276 fields.join(",")
277 }
278 };
279 match self.mode.as_str() {
280 "mysql" => {
281 fields.to_string()
282 }
283 "sqlite" => {
284 fields.to_string()
285 }
286 "mssql" => {
287 fields.to_string()
288 }
289 _ => fields.to_string()
290 }
291 }
292 pub fn top(&mut self) -> String {
293 match self.mode.as_str() {
294 "mssql" => {
295 let wheres = self.where_sql();
296 if !self.top2.is_empty() {
297 let order = self.order();
298 if order.is_empty() {
299 self.top = format!("(select ROW_NUMBER() OVER(ORDER BY rand()) as ROW,* from {} {}) as ", self.table, wheres);
300 } else {
301 self.top = format!("(select ROW_NUMBER() OVER({}) as ROW,* from {} {}) as ", order, self.table, wheres);
302 }
303 return self.top.to_string();
304 }
305 self.top.to_string()
306 }
307 _ => {
308 "".to_string()
309 }
310 }
311 }
312 pub fn top2(&mut self) -> String {
313 match self.mode.as_str() {
314 "mssql" => {
315 if self.where_and.is_empty() && self.where_or.is_empty() && !self.top2.is_empty() {
316 return format!("where {}", self.top2);
317 }
318 if (!self.where_and.is_empty() || !self.where_or.is_empty()) && !self.top2.is_empty() {
319 return format!("AND {}", self.top2);
320 }
321 self.top2.to_string()
322 }
323 _ => {
324 "".to_string()
325 }
326 }
327 }
328 pub fn table(&mut self) -> String {
329 match self.mode.as_str() {
330 "mssql" => {
331 if !self.top2.is_empty() {
332 return "t".to_string();
333 }
334 self.table.to_string()
335 }
336 _ => {
337 self.table.to_string()
338 }
339 }
340 }
341 pub fn join(&mut self) -> String {
342 match self.mode.as_str() {
343 "mssql" => {
344 let mut join = "".to_string();
345 for item in self.join.iter() {
346 join = format!("{} {}", join, item);
347 }
348 join.to_string()
349 }
350 _ => {
351 let mut join = "".to_string();
352 for item in self.join.iter() {
353 join = format!("{} {}", join, item);
354 }
355 join.to_string()
356 }
357 }
358 }
359
360 pub fn join_inner(&mut self) -> String {
362 match self.mode.as_str() {
363 "mysql" => {
364 let mut join_inner = "".to_string();
365 for item in self.join_inner.iter() {
366 join_inner = format!("{} {}", join_inner, item);
367 }
368 join_inner.to_string()
369 }
370 _ => {
371 "".to_string()
372 }
373 }
374 }
375
376
377 pub fn order(&mut self) -> String {
378 let mut sql = vec![];
379 for (field, item) in self.order.entries() {
380 match self.mode.as_str() {
381 "mssql" => {
382 sql.push(format!("{} {}", field, item));
383 }
384 "pgsql" => {
385 sql.push(format!("{} {}", field, item));
386 }
387 _ => {
388 sql.push(format!("`{}` {}", field, item));
389 }
390 }
391 }
392 if !sql.is_empty() {
393 return format!("ORDER BY {}", sql.join(","));
394 }
395 "".to_string()
396 }
397 pub fn group(&mut self) -> String {
398 let mut sql = vec![];
399 for (_, field) in self.group.entries() {
400 match self.mode.as_str() {
401 "mssql" => {
402 sql.push(format!("{}.{}", self.join_table, field));
403 }
404 "pgsql" => {
405 sql.push(format!("{}.{}", self.join_table, field));
406 }
407 _ => {
408 sql.push(format!("{}.`{}`", self.join_table, field));
409 }
410 }
411 }
412 if !sql.is_empty() {
413 return format!("GROUP BY {}", sql.join(","));
414 }
415 "".to_string()
416 }
417 pub fn distinct(&self) -> String {
418 if self.distinct {
419 "DISTINCT".to_string()
420 } else {
421 "".to_string()
422 }
423 }
424 pub fn select_sql(&mut self) -> String {
425 format!("SELECT {} {} FROM {} {} {} {} {} {} {} {} {}", self.distinct(), self.fields(), self.top(), self.table(), self.join(), self.join_inner(), self.where_sql(), self.top2(), self.group(), self.order(), self.page_limit_sql())
426 }
427}