1.停用自动初始化数据库,须显式调用

2.初始化加入可选文件载入,可自定义加载所需的数据库文件
This commit is contained in:
liangzy 2023-02-17 11:24:58 +08:00
parent 851e2bd23c
commit 59018c5269
3 changed files with 36 additions and 14 deletions

View File

@ -6,3 +6,8 @@
```go ```go
import "golib.gaore.com/GaoreGo/beegoinit" import "golib.gaore.com/GaoreGo/beegoinit"
``` ```
必须显式调用初始化
```go
db.ReloadByDefault()
```

View File

@ -41,10 +41,14 @@ func (d *DbItem) Dsn() string {
} }
func init() { func init() {
Reload() // Reload() 不再自动链接数据库了, 需要自己手动
} }
func Reload(dir ...string) { func ReloadByDefault() {
Reload(nil)
}
func Reload(files []string, dir ...string) {
var err error var err error
var configer *grconfig.Configer var configer *grconfig.Configer
logger := grlogs.GetAli("sql") logger := grlogs.GetAli("sql")
@ -61,16 +65,27 @@ func Reload(dir ...string) {
dir = append(dir, "conf/") dir = append(dir, "conf/")
configer = grconfig.New(path.Join(dir...)) configer = grconfig.New(path.Join(dir...))
} }
dsns, err := GetDsn(configer) dsns, err := GetDsn(configer, files...)
if err = LoadXorm(dsns); err != nil { if err = LoadXorm(dsns); err != nil {
logger.Critical(err.Error()) logger.Critical(err.Error())
} }
} }
func GetDsn(configer *grconfig.Configer) (DsnList map[string]dsnConfig, err error) { func GetDsn(configer *grconfig.Configer, dbfiles ...string) (DsnList map[string]dsnConfig, err error) {
var items map[string]*DbItem var items map[string]*DbItem
DsnList = make(map[string]dsnConfig) DsnList = make(map[string]dsnConfig)
err = configer.Item("db", &items)
if nil == dbfiles || len(dbfiles) == 0 {
dbfiles = []string{"db"}
}
for _, dbfile := range dbfiles {
if dbfile == "" {
continue
}
err = configer.Item(dbfile, &items)
if err != nil { if err != nil {
return return
} }
@ -82,5 +97,7 @@ func GetDsn(configer *grconfig.Configer) (DsnList map[string]dsnConfig, err erro
} }
DsnList[alias] = dsnConfig{Dsn: dsn, Driver: driver, Item: v} DsnList[alias] = dsnConfig{Dsn: dsn, Driver: driver, Item: v}
} }
}
return return
} }

2
go.mod
View File

@ -1,6 +1,6 @@
module golib.gaore.com/GaoreGo/beegoinit module golib.gaore.com/GaoreGo/beegoinit
go 1.13 go 1.15
require ( require (
github.com/OwnLocal/goes v1.0.0 // indirect github.com/OwnLocal/goes v1.0.0 // indirect