1use json::{JsonValue, object};
2use crate::{Field};
3
4pub struct Int {
14 pub require: bool,
15 pub field: String,
16 pub mode: String,
17 pub title: String,
18 pub def: i64,
19 pub length: i32,
20 pub dec: String,
21}
22
23impl Int {
24 pub fn new(require: bool, field: &str, title: &str, length: i32, default: i64) -> Self {
25 Self {
26 require,
27 field: field.to_string(),
28 mode: "int".to_string(),
29 title: title.to_string(),
30 def: default,
31 length,
32 dec: "".to_string(),
33 }
34 }
35}
36
37impl Field for Int {
38 fn sql(&mut self, model: &str) -> String {
39 let mut sql = format!("{} int({})", self.field, self.length);
40 if self.require {
41 sql = format!("{} not null", sql.clone())
42 };
43 sql = format!("{} default {}", sql.clone(), self.def);
44 match model {
45 "sqlite" => sql,
46 _ => format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def, self.dec)
47 }
48 }
49 fn field(&mut self) -> JsonValue {
50 let mut field = object! {};
51 field.insert("require", JsonValue::from(self.require.clone())).unwrap();
52 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
53 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
54 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
55 field.insert("length", JsonValue::from(self.length.clone())).unwrap();
56 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
57 field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
58 field
59 }
60}
61
62
63pub struct AutoKey {
72 pub require: bool,
73 pub field: String,
74 pub mode: String,
75 pub title: String,
76 pub length: i32,
77}
78
79impl AutoKey {
80 pub fn new(require: bool, field: &str, title: &str) -> Self {
81 Self {
82 require,
83 field: field.to_string(),
84 mode: "autokey".to_string(),
85 title: title.to_string(),
86 length: 0,
87 }
88 }
89}
90
91impl Field for AutoKey {
92 fn sql(&mut self, model: &str) -> String {
93 let mut sql = "".to_string();
94 match model {
96 "sqlite" => {
97 sql = format!("{}{} INTEGER PRIMARY KEY", sql.clone(), self.field);
98 sql = format!("{} AUTOINCREMENT", sql.clone());
99 return sql;
100 }
101 _ => {
102 sql = format!("{}{} int({})", sql.clone(), self.field, self.length);
103 sql = format!("{} not null AUTO_INCREMENT", sql.clone());
104 format!("{} comment '{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length)
105 }
106 }
107 }
108 fn field(&mut self) -> JsonValue {
109 let mut field = object! {};
110 field.insert("require", JsonValue::from(self.require.clone())).unwrap();
111 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
112 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
113 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
114 field.insert("length", JsonValue::from(self.length.clone())).unwrap();
115 field
116 }
117}
118
119
120pub struct Switch {
128 pub require: bool,
129 pub field: String,
130 pub mode: String,
131 pub title: String,
132 pub def: bool,
133}
134
135impl Switch {
136 pub fn new(require: bool, field: &str, title: &str, default: bool) -> Self {
137 Self {
138 require,
139 field: field.to_string(),
140 mode: "switch".to_string(),
141 title: title.to_string(),
142 def: default,
143 }
144 }
145}
146
147impl Field for Switch {
148 fn sql(&mut self, model: &str) -> String {
149 let mut sql = format!("{} bool", self.field);
150 if self.require {
151 sql = format!("{} not null", sql.clone())
152 };
153 sql = format!("{} default {}", sql.clone(), self.def);
154 match model {
155 "sqlite" => sql,
156 _ => format!("{} comment '{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.def)
157 }
158 }
159 fn field(&mut self) -> JsonValue {
160 let mut field = object! {};
161 field.insert("require", JsonValue::from(self.require.clone())).unwrap();
162 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
163 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
164 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
165 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
166 field
167 }
168}