Skip to main content

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