1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use json::{JsonValue, object};
use crate::Field;

pub struct Float {
    pub require: bool,
    pub field: String,
    pub mode: String,
    pub title: String,
    pub def: f32,
    pub length: i32,
    pub dec: usize,
}

impl Float {
    /// 数字
    ///
    /// * field 字段名
    /// * mode 模式 string
    /// * title 字段描述
    /// * length 字段总长度(含小数位)
    /// * default 默认值
    /// * dec 小数位
    pub fn new(require: bool, field: &str, title: &str, length: i32, dec: usize, default: f32) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "float".to_string(),
            title: title.to_string(),
            def: default,
            length,
            dec,
        }
    }
}

impl Field for Float {
    fn sql(&mut self, model: &str) -> String {
        let mut sql = format!("{} decimal({},{})", self.field, self.length, self.dec);
        if self.require {
            sql = format!("{} not null", sql.clone())
        };
        sql = format!("{} default {1:.width$}", sql.clone(), self.def, width = self.dec);
        match model {
            "sqlite" => sql,
            _ => format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.length, self.dec, self.def)
        }
    }
    fn field(&mut self) -> JsonValue {
        let mut field = object! {};
        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
        field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
        field
    }
}