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 std::fmt::{Display, Formatter};

use once_cell::sync::OnceCell;

pub static DYSQL_CONFIG: OnceCell<DySqlConfig> = OnceCell::new();

pub struct DySqlConfig {
    pub  dialect: SqlDialect
}

impl DySqlConfig {
    pub fn new() -> Self {
        Self {
            dialect: SqlDialect::postgres
        }
    }
}

pub fn get_dysql_config() -> &'static DySqlConfig {
    let cfg = DYSQL_CONFIG.get_or_init(|| {
        DySqlConfig::new()
    });

    cfg
}

#[allow(non_camel_case_types)]
#[derive(Debug)]
pub enum SqlDialect {
    postgres,
    mysql,
    sqlite,
}

impl Display for SqlDialect {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl From<String> for SqlDialect {
    fn from(source: String) -> Self {
        if source == SqlDialect::postgres.to_string() {
            SqlDialect::postgres
        } else if source == SqlDialect::mysql.to_string() {
            SqlDialect::mysql
        } else if source == SqlDialect::sqlite.to_string() {
            SqlDialect::sqlite
        } else {
            panic!("{} dialect is not support", source);
        }
    }
}

impl PartialEq<String> for SqlDialect {
    fn eq(&self, other: &String) -> bool {
        *other == self.to_string()
    }
}