| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- package infrastructure
- import (
- "git.sxidc.com/go-framework/baize/infrastructure/database"
- "git.sxidc.com/go-framework/baize/infrastructure/database/data_service"
- "git.sxidc.com/go-framework/baize/infrastructure/database/operations"
- )
- type Config struct {
- DatabaseConfig
- }
- type DatabaseConfig struct {
- Operations *operations.Config
- DataService *data_service.Config
- }
- type Infrastructure struct {
- dbExecutor database.Executor
- }
- func NewInfrastructure(config Config) *Infrastructure {
- i := new(Infrastructure)
- // 数据库执行器多选一
- if config.DatabaseConfig.Operations != nil {
- op, err := operations.NewOperations(config.DatabaseConfig.Operations)
- if err != nil {
- panic("创建数据库操作失败" + err.Error())
- }
- i.dbExecutor = op
- } else if config.DatabaseConfig.DataService != nil {
- dataService, err := data_service.NewDataService(config.DatabaseConfig.DataService)
- if err != nil {
- panic("创建数据服务失败" + err.Error())
- }
- i.dbExecutor = dataService
- }
- return i
- }
- func DestroyInfrastructure(i *Infrastructure) {
- if i == nil {
- return
- }
- if i.dbExecutor != nil {
- switch dbExecutor := i.dbExecutor.(type) {
- case *operations.Operations:
- err := operations.DestroyOperation(dbExecutor)
- if err != nil {
- panic("销毁数据库操作失败" + err.Error())
- }
- case *data_service.DataService:
- err := data_service.DestroyDataService(dbExecutor)
- if err != nil {
- panic("销毁数据服务失败" + err.Error())
- }
- default:
- panic("不支持的数据库执行器类型")
- }
- }
- return
- }
- func (i Infrastructure) DBExecutor() database.Executor {
- return i.dbExecutor
- }
|