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
use std::collections::HashMap;
use std::path::Path;
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 to_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.to_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,
        }
    }
}

#[derive(Clone, Debug)]
pub enum Mode {
    Mysql,
    Mssql,
    Sqlite,
}

impl Mode {
    pub fn to_str(&mut self) -> &'static str {
        match self {
            Mode::Mysql => "mysql",
            Mode::Sqlite => "sqlite",
            Mode::Mssql => "mssql"
        }
    }
    pub fn from(name: &str) -> Self {
        match name {
            "mysql" => Mode::Mysql,
            "sqlite" => Mode::Sqlite,
            "mssql" => Mode::Mssql,
            _ => Mode::Sqlite
        }
    }
}

#[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: String,
    pub prefix: String,
    pub debug: bool,
}

impl Connection {
    pub fn default() -> Connection {
        Self {
            mode: Mode::Sqlite,
            hostname: "".to_string(),
            hostport: "".to_string(),
            database: "db/db.db".to_string(),
            username: "".to_string(),
            userpass: "".to_string(),
            params: vec![],
            charset: "".to_string(),
            prefix: "".to_string(),
            debug: false,
        }
    }
    pub fn to_json(&mut self) -> JsonValue {
        let mut data = object! {};
        data["mode"] = self.mode.to_str().into();
        data["hostname"] = self.hostname.clone().into();
        data["hostport"] = self.hostport.clone().into();
        data["database"] = self.database.clone().into();
        data["username"] = self.username.clone().into();
        data["userpass"] = self.userpass.clone().into();
        data["params"] = self.params.clone().into();
        data["charset"] = self.charset.clone().into();
        data["prefix"] = self.prefix.clone().into();
        data["debug"] = self.debug.clone().into();
        data
    }
    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 = data["charset"].to_string();
        self.prefix = data["prefix"].to_string();
        self.debug = data["debug"].to_string().parse::<bool>().unwrap_or(false).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 data = Path::new(self.database.as_str());
                format!("{}", data.to_str().unwrap())
            }
            Mode::Mssql => format!("sqlsrv://{}:{}@{}:{}/{}", self.username, self.userpass, self.hostname, self.hostport, self.database),
        }
    }
}