chore: 改用函数暴露句柄

This commit is contained in:
tiglog 2023-08-17 19:21:21 +08:00
parent ac51a6a211
commit 72aa931caa

View File

@ -21,7 +21,7 @@ import (
"time" "time"
) )
var DM *DbMap var dm *DbMap
type DbOption struct { type DbOption struct {
Type string Type string
@ -65,13 +65,24 @@ func InitDb(opt DbOption) error {
default: default:
return errors.New("unrecognized database driver") return errors.New("unrecognized database driver")
} }
DM = &DbMap{ dm = &DbMap{
Db: db, Db: db,
Dialect: dialect, Dialect: dialect,
} }
return nil return nil
} }
// 通过函数暴露句柄
func GetDm() *DbMap {
if dm == nil {
InitDb(DbOption{
Type: "sqlite3",
Dsn: ":memory:",
})
}
return dm
}
// DbMap is the root sqldb mapping object. Create one of these for each // DbMap is the root sqldb mapping object. Create one of these for each
// database schema you wish to map. Each DbMap contains a list of // database schema you wish to map. Each DbMap contains a list of
// mapped tables. // mapped tables.