Browse Source

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

2.初始化加入可选文件载入,可自定义加载所需的数据库文件
tags/v1.0.13
parent
commit
59018c5269
3 changed files with 36 additions and 14 deletions
  1. +5
    -0
      README.md
  2. +30
    -13
      db/db.go
  3. +1
    -1
      go.mod

+ 5
- 0
README.md View File

@@ -5,4 +5,9 @@

```go
import "golib.gaore.com/GaoreGo/beegoinit"
```

必须显式调用初始化
```go
db.ReloadByDefault()
```

+ 30
- 13
db/db.go View File

@@ -41,10 +41,14 @@ func (d *DbItem) Dsn() string {
}

func init() {
Reload()
// Reload() 不再自动链接数据库了, 需要自己手动
}

func Reload(dir ...string) {
func ReloadByDefault() {
Reload(nil)
}

func Reload(files []string, dir ...string) {
var err error
var configer *grconfig.Configer
logger := grlogs.GetAli("sql")
@@ -61,26 +65,39 @@ func Reload(dir ...string) {
dir = append(dir, "conf/")
configer = grconfig.New(path.Join(dir...))
}
dsns, err := GetDsn(configer)
dsns, err := GetDsn(configer, files...)
if err = LoadXorm(dsns); err != nil {
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
DsnList = make(map[string]dsnConfig)
err = configer.Item("db", &items)
if err != nil {
return
if nil == dbfiles || len(dbfiles) == 0 {
dbfiles = []string{"db"}
}
for alias, v := range items {
dsn := v.Dsn()
driver := v.Driver
if driver == "" {
driver = "mysql"

for _, dbfile := range dbfiles {

if dbfile == "" {
continue
}

err = configer.Item(dbfile, &items)
if err != nil {
return
}
for alias, v := range items {
dsn := v.Dsn()
driver := v.Driver
if driver == "" {
driver = "mysql"
}
DsnList[alias] = dsnConfig{Dsn: dsn, Driver: driver, Item: v}
}
DsnList[alias] = dsnConfig{Dsn: dsn, Driver: driver, Item: v}
}

return
}

+ 1
- 1
go.mod View File

@@ -1,6 +1,6 @@
module golib.gaore.com/GaoreGo/beegoinit

go 1.13
go 1.15

require (
github.com/OwnLocal/goes v1.0.0 // indirect


Loading…
Cancel
Save