1 Star 2 Fork 3

kristas/booting-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
datasource.go 951 Bytes
一键复制 编辑 原始数据 按行查看 历史
kristas 提交于 2021-12-13 16:40 . feat: refactor datasource
package datasource
import (
"errors"
"gitee.com/kristas/booting-go/framework/core/statement/types"
)
type DataSourceImpl struct {
types.Component
Drivers []DataSourceDriver `wire:""`
db interface{}
name string
}
func (d *DataSourceImpl) Bean() string {
return d.name
}
func NewDataSource(name ...string) DataSource {
d := &DataSourceImpl{}
if len(name) > 0 {
d.name = name[0]
} else {
d.name = PropertiesPath
}
return d
}
func (d *DataSourceImpl) Build(properties *DataSourceProperties) (DataSource, error) {
found := false
for _, driver := range d.Drivers {
if driver.Type() == properties.Type && driver.Driver() == properties.Driver {
found = true
conn, err := driver.Open(properties)
if err != nil {
return nil, err
}
d.db = conn
}
}
if !found {
return nil, errors.New(d.Bean() + " not found matched driver!")
}
return d, nil
}
func (d *DataSourceImpl) DB() interface{} {
return d.db
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kristas/booting-go.git
git@gitee.com:kristas/booting-go.git
kristas
booting-go
booting-go
v1.4.3

搜索帮助