1use json::{JsonValue, object};
2
3pub trait Field {
4 fn sql(&mut self) -> String;
5 fn info(&mut self) -> JsonValue;
6}
7
8pub struct Key {
17 pub field: String,
18 pub mode: String,
19 pub title: String,
20 pub length: i32,
21 pub auto: bool,
22}
23
24impl Key {
25 pub fn field(field: &str, title: &str, length: i32, auto: bool) -> Self {
26 Self {
27 field: field.to_string(),
28 mode: "key".to_string(),
29 title: title.to_string(),
30 length,
31 auto,
32 }
33 }
34}
35
36impl Field for Key {
37 fn sql(&mut self) -> String {
38 let mut sql = "".to_string();
39 if self.auto {
40 sql = format!("{}{} int({})", sql.clone(), self.field, self.length);
41 sql = format!("{} not null AUTO_INCREMENT", sql.clone());
42 } else {
43 sql = format!("{}{} varchar({})", sql.clone(), self.field, self.length);
44 sql = format!("{}", sql.clone());
45 }
46 sql = format!("{} comment '{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.length, self.auto);
48 return sql.clone();
49 }
50
51 fn info(&mut self) -> JsonValue {
52 let mut data = object! {};
53 data["field"] = self.field.clone().into();
54 data["mode"] = self.mode.clone().into();
55 data["title"] = self.title.clone().into();
56 data["length"] = self.length.into();
57 data["auto"] = self.auto.into();
58 return data;
59 }
60}
61
62
63pub struct Str {
64 pub require: bool,
65 pub field: String,
66 pub mode: String,
67 pub title: String,
68 pub def: String,
69 pub length: i32,
70}
71
72impl Str {
73 pub fn string(require: bool, field: &str, title: &str, length: i32, default: &str) -> Self {
82 Self {
83 require,
84 field: field.to_string(),
85 mode: "string".to_string(),
86 title: title.to_string(),
87 def: default.to_string(),
88 length,
89 }
90 }
91 pub fn pass(require: bool, field: &str, title: &str, length: i32, default: &str) -> Self {
100 Self {
101 require,
102 field: field.to_string(),
103 mode: "pass".to_string(),
104 title: title.to_string(),
105 def: default.to_string(),
106 length,
107 }
108 }
109}
110
111impl Field for Str {
112 fn sql(&mut self) -> String {
113 let mut sql = format!("{} varchar({})", self.field, self.length);
114 if self.require {
115 sql = format!("{} not null", sql.clone())
116 };
117 sql = format!("{} default '{}'", sql.clone(), self.def);
118 sql = format!("{} comment '{}|{}|{}'", sql.clone(), self.title, self.mode, self.require);
119 return sql.clone();
120 }
121 fn info(&mut self) -> JsonValue {
122 let mut data = object! {};
123 data["require"] = self.require.into();
124 data["field"] = self.field.clone().into();
125 data["mode"] = self.mode.clone().into();
126 data["title"] = self.title.clone().into();
127 data["length"] = self.length.into();
128 data["def"] = self.def.clone().into();
129 return data;
130 }
131}
132
133
134pub struct Number {
135 pub require: bool,
136 pub field: String,
137 pub mode: String,
138 pub title: String,
139 pub def: f32,
140 pub length: i32,
141 pub dec: i32,
142}
143
144impl Number {
145 pub fn field(require: bool, field: &str, title: &str, length: i32, dec: i32, default: f32) -> Self {
155 Self {
156 require,
157 field: field.to_string(),
158 mode: "number".to_string(),
159 title: title.to_string(),
160 def: default,
161 length,
162 dec,
163 }
164 }
165}
166
167impl Field for Number {
168 fn sql(&mut self) -> String {
169 let mut sql = format!("{} decimal({},{})", self.field, self.length, self.dec);
170 if self.require {
171 sql = format!("{} not null", sql.clone())
172 };
173 sql = format!("{} default {1:.width$}", sql.clone(), self.def, width = self.dec as usize);
174 sql = format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.length, self.dec, self.def);
175 return sql.clone();
176 }
177
178 fn info(&mut self) -> JsonValue {
179 let mut data = object! {};
180 data["require"] = self.require.into();
181 data["field"] = self.field.clone().into();
182 data["mode"] = self.mode.clone().into();
183 data["title"] = self.title.clone().into();
184 data["length"] = self.length.into();
185 data["def"] = self.def.into();
186 data["dec"] = self.dec.into();
187 return data;
188 }
189}
190
191
192pub struct Table {
193 pub require: bool,
194 pub field: String,
195 pub mode: String,
196 pub title: String,
197 pub table: String,
198 pub def: String,
199 pub fields: String,
200 pub api: String,
201}
202
203impl Table {
204 pub fn field(require: bool, field: &str, title: &str, table: &str, fields: &str, default: &str, api: &str) -> Self {
214 Self {
215 require,
216 field: field.to_string(),
217 mode: "table".to_string(),
218 title: title.to_string(),
219 def: default.to_string(),
220 table: table.to_string(),
221 fields: fields.to_string(),
222 api: api.to_string(),
223 }
224 }
225}
226
227impl Field for Table {
228 fn sql(&mut self) -> String {
229 let mut sql = format!("{} text", self.field);
230 sql = format!("{} comment '{}|{}|{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.table, self.fields, self.api, self.def);
231 return sql.clone();
232 }
233
234 fn info(&mut self) -> JsonValue {
235 let mut data = object! {};
236 data["require"] = self.require.into();
237 data["field"] = self.field.clone().into();
238 data["mode"] = self.mode.clone().into();
239 data["title"] = self.title.clone().into();
240 data["def"] = self.def.clone().into();
241 data["fields"] = self.fields.clone().into();
242 data["api"] = self.api.clone().into();
243 data["table"] = self.table.clone().into();
244 return data;
245 }
246}
247
248
249pub struct Switch {
250 pub require: bool,
251 pub field: String,
252 pub mode: String,
253 pub title: String,
254 pub def: bool,
255}
256
257impl Switch {
258 pub fn field(require: bool, field: &str, title: &str, default: bool) -> Self {
266 Self {
267 require,
268 field: field.to_string(),
269 mode: "switch".to_string(),
270 title: title.to_string(),
271 def: default,
272 }
273 }
274}
275
276impl Field for Switch {
277 fn sql(&mut self) -> String {
278 let mut sql = format!("{} bool", self.field);
279 if self.require {
280 sql = format!("{} not null", sql.clone())
281 };
282 sql = format!("{} default {}", sql.clone(), self.def);
283 sql = format!("{} comment '{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.def);
284 return sql.clone();
285 }
286
287 fn info(&mut self) -> JsonValue {
288 let mut data = object! {};
289 data["require"] = self.require.into();
290 data["field"] = self.field.clone().into();
291 data["mode"] = self.mode.clone().into();
292 data["title"] = self.title.clone().into();
293 data["def"] = self.def.into();
294 return data;
295 }
296}
297
298
299pub struct Date {
300 pub require: bool,
301 pub field: String,
302 pub mode: String,
303 pub title: String,
304 pub def: String,
305}
306
307impl Date {
308 pub fn year(require: bool, field: &str, title: &str, default: &str) -> Self {
309 Self {
310 field: field.to_string(),
311 mode: "year".to_string(),
312 title: title.to_string(),
313 def: default.to_string(),
314 require,
315 }
316 }
317 pub fn datetime(require: bool, field: &str, title: &str, mut default: &str) -> Self {
325 if default == "" {
326 default = "0000-01-01 00:00:00"
327 }
328 Self {
329 field: field.to_string(),
330 mode: "datetime".to_string(),
331 title: title.to_string(),
332 def: default.to_string(),
333 require,
334 }
335 }
336 pub fn date(require: bool, field: &str, title: &str, mut default: &str) -> Self {
337 if default == "" {
338 default = "0000-01-01"
339 }
340 Self {
341 field: field.to_string(),
342 mode: "date".to_string(),
343 title: title.to_string(),
344 def: default.to_string(),
345 require,
346 }
347 }
348 pub fn time(require: bool, field: &str, title: &str, default: &str) -> Self {
349 Self {
350 field: field.to_string(),
351 mode: "time".to_string(),
352 title: title.to_string(),
353 def: default.to_string(),
354 require,
355 }
356 }
357}
358
359impl Field for Date {
360 fn sql(&mut self) -> String {
361 let mut sql = format!("{}", self.field);
362 match self.mode.as_str() {
363 "year" => {
364 sql = format!("{} year", sql.clone())
365 }
366 "date" => {
367 sql = format!("{} date", sql.clone())
368 }
369 "time" => {
370 sql = format!("{} time", sql.clone())
371 }
372 "datetime" => {
373 sql = format!("{} datetime", sql.clone())
374 }
375 _ => {
376 sql = format!("{} datetime", sql.clone())
377 }
378 }
379 if self.require {
380 sql = format!("{} not null default '{}'", sql.clone(), self.def);
381 } else {
382 sql = format!("{} null", sql.clone())
383 }
384 sql = format!("{} comment '{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.def);
385 return sql.clone();
386 }
387
388 fn info(&mut self) -> JsonValue {
389 let mut data = object! {};
390 data["require"] = self.require.into();
391 data["field"] = self.field.clone().into();
392 data["mode"] = self.mode.clone().into();
393 data["title"] = self.title.clone().into();
394 data["def"] = self.def.clone().into();
395 return data;
396 }
397}
398
399
400pub struct Timestamp {
401 pub require: bool,
402 pub field: String,
403 pub mode: String,
404 pub title: String,
405 pub def: f64,
406 pub dec: i32,
407}
408
409impl Timestamp {
410 pub fn timestamp(require: bool, field: &str, title: &str, dec: i32, default: f64) -> Self {
411 Self {
412 require,
413 field: field.to_string(),
414 mode: "timestamp".to_string(),
415 title: title.to_string(),
416 def: default,
417 dec,
418 }
419 }
420}
421
422impl Field for Timestamp {
423 fn sql(&mut self) -> String {
424 let max = 10 + self.dec;
425
426 let mut sql = format!("{}", self.field);
427 sql = format!("{} float({},{})", sql.clone(), max, self.dec);
428 if self.require {
429 sql = format!("{} not null", sql.clone())
430 };
431 self.def = format!("{0:.width$}", self.def, width = self.dec as usize).parse::<f64>().unwrap();
432 sql = format!("{} default {}", sql.clone(), self.def);
433 sql = format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.dec, self.def);
434 return sql.clone();
435 }
436
437 fn info(&mut self) -> JsonValue {
438 let mut data = object! {};
439 data["require"] = self.require.into();
440 data["field"] = self.field.clone().into();
441 data["mode"] = self.mode.clone().into();
442 data["title"] = self.title.clone().into();
443 data["def"] = self.def.clone().into();
444 data["dec"] = self.dec.into();
445 return data;
446 }
447}
448
449
450pub struct Select {
451 pub require: bool,
452 pub field: String,
453 pub mode: String,
454 pub title: String,
455 pub def: JsonValue,
456 pub option: JsonValue,
457 pub multiple: bool,
458}
459
460impl Select {
461 pub fn field(require: bool, field: &str, title: &str, option: JsonValue, multiple: bool, default: JsonValue) -> Self {
462 Self {
463 require,
464 field: field.to_string(),
465 mode: "select".to_string(),
466 title: title.to_string(),
467 def: default,
468 option,
469 multiple,
470 }
471 }
472}
473
474impl Field for Select {
475 fn sql(&mut self) -> String {
476 let mut sql = format!("{}", self.field);
477 let mut options = String::new();
478 if !self.require {
479 self.option.push("").unwrap();
480 }
481 for item in self.option.members() {
482 if options == "" {
483 options = format!("'{}'", item);
484 } else {
485 options = format!("{},'{}'", options, item);
486 }
487 }
488 if self.multiple {
489 sql = format!("{} set({}) default '{}'", sql.clone(), options, self.def);
490 } else {
491 sql = format!("{} enum({}) default '{}'", sql.clone(), options, self.def);
492 }
493 sql = format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.multiple, self.def);
494 return sql.clone();
495 }
496
497 fn info(&mut self) -> JsonValue {
498 let mut data = object! {};
499 data["require"] = self.require.into();
500 data["field"] = self.field.clone().into();
501 data["mode"] = self.mode.clone().into();
502 data["title"] = self.title.clone().into();
503 data["def"] = self.def.clone().into();
504 data["multiple"] = self.multiple.into();
505 data["option"] = self.option.clone().into();
506 return data;
507 }
508}
509
510
511pub struct File {
512 pub require: bool,
513 pub field: String,
514 pub mode: String,
515 pub title: String,
516 pub length: i32,
517 pub def: String,
518}
519
520impl File {
521 pub fn field(require: bool, field: &str, title: &str, length: i32, default: &str) -> Self {
530 Self {
531 require,
532 field: field.to_string(),
533 mode: "file".to_string(),
534 title: title.to_string(),
535 def: default.to_string(),
536 length,
537 }
538 }
539}
540
541impl Field for File {
542 fn sql(&mut self) -> String {
543 let mut sql = format!("{} text", self.field);
544 sql = format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.length, self.def);
545 return sql.clone();
546 }
547
548 fn info(&mut self) -> JsonValue {
549 let mut data = object! {};
550 data["require"] = self.require.into();
551 data["field"] = self.field.clone().into();
552 data["mode"] = self.mode.clone().into();
553 data["title"] = self.title.clone().into();
554 data["def"] = self.def.clone().into();
555 data["length"] = self.length.clone().into();
556 return data;
557 }
558}
559
560pub struct Text {
561 pub require: bool,
562 pub field: String,
563 pub mode: String,
564 pub title: String,
565 pub def: String,
566}
567
568impl Text {
569 pub fn field(require: bool, field: &str, title: &str, default: &str) -> Self {
578 Self {
579 require,
580 field: field.to_string(),
581 mode: "text".to_string(),
582 title: title.to_string(),
583 def: default.to_string(),
584 }
585 }
586}
587
588impl Field for Text {
589 fn sql(&mut self) -> String {
590 let mut sql = format!("{} text", self.field);
591 sql = format!("{} comment '{}|{}|{}'", sql.clone(), self.title, self.mode, self.def);
592 return sql.clone();
593 }
594
595 fn info(&mut self) -> JsonValue {
596 let mut data = object! {};
597 data["require"] = self.require.into();
598 data["field"] = self.field.clone().into();
599 data["mode"] = self.mode.clone().into();
600 data["title"] = self.title.clone().into();
601 data["def"] = self.def.clone().into();
602 return data;
603 }
604}