feat: 新增简易版ioc

This commit is contained in:
meilin.huang
2024-01-21 22:52:20 +08:00
parent f4a64b96a9
commit f27d3d200f
106 changed files with 815 additions and 707 deletions

View File

@@ -4,21 +4,26 @@ import (
"fmt"
"mayfly-go/internal/db/domain/repository"
"mayfly-go/internal/db/infrastructure/persistence"
tagapp "mayfly-go/internal/tag/application"
"mayfly-go/pkg/ioc"
"sync"
)
var (
instanceApp Instance
dbApp Db
dbSqlExecApp DbSqlExec
dbSqlApp DbSql
dbBackupApp *DbBackupApp
dbRestoreApp *DbRestoreApp
dbBinlogApp *DbBinlogApp
dataSyncApp DataSyncTask
)
func init() {
persistence.Init()
ioc.Register(new(instanceAppImpl), ioc.WithComponentName("DbInstanceApp"))
ioc.Register(new(dbAppImpl), ioc.WithComponentName("DbApp"))
ioc.Register(new(dbSqlExecAppImpl), ioc.WithComponentName("DbSqlExecApp"))
ioc.Register(new(dbSqlAppImpl), ioc.WithComponentName("DbSqlApp"))
ioc.Register(new(dataSyncAppImpl), ioc.WithComponentName("DbDataSyncTaskApp"))
}
func Init() {
sync.OnceFunc(func() {
repositories := &repository.Repositories{
@@ -31,13 +36,7 @@ func Init() {
BinlogHistory: persistence.NewDbBinlogHistoryRepo(),
}
var err error
instanceRepo := persistence.GetInstanceRepo()
instanceApp = newInstanceApp(instanceRepo)
dbApp = newDbApp(persistence.GetDbRepo(), persistence.GetDbSqlRepo(), instanceApp, tagapp.GetTagTreeApp())
dbSqlExecApp = newDbSqlExecApp(persistence.GetDbSqlExecRepo())
dbSqlApp = newDbSqlApp(persistence.GetDbSqlRepo())
dataSyncApp = newDataSyncApp(persistence.GetDataSyncTaskRepo(), persistence.GetDataSyncLogRepo())
dbApp := GetDbApp()
scheduler, err := newDbScheduler(repositories)
if err != nil {
panic(fmt.Sprintf("初始化 dbScheduler 失败: %v", err))
@@ -55,24 +54,24 @@ func Init() {
panic(fmt.Sprintf("初始化 dbBinlogApp 失败: %v", err))
}
dataSyncApp.InitCronJob()
GetDataSyncTaskApp().InitCronJob()
})()
}
func GetInstanceApp() Instance {
return instanceApp
return ioc.Get[Instance]("DbInstance")
}
func GetDbApp() Db {
return dbApp
return ioc.Get[Db]("DbApp")
}
func GetDbSqlApp() DbSql {
return dbSqlApp
return ioc.Get[DbSql]("DbSqlApp")
}
func GetDbSqlExecApp() DbSqlExec {
return dbSqlExecApp
return ioc.Get[DbSqlExec]("DbSqlExecApp")
}
func GetDbBackupApp() *DbBackupApp {
@@ -88,5 +87,5 @@ func GetDbBinlogApp() *DbBinlogApp {
}
func GetDataSyncTaskApp() DataSyncTask {
return dataSyncApp
return ioc.Get[DataSyncTask]("DbDataSyncTaskApp")
}

View File

@@ -40,22 +40,17 @@ type Db interface {
GetDbConnByInstanceId(instanceId uint64) (*dbi.DbConn, error)
}
func newDbApp(dbRepo repository.Db, dbSqlRepo repository.DbSql, dbInstanceApp Instance, tagApp tagapp.TagTree) Db {
app := &dbAppImpl{
dbSqlRepo: dbSqlRepo,
dbInstanceApp: dbInstanceApp,
tagApp: tagApp,
}
app.Repo = dbRepo
return app
}
type dbAppImpl struct {
base.AppImpl[*entity.Db, repository.Db]
dbSqlRepo repository.DbSql
dbInstanceApp Instance
tagApp tagapp.TagTree
DbSqlRepo repository.DbSql `inject:""`
DbInstanceApp Instance `inject:""`
TagApp tagapp.TagTree `inject:"TagTreeApp"`
}
// 注入DbRepo
func (d *dbAppImpl) InjectDbRepo(repo repository.Db) {
d.Repo = repo
}
// 分页获取数据库信息列表
@@ -83,7 +78,7 @@ func (d *dbAppImpl) SaveDb(ctx context.Context, dbEntity *entity.Db, tagIds ...u
return d.Tx(ctx, func(ctx context.Context) error {
return d.Insert(ctx, dbEntity)
}, func(ctx context.Context) error {
return d.tagApp.RelateResource(ctx, resouceCode, consts.TagResourceTypeDb, tagIds)
return d.TagApp.RelateResource(ctx, resouceCode, consts.TagResourceTypeDb, tagIds)
})
}
@@ -107,13 +102,13 @@ func (d *dbAppImpl) SaveDb(ctx context.Context, dbEntity *entity.Db, tagIds ...u
// 关闭数据库连接
dbm.CloseDb(dbEntity.Id, v)
// 删除该库关联的所有sql记录
d.dbSqlRepo.DeleteByCond(ctx, &entity.DbSql{DbId: dbId, Db: v})
d.DbSqlRepo.DeleteByCond(ctx, &entity.DbSql{DbId: dbId, Db: v})
}
return d.Tx(ctx, func(ctx context.Context) error {
return d.UpdateById(ctx, dbEntity)
}, func(ctx context.Context) error {
return d.tagApp.RelateResource(ctx, old.Code, consts.TagResourceTypeDb, tagIds)
return d.TagApp.RelateResource(ctx, old.Code, consts.TagResourceTypeDb, tagIds)
})
}
@@ -134,10 +129,10 @@ func (d *dbAppImpl) Delete(ctx context.Context, id uint64) error {
},
func(ctx context.Context) error {
// 删除该库下用户保存的所有sql信息
return d.dbSqlRepo.DeleteByCond(ctx, &entity.DbSql{DbId: id})
return d.DbSqlRepo.DeleteByCond(ctx, &entity.DbSql{DbId: id})
}, func(ctx context.Context) error {
var tagIds []uint64
return d.tagApp.RelateResource(ctx, db.Code, consts.TagResourceTypeDb, tagIds)
return d.TagApp.RelateResource(ctx, db.Code, consts.TagResourceTypeDb, tagIds)
})
}
@@ -148,7 +143,7 @@ func (d *dbAppImpl) GetDbConn(dbId uint64, dbName string) (*dbi.DbConn, error) {
return nil, errorx.NewBiz("数据库信息不存在")
}
instance, err := d.dbInstanceApp.GetById(new(entity.DbInstance), db.InstanceId)
instance, err := d.DbInstanceApp.GetById(new(entity.DbInstance), db.InstanceId)
if err != nil {
return nil, errorx.NewBiz("数据库实例不存在")
}
@@ -169,7 +164,7 @@ func (d *dbAppImpl) GetDbConn(dbId uint64, dbName string) (*dbi.DbConn, error) {
if err := instance.PwdDecrypt(); err != nil {
return nil, errorx.NewBiz(err.Error())
}
return toDbInfo(instance, dbId, dbName, d.tagApp.ListTagPathByResource(consts.TagResourceTypeDb, db.Code)...), nil
return toDbInfo(instance, dbId, dbName, d.TagApp.ListTagPathByResource(consts.TagResourceTypeDb, db.Code)...), nil
})
}

