db/db.go

55 lines
910 B
Go
Raw Normal View History

2023-04-12 15:58:25 +08:00
package db
import (
"database/sql"
)
var Engine *DB
type DB struct {
DatabaseManager
LogFunc func(log Log)
}
func (d *DB) SetLogger(f func(log Log)) *DB {
d.LogFunc = f
return d
}
func Open(config map[string]DBConfig) *DB {
2023-06-21 09:56:28 +08:00
if Engine == nil {
Engine = &DB{
DatabaseManager: DatabaseManager{
Configs: make(map[string]*DBConfig),
Connections: make(map[string]*Connection),
},
}
}
2023-04-12 15:58:25 +08:00
for name := range config {
c := config[name]
2023-06-21 09:56:28 +08:00
Engine.Configs[name] = &c
2023-04-12 15:58:25 +08:00
}
return Engine
}
func (d *DB) AddConfig(name string, config *DBConfig) *DB {
Engine.Configs[name] = config
return d
}
func (d *DB) GetConfigs() map[string]*DBConfig {
return Engine.Configs
}
func (*DB) Raw(connectionName ...string) *sql.DB {
if len(connectionName) > 0 {
c := Engine.Connection(connectionName[0])
return (*c).GetDB()
} else {
c := Engine.Connection("default")
return (*c).GetDB()
}
}