forked from andeya/faygo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
103 lines (97 loc) · 2.86 KB
/
config.go
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
package xorm
import (
"os"
"path/filepath"
"github.com/henrylee2cn/faygo"
"github.com/henrylee2cn/ini"
)
// DBConfig is database connection config
type DBConfig struct {
Name string `ini:"-"`
Enable bool `ini:"enable" comment:"Enable the config section"`
Driver string `ini:"driver" comment:"mssql | odbc(mssql) | mysql | mymysql | postgres | sqlite3 | oci8 | goracle"`
Connstring string `ini:"connstring" comment:"Connect String"`
MaxOpenConns int `ini:"max_open_conns"`
MaxIdleConns int `ini:"max_idle_conns"`
ShowSql bool `ini:"show_sql" comment:"print sql"`
TableFix string `ini:"table_fix" comment:"the table namespace is located in the prefix or suffix: prefix | suffix"`
TableSpace string `ini:"table_space" comment:"table namespace"`
TableSnake bool `ini:"table_snake" comment:"the table name uses the snake style or remains unchanged"`
ColumnFix string `ini:"column_fix" comment:"column namespace is located in the prefix or suffix: prefix | suffix"`
ColumnSpace string `ini:"column_space" comment:"column namespace"`
ColumnSnake bool `ini:"column_snake" comment:"the column name uses the snake style or remains unchanged"`
DisableCache bool `ini:"disable_cache"`
ShowExecTime bool `ini:"show_exec_time" comment:"print exec time"`
}
// default constant
const (
DBCONFIG_FILE = faygo.CONFIG_DIR + "xorm.ini"
DATABASE_DIR = "database/"
DEFAULTDB_NAME = "default"
)
var (
dbConfigs = map[string]*DBConfig{}
defaultConfig = &DBConfig{
Name: DEFAULTDB_NAME,
Driver: "mysql",
Connstring: "root:@tcp(127.0.0.1:3306)/test?charset=utf8",
MaxOpenConns: 100,
MaxIdleConns: 100,
TableFix: "prefix",
TableSpace: "",
TableSnake: true,
ColumnFix: "prefix",
ColumnSpace: "",
ColumnSnake: true,
DisableCache: false,
ShowExecTime: false,
ShowSql: false,
}
)
func loadDBConfig() error {
var cfg *ini.File
var err error
var exist bool
cfg, err = ini.Load(DBCONFIG_FILE)
if err != nil {
os.MkdirAll(filepath.Dir(DBCONFIG_FILE), 0777)
cfg, err = ini.LooseLoad(DBCONFIG_FILE)
if err != nil {
return err
}
} else {
exist = true
}
var hadDefaultConfig bool
for _, section := range cfg.Sections() {
if section.Name() == ini.DEFAULT_SECTION {
continue
}
var dbConfig *DBConfig
if section.Name() == DEFAULTDB_NAME {
dbConfig = defaultConfig
hadDefaultConfig = true
} else {
dbConfig = &DBConfig{Name: section.Name()}
}
err := section.MapTo(dbConfig)
if err != nil {
return err
}
dbConfigs[dbConfig.Name] = dbConfig
}
if !exist {
sec, _ := cfg.NewSection(DEFAULTDB_NAME)
defaultConfig.Enable = true
err := sec.ReflectFrom(defaultConfig)
if err != nil {
return err
}
dbConfigs[DEFAULTDB_NAME] = defaultConfig
return cfg.SaveTo(DBCONFIG_FILE)
}
if !hadDefaultConfig {
*defaultConfig = DBConfig{}
}
return nil
}