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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use std::collections::HashMap;
use std::fs;
use std::path::{PathBuf};
use json::{JsonValue, object};

#[derive(Clone, Debug)]
pub struct Config {
    pub default: String,
    pub connections: HashMap<String, Connection>,
}

impl Config {
    pub fn default() -> Config {
        let mut connections = HashMap::new();
        connections.insert("mydbname".to_string(), Connection::default());
        Self {
            default: "mydbname".to_string(),
            connections,
        }
    }
    pub fn json(&mut self) -> JsonValue {
        let mut data = object! {};
        data["default"] = self.default.clone().into();
        let mut connections = object! {};
        for (name, connection) in self.connections.iter_mut() {
            connections[name.clone()] = connection.json().clone();
        }
        data["connections"] = connections;
        data
    }
    pub fn from(data: JsonValue) -> Config {
        let default = data["default"].to_string();
        let mut connections = HashMap::new();
        for (key, value) in data["connections"].entries() {
            let connection = Connection::default().from(value.clone()).clone();
            connections.insert(key.to_string(), connection.clone());
        }
        Self {
            default,
            connections,
        }
    }
    pub fn create(path: &str) -> JsonValue {
        let config = Config::default().json();
        let path_buf = PathBuf::from(path);
        fs::create_dir_all(path.trim_end_matches(path_buf.file_name().unwrap().to_str().unwrap())).unwrap();
        fs::write(path, config.to_string()).unwrap();
        config
    }
}
/// 数据库模式
#[derive(Clone, Debug)]
pub enum Mode {
    Mysql,
    Mssql,
    Sqlite,
    None,
}

impl Mode {
    pub fn str(&mut self) -> &'static str {
        match self {
            Mode::Mysql => "mysql",
            Mode::Sqlite => "sqlite",
            Mode::Mssql => "mssql",
            Mode::None => ""
        }
    }
    pub fn from(name: &str) -> Self {
        match name {
            "mysql" => Mode::Mysql,
            "sqlite" => Mode::Sqlite,
            "mssql" => Mode::Mssql,
            _ => Mode::None
        }
    }
}
/// 数据库连接
#[derive(Clone, Debug)]
pub struct Connection {
    pub mode: Mode,
    /// 连接地址
    pub hostname: String,
    /// 连接端口
    pub hostport: String,
    /// 数据库名称
    pub database: String,
    /// 账号
    pub username: String,
    /// 密码
    pub userpass: String,
    pub params: Vec<String>,
    /// 数据库语言
    pub charset: Charset,
    /// 表前缀
    pub prefix: String,
    /// 调试开关
    pub debug: bool,
    /// 备份时间
    /// * * * * * command to be executed
    /// - - - - -
    /// | | | | |
    /// | | | | +----- 星期几 (0 - 7) (0 和 7 都代表星期日)
    /// | | | +------- 月份 (1 - 12)
    /// | | +--------- 日期 (1 - 31)
    /// | +----------- 小时 (0 - 23)
    /// +------------- 分钟 (0 - 59)
    ///
    ///     •	*:代表任何值。
    /// 	•	,:用于分隔多个值。
    /// 	•	-:用于表示范围。
    /// 	•	/:用于指定步长。
    pub backup_time: String,
    /// 备份路径
    pub backup_path: PathBuf,
    /// 备份保留数量
    pub backup_number: u8,
}

impl Connection {
    pub fn default() -> Connection {
        Self {
            mode: Mode::Sqlite,
            hostname: "".to_string(),
            hostport: "".to_string(),
            database: "".to_string(),
            username: "".to_string(),
            userpass: "".to_string(),
            params: vec![],
            charset: Charset::Utf8mb4,
            prefix: "".to_string(),
            debug: false,
            backup_time: "".to_string(),
            backup_path: Default::default(),
            backup_number: 0,
        }
    }
    pub fn json(&mut self) -> JsonValue {
        object! {
            mode: self.mode.str(),
            hostname: self.hostname.clone(),
            hostport: self.hostport.clone(),
            database: self.database.clone(),
            username: self.username.clone(),
            userpass:self.userpass.clone(),
            params: self.params.clone(),
            charset: self.charset.str(),
            prefix: self.prefix.clone(),
            debug: self.debug.clone(),
            backup_time: self.backup_time.to_string(),
            backup_path: self.backup_path.as_path().to_str().unwrap(),
            backup_number: self.backup_number,
        }
    }
    pub fn from(&mut self, data: JsonValue) -> &mut Connection {
        self.mode = Mode::from(data["mode"].as_str().unwrap());
        self.hostname = data["hostname"].to_string();
        self.hostport = data["hostport"].to_string();
        self.database = data["database"].to_string();
        self.username = data["username"].to_string();
        self.userpass = data["userpass"].to_string();
        self.params = data["params"].members().map(|x| x.to_string()).collect();
        self.charset = Charset::from(data["charset"].as_str().unwrap_or("utf8mb4"));
        self.prefix = data["prefix"].as_str().unwrap_or("").to_string();
        self.debug = data["debug"].to_string().parse::<bool>().unwrap_or(false).into();
        self.backup_time = data["backup_time"].to_string().into();
        self.backup_path = PathBuf::from(data["backup_path"].to_string());
        self.backup_number = data["backup_number"].to_string().parse::<u8>().unwrap_or(0).into();
        self
    }
    pub fn get_dsn(self) -> String {
        match self.mode {
            Mode::Mysql => {
                format!("mysql://{}:{}@{}:{}/{}", self.username, self.userpass, self.hostname, self.hostport, self.database)
            }
            Mode::Sqlite => {
                let db_path = self.database.as_str();
                let path_buf = PathBuf::from(db_path);
                if !path_buf.is_file() && path_buf.file_name().is_some() {
                    fs::create_dir_all(db_path.trim_end_matches(path_buf.file_name().unwrap().to_str().unwrap())).unwrap();
                }
                format!("{}", path_buf.to_str().unwrap())
            }
            Mode::Mssql => format!("sqlsrv://{}:{}@{}:{}/{}", self.username, self.userpass, self.hostname, self.hostport, self.database),
            Mode::None => "".to_string()
        }
    }
}

#[derive(Clone, Debug)]
pub enum Charset {
    Utf8mb4,
    Utf8,
    None,
}

impl Charset {
    pub fn from(str: &str) -> Charset {
        match str {
            "utf8" => Charset::Utf8,
            "utf8mb4" => Charset::Utf8mb4,
            _ => Charset::None
        }
    }
    pub fn str(&mut self) -> &'static str {
        match self {
            Charset::Utf8 => "utf8",
            Charset::Utf8mb4 => "utf8mb4",
            Charset::None => ""
        }
    }
}