View File

@@ -38,17 +38,14 @@ type DataSyncTask interface {
GetTaskLogList(condition *entity.DataSyncLogQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
}
func newDataSyncApp(dataSyncRepo repository.DataSyncTask, dataSyncLogRepo repository.DataSyncLog) DataSyncTask {
app := new(dataSyncAppImpl)
app.Repo = dataSyncRepo
app.dataSyncLogRepo = dataSyncLogRepo
return app
}
type dataSyncAppImpl struct {
base.AppImpl[*entity.DataSyncTask, repository.DataSyncTask]
dataSyncLogRepo repository.DataSyncLog
DbDataSyncLogRepo repository.DataSyncLog `inject:""`
}
func (d *dataSyncAppImpl) InjectDbDataSyncTaskRepo(repo repository.DataSyncTask) {
d.Repo = repo
}
func (app *dataSyncAppImpl) GetPageList(condition *entity.DataSyncTaskQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
@@ -328,7 +325,7 @@ func (app *dataSyncAppImpl) endRunning(taskEntity *entity.DataSyncTask, log *ent
}
func (app *dataSyncAppImpl) saveLog(log *entity.DataSyncLog) {
app.dataSyncLogRepo.Save(context.Background(), log)
app.DbDataSyncLogRepo.Save(context.Background(), log)
}
func (app *dataSyncAppImpl) InitCronJob() {
@@ -374,5 +371,5 @@ func (app *dataSyncAppImpl) InitCronJob() {
}
func (app *dataSyncAppImpl) GetTaskLogList(condition *entity.DataSyncLogQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
return app.dataSyncLogRepo.GetTaskLogList(condition, pageParam, toEntity, orderBy...)
return app.DbDataSyncLogRepo.GetTaskLogList(condition, pageParam, toEntity, orderBy...)
}

View File

@@ -33,7 +33,7 @@ type dbScheduler struct {
func newDbScheduler(repositories *repository.Repositories) (*dbScheduler, error) {
scheduler := &dbScheduler{
dbApp: dbApp,
dbApp: GetDbApp(),
backupRepo: repositories.Backup,
backupHistoryRepo: repositories.BackupHistory,
restoreRepo: repositories.Restore,

View File

@@ -14,8 +14,7 @@ type dbSqlAppImpl struct {
base.AppImpl[*entity.DbSql, repository.DbSql]
}
func newDbSqlApp(dbSqlRepo repository.DbSql) DbSql {
app := new(dbSqlAppImpl)
app.Repo = dbSqlRepo
return app
// 注入DbSqlRepo
func (d *dbSqlAppImpl) InjectDbSqlRepo(repo repository.DbSql) {
d.Repo = repo
}

View File

@@ -56,14 +56,8 @@ type DbSqlExec interface {
GetPageList(condition *entity.DbSqlExecQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
}
func newDbSqlExecApp(dbExecSqlRepo repository.DbSqlExec) DbSqlExec {
return &dbSqlExecAppImpl{
dbSqlExecRepo: dbExecSqlRepo,
}
}
type dbSqlExecAppImpl struct {
dbSqlExecRepo repository.DbSqlExec
DbSqlExecRepo repository.DbSqlExec `inject:""`
}
func createSqlExecRecord(ctx context.Context, execSqlReq *DbSqlExecReq) *entity.DbSqlExec {
@@ -144,23 +138,23 @@ func (d *dbSqlExecAppImpl) Exec(ctx context.Context, execSqlReq *DbSqlExecReq) (
// 保存sql执行记录如果是查询类则根据系统配置判断是否保存
func (d *dbSqlExecAppImpl) saveSqlExecLog(isQuery bool, dbSqlExecRecord *entity.DbSqlExec) {
if !isQuery {
d.dbSqlExecRepo.Insert(context.TODO(), dbSqlExecRecord)
d.DbSqlExecRepo.Insert(context.TODO(), dbSqlExecRecord)
return
}
if config.GetDbSaveQuerySql() {
dbSqlExecRecord.Table = "-"
dbSqlExecRecord.OldValue = "-"
dbSqlExecRecord.Type = entity.DbSqlExecTypeQuery
d.dbSqlExecRepo.Insert(context.TODO(), dbSqlExecRecord)
d.DbSqlExecRepo.Insert(context.TODO(), dbSqlExecRecord)
}
}
func (d *dbSqlExecAppImpl) DeleteBy(ctx context.Context, condition *entity.DbSqlExec) {
d.dbSqlExecRepo.DeleteByCond(ctx, condition)
d.DbSqlExecRepo.DeleteByCond(ctx, condition)
}
func (d *dbSqlExecAppImpl) GetPageList(condition *entity.DbSqlExecQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
return d.dbSqlExecRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
return d.DbSqlExecRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
}
func doSelect(ctx context.Context, selectStmt *sqlparser.Select, execSqlReq *DbSqlExecReq) (*DbSqlExecRes, error) {

View File

@@ -30,16 +30,15 @@ type Instance interface {
GetDatabases(entity *entity.DbInstance) ([]string, error)
}
func newInstanceApp(instanceRepo repository.Instance) Instance {
app := new(instanceAppImpl)
app.Repo = instanceRepo
return app
}
type instanceAppImpl struct {
base.AppImpl[*entity.DbInstance, repository.Instance]
}
// 注入DbInstanceRepo
func (i *instanceAppImpl) InjectDbInstanceRepo(repo repository.Instance) {
i.Repo = repo
}
// GetPageList 分页获取数据库实例
func (app *instanceAppImpl) GetPageList(condition *entity.InstanceQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
return app.GetRepo().GetInstanceList(condition, pageParam, toEntity, orderBy...)