mirror of
http://github.com/goal-web/database
synced 2025-12-24 10:40:53 +08:00
57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
package database
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/goal-web/contracts"
|
|
"github.com/goal-web/supports/utils"
|
|
)
|
|
|
|
type Factory struct {
|
|
events contracts.EventDispatcher
|
|
config contracts.Config
|
|
connections map[string]contracts.DBConnection
|
|
drivers map[string]contracts.DBConnector
|
|
dbConfig Config
|
|
}
|
|
|
|
func (this *Factory) Connection(name ...string) contracts.DBConnection {
|
|
connection := this.dbConfig.Default
|
|
if len(name) > 0 && name[0] != "" {
|
|
connection = name[0]
|
|
}
|
|
if conn, existsConnection := this.connections[connection]; existsConnection {
|
|
return conn
|
|
}
|
|
|
|
this.connections[connection] = this.make(connection)
|
|
|
|
return this.connections[connection]
|
|
}
|
|
|
|
func (this *Factory) Extend(name string, driver contracts.DBConnector) {
|
|
this.drivers[name] = driver
|
|
}
|
|
|
|
func (this *Factory) make(name string) contracts.DBConnection {
|
|
config := this.config.Get("database").(Config)
|
|
|
|
if connectionConfig, existsConnection := config.Connections[name]; existsConnection {
|
|
driverName := utils.GetStringField(connectionConfig, "driver")
|
|
if driver, existsDriver := this.drivers[driverName]; existsDriver {
|
|
return driver(connectionConfig, this.events)
|
|
}
|
|
|
|
panic(DBConnectionException{
|
|
error: errors.New("该数据库驱动不存在:" + driverName),
|
|
Code: DbDriverDontExist,
|
|
fields: connectionConfig,
|
|
})
|
|
}
|
|
|
|
panic(DBConnectionException{
|
|
error: errors.New("数据库连接不存在:" + name),
|
|
Code: DbConnectionDontExist,
|
|
Connection: name,
|
|
})
|
|
}
|