wangpengfei
2023-08-25 25c573d55986e02cf5f70cc3868e2b94a4be98e2
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
package request
 
import (
    "fmt"
    "os"
    "srm/config"
)
 
type InitDB struct {
    DBType   string `json:"dbType"`                    // 数据库类型
    Host     string `json:"host"`                      // 服务器地址
    Port     string `json:"port"`                      // 数据库连接端口
    UserName string `json:"userName"`                  // 数据库用户名
    Password string `json:"password"`                  // 数据库密码
    DBName   string `json:"dbName" binding:"required"` // 数据库名
    DBPath   string `json:"dbPath"`                    // sqlite数据库文件路径
}
 
// MysqlEmptyDsn msyql 空数据库 建库链接
// Author SliverHorn
func (i *InitDB) MysqlEmptyDsn() string {
    if i.Host == "" {
        i.Host = "127.0.0.1"
    }
    if i.Port == "" {
        i.Port = "3306"
    }
    return fmt.Sprintf("%s:%s@tcp(%s:%s)/", i.UserName, i.Password, i.Host, i.Port)
}
 
// PgsqlEmptyDsn pgsql 空数据库 建库链接
// Author SliverHorn
func (i *InitDB) PgsqlEmptyDsn() string {
    if i.Host == "" {
        i.Host = "127.0.0.1"
    }
    if i.Port == "" {
        i.Port = "5432"
    }
    return "host=" + i.Host + " user=" + i.UserName + " password=" + i.Password + " port=" + i.Port + " dbname=" + "postgres" + " " + "sslmode=disable TimeZone=Asia/Shanghai"
}
 
// SqliteEmptyDsn sqlite 空数据库 建库链接
// Author Kafumio
func (i *InitDB) SqliteEmptyDsn() string {
    separator := string(os.PathSeparator)
    return i.DBPath + separator + i.DBName + ".db"
}
 
// ToMysqlConfig 转换 config.Mysql
// Author [SliverHorn](https://github.com/SliverHorn)
func (i *InitDB) ToMysqlConfig() config.Mysql {
    return config.Mysql{
        GeneralDB: config.GeneralDB{
            Path:         i.Host,
            Port:         i.Port,
            Dbname:       i.DBName,
            Username:     i.UserName,
            Password:     i.Password,
            MaxIdleConns: 10,
            MaxOpenConns: 100,
            LogMode:      "error",
            Config:       "charset=utf8mb4&parseTime=True&loc=Local",
        },
    }
}
 
// ToPgsqlConfig 转换 config.Pgsql
// Author [SliverHorn](https://github.com/SliverHorn)
func (i *InitDB) ToPgsqlConfig() config.Pgsql {
    return config.Pgsql{
        GeneralDB: config.GeneralDB{
            Path:         i.Host,
            Port:         i.Port,
            Dbname:       i.DBName,
            Username:     i.UserName,
            Password:     i.Password,
            MaxIdleConns: 10,
            MaxOpenConns: 100,
            LogMode:      "error",
            Config:       "sslmode=disable TimeZone=Asia/Shanghai",
        },
    }
}
 
// ToSqliteConfig 转换 config.Sqlite
// Author [Kafumio](https://github.com/Kafumio)
func (i *InitDB) ToSqliteConfig() config.Sqlite {
    return config.Sqlite{
        GeneralDB: config.GeneralDB{
            Path:         i.DBPath,
            Port:         i.Port,
            Dbname:       i.DBName,
            Username:     i.UserName,
            Password:     i.Password,
            MaxIdleConns: 10,
            MaxOpenConns: 100,
            LogMode:      "error",
            Config:       "",
        },
    }
}