infrastructure.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package infrastructure
  2. import (
  3. "git.sxidc.com/go-framework/baize/framwork/infrastructure/database"
  4. "git.sxidc.com/go-framework/baize/framwork/infrastructure/database/data_service"
  5. "git.sxidc.com/go-framework/baize/framwork/infrastructure/database/operations"
  6. )
  7. type Config struct {
  8. DatabaseConfig
  9. }
  10. type DatabaseConfig struct {
  11. Operations *operations.Config `json:"operations" yaml:"operations"`
  12. DataService *data_service.Config `json:"data_service" yaml:"data_service"`
  13. }
  14. type Infrastructure struct {
  15. dbExecutor database.Executor
  16. }
  17. func NewInfrastructure(config Config) *Infrastructure {
  18. i := new(Infrastructure)
  19. // 数据库执行器多选一
  20. if config.DatabaseConfig.Operations != nil {
  21. op, err := operations.NewOperations(config.DatabaseConfig.Operations)
  22. if err != nil {
  23. panic("创建数据库操作失败" + err.Error())
  24. }
  25. i.dbExecutor = op
  26. } else if config.DatabaseConfig.DataService != nil {
  27. dataService, err := data_service.NewDataService(config.DatabaseConfig.DataService)
  28. if err != nil {
  29. panic("创建数据服务失败" + err.Error())
  30. }
  31. i.dbExecutor = dataService
  32. }
  33. return i
  34. }
  35. func DestroyInfrastructure(i *Infrastructure) {
  36. if i == nil {
  37. return
  38. }
  39. if i.dbExecutor != nil {
  40. switch dbExecutor := i.dbExecutor.(type) {
  41. case *operations.Operations:
  42. err := operations.DestroyOperation(dbExecutor)
  43. if err != nil {
  44. panic("销毁数据库操作失败" + err.Error())
  45. }
  46. case *data_service.DataService:
  47. err := data_service.DestroyDataService(dbExecutor)
  48. if err != nil {
  49. panic("销毁数据服务失败" + err.Error())
  50. }
  51. default:
  52. panic("不支持的数据库执行器类型")
  53. }
  54. }
  55. return
  56. }
  57. func (i Infrastructure) DBExecutor() database.Executor {
  58. return i.dbExecutor
  59. }