59 lines
936 B
Go
59 lines
936 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 {
|
||
|
var configP = make(map[string]*DBConfig)
|
||
|
|
||
|
for name := range config {
|
||
|
c := config[name]
|
||
|
configP[name] = &c
|
||
|
}
|
||
|
|
||
|
db := DB{
|
||
|
DatabaseManager: DatabaseManager{
|
||
|
Configs: configP,
|
||
|
Connections: make(map[string]*Connection),
|
||
|
},
|
||
|
}
|
||
|
|
||
|
db.Connection("default")
|
||
|
|
||
|
Engine = &db
|
||
|
|
||
|
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()
|
||
|
}
|
||
|
}
|