From b973d63331a370745295bb6e656cb731aec33148 Mon Sep 17 00:00:00 2001 From: wanli Date: Tue, 5 Sep 2023 08:13:32 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E5=AE=9E=E4=BE=8B=E8=A1=A8=20t=5Finstance=20=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=20t=5Fdb=5Finstance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 同时,为避免混淆,将 application.DbInstance 改为 application.DbConnection --- server/internal/db/api/db.go | 24 ++++++++-------- server/internal/db/application/db.go | 28 +++++++++---------- server/internal/db/application/db_sql_exec.go | 4 +-- server/internal/db/application/mysql_meta.go | 2 +- server/internal/db/application/pgsql_meta.go | 2 +- server/internal/db/domain/entity/instance.go | 4 +++ .../db/infrastructure/persistence/db.go | 2 +- server/mayfly-go.sql | 8 +++--- .../migrations/migrate-database-instance.sql | 8 +++--- 9 files changed, 43 insertions(+), 39 deletions(-) diff --git a/server/internal/db/api/db.go b/server/internal/db/api/db.go index 0f578df2..8d6bac7b 100644 --- a/server/internal/db/api/db.go +++ b/server/internal/db/api/db.go @@ -90,29 +90,29 @@ func (d *Db) DeleteDb(rc *req.Ctx) { } } -func (d *Db) getDbInstance(g *gin.Context) *application.DbInstance { +func (d *Db) getDbConnection(g *gin.Context) *application.DbConnection { dbName := g.Query("db") biz.NotEmpty(dbName, "db不能为空") dbId := getDbId(g) db := d.DbApp.GetById(dbId) instance := d.InstanceApp.GetById(db.InstanceId) - return d.DbApp.GetDbInstance(db, instance, dbName) + return d.DbApp.GetDbConnection(db, instance, dbName) } func (d *Db) TableInfos(rc *req.Ctx) { - rc.ResData = d.getDbInstance(rc.GinCtx).GetMeta().GetTableInfos() + rc.ResData = d.getDbConnection(rc.GinCtx).GetMeta().GetTableInfos() } func (d *Db) TableIndex(rc *req.Ctx) { tn := rc.GinCtx.Query("tableName") biz.NotEmpty(tn, "tableName不能为空") - rc.ResData = d.getDbInstance(rc.GinCtx).GetMeta().GetTableIndex(tn) + rc.ResData = d.getDbConnection(rc.GinCtx).GetMeta().GetTableIndex(tn) } func (d *Db) GetCreateTableDdl(rc *req.Ctx) { tn := rc.GinCtx.Query("tableName") biz.NotEmpty(tn, "tableName不能为空") - rc.ResData = d.getDbInstance(rc.GinCtx).GetMeta().GetCreateTableDdl(tn) + rc.ResData = d.getDbConnection(rc.GinCtx).GetMeta().GetCreateTableDdl(tn) } func (d *Db) ExecSql(rc *req.Ctx) { @@ -123,7 +123,7 @@ func (d *Db) ExecSql(rc *req.Ctx) { dbId := getDbId(g) db := d.DbApp.GetById(dbId) instance := d.InstanceApp.GetById(db.InstanceId) - dbInstance := d.DbApp.GetDbInstance(db, instance, form.Db) + dbInstance := d.DbApp.GetDbConnection(db, instance, form.Db) biz.ErrIsNilAppendErr(d.TagApp.CanAccess(rc.LoginAccount.Id, dbInstance.Info.TagPath), "%s") rc.ReqParam = fmt.Sprintf("%s\n-> %s", dbInstance.Info.GetLogDesc(), form.Sql) @@ -180,7 +180,7 @@ func (d *Db) ExecSqlFile(rc *req.Ctx) { dbId := getDbId(g) dbName := getDbName(g) - dbInstance := d.getDbInstance(rc.GinCtx) + dbInstance := d.getDbConnection(rc.GinCtx) biz.ErrIsNilAppendErr(d.TagApp.CanAccess(rc.LoginAccount.Id, dbInstance.Info.TagPath), "%s") rc.ReqParam = fmt.Sprintf("%s -> filename: %s", dbInstance.Info.GetLogDesc(), filename) @@ -246,7 +246,7 @@ func (d *Db) DumpSql(rc *req.Ctx) { // 是否需要导出数据 needData := dumpType == "2" || dumpType == "3" - dbInstance := d.getDbInstance(rc.GinCtx) + dbInstance := d.getDbConnection(rc.GinCtx) biz.ErrIsNilAppendErr(d.TagApp.CanAccess(rc.LoginAccount.Id, dbInstance.Info.TagPath), "%s") now := time.Now() @@ -261,7 +261,7 @@ func (d *Db) DumpSql(rc *req.Ctx) { writer.WriteString(fmt.Sprintf("\n-- 导出数据库: %s ", db)) writer.WriteString("\n-- ----------------------------\n") - dbmeta := d.getDbInstance(rc.GinCtx).GetMeta() + dbmeta := d.getDbConnection(rc.GinCtx).GetMeta() for _, table := range tables { if needStruct { writer.WriteString(fmt.Sprintf("\n-- ----------------------------\n-- 表结构: %s \n-- ----------------------------\n", table)) @@ -315,7 +315,7 @@ func (d *Db) DumpSql(rc *req.Ctx) { // @router /api/db/:dbId/t-metadata [get] func (d *Db) TableMA(rc *req.Ctx) { - dbi := d.getDbInstance(rc.GinCtx) + dbi := d.getDbConnection(rc.GinCtx) rc.ResData = dbi.GetMeta().GetTables() } @@ -325,13 +325,13 @@ func (d *Db) ColumnMA(rc *req.Ctx) { tn := g.Query("tableName") biz.NotEmpty(tn, "tableName不能为空") - dbi := d.getDbInstance(rc.GinCtx) + dbi := d.getDbConnection(rc.GinCtx) rc.ResData = dbi.GetMeta().GetColumns(tn) } // @router /api/db/:dbId/hint-tables [get] func (d *Db) HintTables(rc *req.Ctx) { - dbi := d.getDbInstance(rc.GinCtx) + dbi := d.getDbConnection(rc.GinCtx) dm := dbi.GetMeta() // 获取所有表 diff --git a/server/internal/db/application/db.go b/server/internal/db/application/db.go index 7350d3c9..77fcbd5e 100644 --- a/server/internal/db/application/db.go +++ b/server/internal/db/application/db.go @@ -39,7 +39,7 @@ type Db interface { // 获取数据库连接实例 // @param id 数据库实例id // @param db 数据库 - GetDbInstance(db *entity.Db, instance *entity.Instance, dbName string) *DbInstance + GetDbConnection(db *entity.Db, instance *entity.Instance, dbName string) *DbConnection } func newDbApp(dbRepo repository.Db, dbSqlRepo repository.DbSql) Db { @@ -129,7 +129,7 @@ func (d *dbAppImpl) Delete(id uint64) { var mutex sync.Mutex -func (d *dbAppImpl) GetDbInstance(db *entity.Db, instance *entity.Instance, dbName string) *DbInstance { +func (d *dbAppImpl) GetDbConnection(db *entity.Db, instance *entity.Instance, dbName string) *DbConnection { cacheKey := GetDbCacheKey(db.Id, dbName) // Id不为0,则为需要缓存 @@ -137,7 +137,7 @@ func (d *dbAppImpl) GetDbInstance(db *entity.Db, instance *entity.Instance, dbNa if needCache { load, ok := dbCache.Get(cacheKey) if ok { - return load.(*DbInstance) + return load.(*DbConnection) } } mutex.Lock() @@ -151,7 +151,7 @@ func (d *dbAppImpl) GetDbInstance(db *entity.Db, instance *entity.Instance, dbNa dbInfo := NewDbInfo(db, instance) dbInfo.Database = dbName - dbi := &DbInstance{Id: cacheKey, Info: dbInfo} + dbi := &DbConnection{Id: cacheKey, Info: dbInfo} conn, err := getInstanceConn(instance, dbName) if err != nil { @@ -209,7 +209,7 @@ func (d *DbInfo) GetLogDesc() string { } // db实例 -type DbInstance struct { +type DbConnection struct { Id string Info *DbInfo @@ -218,18 +218,18 @@ type DbInstance struct { // 执行查询语句 // 依次返回 列名数组,结果map,错误 -func (d *DbInstance) SelectData(execSql string) ([]string, []map[string]any, error) { +func (d *DbConnection) SelectData(execSql string) ([]string, []map[string]any, error) { return SelectDataByDb(d.db, execSql) } // 将查询结果映射至struct,可具体参考sqlx库 -func (d *DbInstance) SelectData2Struct(execSql string, dest any) error { +func (d *DbConnection) SelectData2Struct(execSql string, dest any) error { return Select2StructByDb(d.db, execSql, dest) } // 执行 update, insert, delete,建表等sql // 返回影响条数和错误 -func (d *DbInstance) Exec(sql string) (int64, error) { +func (d *DbConnection) Exec(sql string) (int64, error) { res, err := d.db.Exec(sql) if err != nil { return 0, err @@ -238,7 +238,7 @@ func (d *DbInstance) Exec(sql string) (int64, error) { } // 获取数据库元信息实现接口 -func (di *DbInstance) GetMeta() DbMetadata { +func (di *DbConnection) GetMeta() DbMetadata { dbType := di.Info.Type if dbType == entity.DbTypeMysql { return &MysqlMetadata{di: di} @@ -250,7 +250,7 @@ func (di *DbInstance) GetMeta() DbMetadata { } // 关闭连接 -func (d *DbInstance) Close() { +func (d *DbConnection) Close() { if d.db != nil { if err := d.db.Close(); err != nil { logx.Errorf("关闭数据库实例[%s]连接失败: %s", d.Id, err.Error()) @@ -266,7 +266,7 @@ var dbCache = cache.NewTimedCache(consts.DbConnExpireTime, 5*time.Second). WithUpdateAccessTime(true). OnEvicted(func(key any, value any) { logx.Info(fmt.Sprintf("删除db连接缓存 id = %s", key)) - value.(*DbInstance).Close() + value.(*DbConnection).Close() }) func init() { @@ -274,7 +274,7 @@ func init() { // 遍历所有db连接实例,若存在db实例使用该ssh隧道机器,则返回true,表示还在使用中... items := dbCache.Items() for _, v := range items { - if v.Value.(*DbInstance).Info.SshTunnelMachineId == machineId { + if v.Value.(*DbConnection).Info.SshTunnelMachineId == machineId { return true } } @@ -286,9 +286,9 @@ func GetDbCacheKey(dbId uint64, db string) string { return fmt.Sprintf("%d:%s", dbId, db) } -func GetDbInstanceByCache(id string) *DbInstance { +func GetDbInstanceByCache(id string) *DbConnection { if load, ok := dbCache.Get(id); ok { - return load.(*DbInstance) + return load.(*DbConnection) } return nil } diff --git a/server/internal/db/application/db_sql_exec.go b/server/internal/db/application/db_sql_exec.go index 80efb3f3..591411ef 100644 --- a/server/internal/db/application/db_sql_exec.go +++ b/server/internal/db/application/db_sql_exec.go @@ -20,7 +20,7 @@ type DbSqlExecReq struct { Sql string Remark string LoginAccount *model.LoginAccount - DbInstance *DbInstance + DbInstance *DbConnection } type DbSqlExecRes struct { @@ -252,7 +252,7 @@ func doInsert(insert *sqlparser.Insert, execSqlReq *DbSqlExecReq, dbSqlExec *ent return doExec(execSqlReq.Sql, execSqlReq.DbInstance) } -func doExec(sql string, dbInstance *DbInstance) (*DbSqlExecRes, error) { +func doExec(sql string, dbInstance *DbConnection) (*DbSqlExecRes, error) { rowsAffected, err := dbInstance.Exec(sql) execRes := "success" if err != nil { diff --git a/server/internal/db/application/mysql_meta.go b/server/internal/db/application/mysql_meta.go index 03d7432e..3d5808e0 100644 --- a/server/internal/db/application/mysql_meta.go +++ b/server/internal/db/application/mysql_meta.go @@ -39,7 +39,7 @@ const ( ) type MysqlMetadata struct { - di *DbInstance + di *DbConnection } // 获取表基础元信息, 如表名等 diff --git a/server/internal/db/application/pgsql_meta.go b/server/internal/db/application/pgsql_meta.go index 838b6f99..224b58f8 100644 --- a/server/internal/db/application/pgsql_meta.go +++ b/server/internal/db/application/pgsql_meta.go @@ -71,7 +71,7 @@ const ( ) type PgsqlMetadata struct { - di *DbInstance + di *DbConnection } // 获取表基础元信息, 如表名等 diff --git a/server/internal/db/domain/entity/instance.go b/server/internal/db/domain/entity/instance.go index a3b6372b..2401aaab 100644 --- a/server/internal/db/domain/entity/instance.go +++ b/server/internal/db/domain/entity/instance.go @@ -21,6 +21,10 @@ type Instance struct { SshTunnelMachineId int `orm:"column(ssh_tunnel_machine_id)" json:"sshTunnelMachineId"` // ssh隧道机器id } +func (d *Instance) TableName() string { + return "t_db_instance" +} + // 获取数据库连接网络, 若没有使用ssh隧道,则直接返回。否则返回拼接的网络需要注册至指定dial func (d *Instance) GetNetwork() string { network := d.Network diff --git a/server/internal/db/infrastructure/persistence/db.go b/server/internal/db/infrastructure/persistence/db.go index 5a96196f..8c87b6b1 100644 --- a/server/internal/db/infrastructure/persistence/db.go +++ b/server/internal/db/infrastructure/persistence/db.go @@ -18,7 +18,7 @@ func newDbRepo() repository.Db { func (d *dbRepoImpl) GetDbList(condition *entity.DbQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] { qd := gormx.NewQueryWithTableName("t_db db"). Select("db.*, inst.name instance_name, inst.type instance_type"). - Joins("JOIN t_instance inst ON db.instance_id = inst.id"). + Joins("JOIN t_db_instance inst ON db.instance_id = inst.id"). Eq("db.instance_id", condition.InstanceId). Like("db.database", condition.Database). In("db.tag_id", condition.TagIds). diff --git a/server/mayfly-go.sql b/server/mayfly-go.sql index 113f6d0c..45081ea2 100644 --- a/server/mayfly-go.sql +++ b/server/mayfly-go.sql @@ -2,10 +2,10 @@ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- --- Table structure for t_instance +-- Table structure for t_db_instance -- ---------------------------- -DROP TABLE IF EXISTS `t_instance`; -CREATE TABLE `t_instance` ( +DROP TABLE IF EXISTS `t_db_instance`; +CREATE TABLE `t_db_instance` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '数据库实例名称', `host` varchar(100) COLLATE utf8mb4_bin NOT NULL, @@ -29,7 +29,7 @@ CREATE TABLE `t_instance` ( ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='数据库实例信息表'; -- ---------------------------- --- Records of t_instance +-- Records of t_db_instance -- ---------------------------- BEGIN; COMMIT; diff --git a/server/migrations/migrate-database-instance.sql b/server/migrations/migrate-database-instance.sql index 21b1cab3..b69299f6 100644 --- a/server/migrations/migrate-database-instance.sql +++ b/server/migrations/migrate-database-instance.sql @@ -1,4 +1,4 @@ -CREATE TABLE `t_instance` ( +CREATE TABLE `t_db_instance` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '数据库实例名称', `host` varchar(100) COLLATE utf8mb4_bin NOT NULL, @@ -40,15 +40,15 @@ INSERT INTO `t_sys_role_resource` (role_id,resource_id,creator_id,creator,create (1,137,1,'admin','2023-08-30 20:17:00', 0, NULL), (1,138,1,'admin','2023-08-30 20:17:00', 0, NULL); -INSERT INTO t_instance (`host`, `port`, `username`, `password`, `type`, `params`, `network`, `ssh_tunnel_machine_id`, `remark`, `create_time`, `creator_id`, `creator`, `update_time`, `modifier_id`, `modifier`, `is_deleted`, `delete_time`) +INSERT INTO t_db_instance (`host`, `port`, `username`, `password`, `type`, `params`, `network`, `ssh_tunnel_machine_id`, `remark`, `create_time`, `creator_id`, `creator`, `update_time`, `modifier_id`, `modifier`, `is_deleted`, `delete_time`) SELECT DISTINCT `host`, `port`, `username`, `password`, `type`, `params`, `network`, `ssh_tunnel_machine_id`, '', '2023-08-30 15:04:07', 1, 'admin', '2023-08-30 15:04:07', 1, 'admin', 0, NULL FROM t_db WHERE is_deleted = 0; -UPDATE t_instance SET name = CONCAT('instance_', id) +UPDATE t_db_instance SET name = CONCAT('instance_', id) WHERE name is NULL; -UPDATE t_db a, t_instance b SET a.instance_id = b.id +UPDATE t_db a, t_db_instance b SET a.instance_id = b.id WHERE a.`host`=b.`host` and a.`port`=b.`port` and a.`username`=b.`username` and a.`password`=b.`password` and a.`type`=b.`type` and a.`params`=b.`params` and a.`network`=b.`network` and a.`ssh_tunnel_machine_id`=b.`ssh_tunnel_machine_id`; COMMIT;