br_fields/
table.rs

1use json::{JsonValue, object};
2use crate::Field;
3
4/// 关联表
5///
6/// * field 字段名
7/// * mode 模式 string
8/// * title 字段描述
9/// * length 字段总长度(含小数位)
10/// * default 默认值
11/// * empty 是否可空
12/// * dec 小数位
13pub struct Tree {
14    pub require: bool,
15    pub field: String,
16    pub mode: String,
17    pub title: String,
18    pub table: String,
19    pub fields: Vec<String>,
20    pub api: String,
21    pub show: bool,
22    pub describe: String,
23    pub def: String,
24    pub multiple: bool,
25    pub multiple_count: i32,
26    pub example: JsonValue,
27}
28
29impl Tree {
30    pub fn new(require: bool, field: &str, title: &str, table: &str, fields: Vec<&str>, api: &str) -> Self {
31        Self {
32            require,
33            field: field.to_string(),
34            mode: "tree".to_string(),
35            title: title.to_string(),
36            table: table.to_string(),
37            api: api.to_string(),
38            show: true,
39            fields: fields.iter().map(|c| c.to_string()).collect(),
40            describe: "".to_string(),
41            def: "".to_string(),
42            multiple: false,
43            multiple_count: 1,
44            example: JsonValue::Null,
45        }
46    }
47    /// 多选数量
48    pub fn multiple(&mut self, count: i32) -> &mut Tree {
49        self.multiple = count > 1;
50        self.multiple_count = count;
51        self
52    }
53}
54
55impl Field for Tree {
56    fn sql(&mut self, model: &str) -> String {
57        match model {
58            "sqlite" => {
59                format!("`{}` varchar({})   default ''", self.field, {
60                    if self.multiple {
61                        64 * self.multiple_count + self.multiple_count
62                    } else {
63                        64
64                    }
65                })
66            }
67            "pgsql" => {
68                let sql = format!("{} varchar({})   default ''", self.field, {
69                    if self.multiple {
70                        64 * self.multiple_count + self.multiple_count
71                    } else {
72                        64
73                    }
74                });
75                format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.title, self.require, self.multiple, self.multiple_count)
76            }
77            _ => {
78                let sql = format!("`{}` varchar({})   default ''", self.field, {
79                    if self.multiple {
80                        64 * self.multiple_count + self.multiple_count
81                    } else {
82                        64
83                    }
84                });
85                format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.title, self.require, self.multiple, self.multiple_count)
86            }
87        }
88    }
89    fn hide(&mut self) -> &mut Self {
90        self.show = false;
91        self
92    }
93    fn describe(&mut self, text: &str) -> &mut Self {
94        self.describe = text.to_string();
95        self
96    }
97
98    fn field(&mut self) -> JsonValue {
99        let mut field = object! {};
100        field.insert("require", JsonValue::from(self.require)).unwrap();
101        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
102        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
103        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
104        field.insert("table", JsonValue::from(self.table.clone())).unwrap();
105        field.insert("api", JsonValue::from(self.api.clone())).unwrap();
106        field.insert("fields", JsonValue::from(self.fields.clone())).unwrap();
107
108        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
109
110        field.insert("multiple", JsonValue::from(self.multiple)).unwrap();
111        field.insert("multiple_count", JsonValue::from(self.multiple_count)).unwrap();
112
113        field.insert("show", JsonValue::from(self.show)).unwrap();
114        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
115        field.insert("example", self.example.clone()).unwrap();
116        field
117    }
118
119    fn swagger(&mut self) -> JsonValue {
120        object! {
121            "type": self.mode.clone(),
122            "example": self.example.clone(),
123        }
124    }
125
126    fn example(&mut self, data: JsonValue) ->&mut Self {
127        self.example = data.clone();
128        self
129    }
130}
131
132/// 关联表
133///
134/// * field 字段名
135/// * mode 模式 string
136/// * title 字段描述
137/// * length 字段总长度(含小数位)
138/// * default 默认值
139/// * empty 是否可空
140/// * dec 小数位
141pub struct Table {
142    pub require: bool,
143    pub field: String,
144    pub mode: String,
145    pub title: String,
146    pub table: String,
147    pub fields: Vec<String>,
148    pub api: String,
149    pub show: bool,
150    pub describe: String,
151    pub def: String,
152    pub multiple: bool,
153    pub multiple_count: i32,
154    pub example: JsonValue,
155}
156
157impl Table {
158    pub fn new(require: bool, field: &str, title: &str, table: &str, fields: Vec<&str>, api: &str) -> Self {
159        Self {
160            require,
161            field: field.to_string(),
162            mode: "table".to_string(),
163            title: title.to_string(),
164            table: table.to_string(),
165            api: api.to_string(),
166            show: true,
167            fields: fields.iter().map(|c| c.to_string()).collect(),
168            describe: "".to_string(),
169            def: "".to_string(),
170            multiple: false,
171            multiple_count: 1,
172            example: JsonValue::Null,
173        }
174    }
175    /// 多选数量
176    pub fn multiple(&mut self, count: i32) -> &mut Table {
177        self.multiple = count > 1;
178        self.multiple_count = count;
179        self
180    }
181}
182
183impl Field for Table {
184    fn sql(&mut self, model: &str) -> String {
185        match model {
186            "sqlite" => {
187                format!("`{}` varchar({})   default ''", self.field, {
188                    if self.multiple {
189                        64 * self.multiple_count + self.multiple_count
190                    } else {
191                        64
192                    }
193                })
194            }
195            "pgsql" => {
196                let sql = format!("{} varchar({})   default ''", self.field, {
197                    if self.multiple {
198                        64 * self.multiple_count + self.multiple_count
199                    } else {
200                        64
201                    }
202                });
203                format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.title, self.require, self.multiple, self.multiple_count)
204            }
205            _ => {
206                let sql = format!("`{}` varchar({})   default ''", self.field, {
207                    if self.multiple {
208                        64 * self.multiple_count + self.multiple_count
209                    } else {
210                        64
211                    }
212                });
213                format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.title, self.require, self.multiple, self.multiple_count)
214            }
215        }
216    }
217    fn hide(&mut self) -> &mut Self {
218        self.show = false;
219        self
220    }
221    fn describe(&mut self, text: &str) -> &mut Self {
222        self.describe = text.to_string();
223        self
224    }
225
226    fn field(&mut self) -> JsonValue {
227        let mut field = object! {};
228        field.insert("require", JsonValue::from(self.require)).unwrap();
229        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
230        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
231        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
232        field.insert("table", JsonValue::from(self.table.clone())).unwrap();
233        field.insert("api", JsonValue::from(self.api.clone())).unwrap();
234        field.insert("fields", JsonValue::from(self.fields.clone())).unwrap();
235
236        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
237
238        field.insert("multiple", JsonValue::from(self.multiple)).unwrap();
239        field.insert("multiple_count", JsonValue::from(self.multiple_count)).unwrap();
240
241        field.insert("show", JsonValue::from(self.show)).unwrap();
242        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
243        field.insert("example", self.example.clone()).unwrap();
244        field
245    }
246
247    fn swagger(&mut self) -> JsonValue {
248        object! {
249            "type": self.mode.clone(),
250            "example": self.example.clone(),
251        }
252    }
253    fn example(&mut self, data: JsonValue) ->&mut Self {
254        self.example = data;
255        self
256    }
257}