Skip to main content

br_fields/
table.rs

1use crate::Field;
2use json::{object, JsonValue};
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 pid_field: String,
20    pub fields: Vec<String>,
21    pub api: String,
22    pub show: bool,
23    pub describe: String,
24    pub def: String,
25    pub multiple: bool,
26    pub multiple_count: i32,
27    pub example: JsonValue,
28}
29
30impl Tree {
31    pub fn new(
32        require: bool,
33        field: &str,
34        title: &str,
35        table: &str,
36        pid_field: &str,
37        fields: Vec<&str>,
38        api: &str,
39    ) -> Self {
40        Self {
41            require,
42            field: field.to_string(),
43            mode: "tree".to_string(),
44            title: title.to_string(),
45            table: table.to_string(),
46            api: api.to_string(),
47            show: true,
48            fields: fields.iter().map(|c| c.to_string()).collect(),
49            describe: "".to_string(),
50            def: "".to_string(),
51            multiple: false,
52            multiple_count: 1,
53            pid_field: pid_field.to_string(),
54            example: JsonValue::Null,
55        }
56    }
57    /// 多选数量
58    pub fn multiple(&mut self, count: i32) -> &mut Tree {
59        self.multiple = count > 1;
60        self.multiple_count = count;
61        self
62    }
63}
64
65impl Field for Tree {
66    fn sql(&mut self, model: &str) -> String {
67        let not_null = if self.require { " not null" } else { "" };
68        let length = if self.multiple {
69            64 * self.multiple_count + self.multiple_count
70        } else {
71            64
72        };
73        match model {
74            "sqlite" => {
75                format!(
76                    "`{}` varchar({}){} default ''",
77                    self.field, length, not_null
78                )
79            }
80            "pgsql" => {
81                let sql = format!(r#""{}" varchar({}) default ''"#, self.field, length);
82                format!(
83                    "{} --{}|{}|{}|{}|{}|{}",
84                    sql,
85                    self.mode,
86                    self.title,
87                    self.pid_field,
88                    self.require,
89                    self.multiple,
90                    self.multiple_count
91                )
92            }
93            _ => {
94                let sql = format!(
95                    "`{}` varchar({}){} default ''",
96                    self.field, length, not_null
97                );
98                format!(
99                    "{} comment '{}|{}|{}|{}|{}|{}'",
100                    sql,
101                    self.mode,
102                    self.title,
103                    self.pid_field,
104                    self.require,
105                    self.multiple,
106                    self.multiple_count
107                )
108            }
109        }
110    }
111    fn hide(&mut self) -> &mut Self {
112        self.show = false;
113        self
114    }
115    fn describe(&mut self, text: &str) -> &mut Self {
116        self.describe = text.to_string();
117        self
118    }
119
120    fn field(&mut self) -> JsonValue {
121        let mut field = object! {};
122        field
123            .insert("require", JsonValue::from(self.require))
124            .unwrap();
125        field
126            .insert("field", JsonValue::from(self.field.clone()))
127            .unwrap();
128        field
129            .insert("mode", JsonValue::from(self.mode.clone()))
130            .unwrap();
131        field
132            .insert("title", JsonValue::from(self.title.clone()))
133            .unwrap();
134        field
135            .insert("table", JsonValue::from(self.table.clone()))
136            .unwrap();
137        field
138            .insert("api", JsonValue::from(self.api.clone()))
139            .unwrap();
140        field
141            .insert("fields", JsonValue::from(self.fields.clone()))
142            .unwrap();
143
144        field
145            .insert("def", JsonValue::from(self.def.clone()))
146            .unwrap();
147
148        field
149            .insert("multiple", JsonValue::from(self.multiple))
150            .unwrap();
151        field
152            .insert("multiple_count", JsonValue::from(self.multiple_count))
153            .unwrap();
154
155        field
156            .insert("pid_field", JsonValue::from(self.pid_field.clone()))
157            .unwrap();
158
159        field.insert("show", JsonValue::from(self.show)).unwrap();
160        field
161            .insert("describe", JsonValue::from(self.describe.clone()))
162            .unwrap();
163        field.insert("example", self.example.clone()).unwrap();
164        field
165    }
166
167    fn swagger(&mut self) -> JsonValue {
168        object! {
169            "type": self.mode.clone(),
170            "example": self.example.clone(),
171        }
172    }
173
174    fn example(&mut self, data: JsonValue) -> &mut Self {
175        self.example = data.clone();
176        self
177    }
178}
179
180/// 关联表
181///
182/// * field 字段名
183/// * mode 模式 string
184/// * title 字段描述
185/// * length 字段总长度(含小数位)
186/// * default 默认值
187/// * empty 是否可空
188/// * dec 小数位
189#[derive(Debug, Clone)]
190pub struct Table {
191    pub require: bool,
192    pub field: String,
193    pub mode: String,
194    pub title: String,
195    pub table: String,
196    pub fields: Vec<String>,
197    pub api: String,
198    pub show: bool,
199    pub describe: String,
200    pub def: String,
201    pub multiple: bool,
202    pub multiple_count: i32,
203    pub example: JsonValue,
204}
205
206impl Table {
207    pub fn new(
208        require: bool,
209        field: &str,
210        title: &str,
211        table: &str,
212        fields: Vec<&str>,
213        api: &str,
214    ) -> Self {
215        Self {
216            require,
217            field: field.to_string(),
218            mode: "table".to_string(),
219            title: title.to_string(),
220            table: table.to_string(),
221            api: api.to_string(),
222            show: true,
223            fields: fields.iter().map(|c| c.to_string()).collect(),
224            describe: "".to_string(),
225            def: "".to_string(),
226            multiple: false,
227            multiple_count: 1,
228            example: JsonValue::Null,
229        }
230    }
231    /// 多选数量
232    pub fn multiple(&mut self, count: i32) -> &mut Table {
233        self.multiple = count > 1;
234        self.multiple_count = count;
235        self
236    }
237}
238
239impl Field for Table {
240    fn sql(&mut self, model: &str) -> String {
241        let not_null = if self.require { " not null" } else { "" };
242        let length = if self.multiple {
243            64 * self.multiple_count + self.multiple_count
244        } else {
245            64
246        };
247        match model {
248            "sqlite" => {
249                format!(
250                    "`{}` varchar({}){} default ''",
251                    self.field, length, not_null
252                )
253            }
254            "pgsql" => {
255                let sql = format!(r#""{}" varchar({}) default ''"#, self.field, length);
256                format!(
257                    "{} --{}|{}|{}|{}|{}",
258                    sql, self.mode, self.title, self.require, self.multiple, self.multiple_count
259                )
260            }
261            _ => {
262                let sql = format!(
263                    "`{}` varchar({}){} default ''",
264                    self.field, length, not_null
265                );
266                format!(
267                    "{} comment '{}|{}|{}|{}|{}'",
268                    sql, self.mode, self.title, self.require, self.multiple, self.multiple_count
269                )
270            }
271        }
272    }
273    fn hide(&mut self) -> &mut Self {
274        self.show = false;
275        self
276    }
277    fn describe(&mut self, text: &str) -> &mut Self {
278        self.describe = text.to_string();
279        self
280    }
281
282    fn field(&mut self) -> JsonValue {
283        let mut field = object! {};
284        field
285            .insert("require", JsonValue::from(self.require))
286            .unwrap();
287        field
288            .insert("field", JsonValue::from(self.field.clone()))
289            .unwrap();
290        field
291            .insert("mode", JsonValue::from(self.mode.clone()))
292            .unwrap();
293        field
294            .insert("title", JsonValue::from(self.title.clone()))
295            .unwrap();
296        field
297            .insert("table", JsonValue::from(self.table.clone()))
298            .unwrap();
299        field
300            .insert("api", JsonValue::from(self.api.clone()))
301            .unwrap();
302        field
303            .insert("fields", JsonValue::from(self.fields.clone()))
304            .unwrap();
305
306        field
307            .insert("def", JsonValue::from(self.def.clone()))
308            .unwrap();
309
310        field
311            .insert("multiple", JsonValue::from(self.multiple))
312            .unwrap();
313        field
314            .insert("multiple_count", JsonValue::from(self.multiple_count))
315            .unwrap();
316
317        field.insert("show", JsonValue::from(self.show)).unwrap();
318        field
319            .insert("describe", JsonValue::from(self.describe.clone()))
320            .unwrap();
321        field.insert("example", self.example.clone()).unwrap();
322        field
323    }
324
325    fn swagger(&mut self) -> JsonValue {
326        object! {
327            "type": self.mode.clone(),
328            "example": self.example.clone(),
329        }
330    }
331    fn example(&mut self, data: JsonValue) -> &mut Self {
332        self.example = data;
333        self
334    }
335}