55 lines
910 B
Go
55 lines
910 B
Go
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 {
|
|
if Engine == nil {
|
|
Engine = &DB{
|
|
DatabaseManager: DatabaseManager{
|
|
Configs: make(map[string]*DBConfig),
|
|
Connections: make(map[string]*Connection),
|
|
},
|
|
}
|
|
}
|
|
|
|
for name := range config {
|
|
c := config[name]
|
|
Engine.Configs[name] = &c
|
|
}
|
|
|
|
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()
|
|
}
|
|
}
|