mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-04 00:10:25 +08:00
feature: 将数据库实例管理集成到数据库管理模块中
This commit is contained in:
@@ -14,9 +14,7 @@ import (
|
||||
"mayfly-go/pkg/gormx"
|
||||
"mayfly-go/pkg/model"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/cryptox"
|
||||
"mayfly-go/pkg/utils/stringx"
|
||||
"mayfly-go/pkg/utils/structx"
|
||||
"mayfly-go/pkg/ws"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -27,6 +25,7 @@ import (
|
||||
)
|
||||
|
||||
type Db struct {
|
||||
InstanceApp application.Instance
|
||||
DbApp application.Db
|
||||
DbSqlExecApp application.DbSqlExec
|
||||
MsgApp msgapp.Msg
|
||||
@@ -58,46 +57,22 @@ func (d *Db) Save(rc *req.Ctx) {
|
||||
form := &form.DbForm{}
|
||||
db := ginx.BindJsonAndCopyTo[*entity.Db](rc.GinCtx, form, new(entity.Db))
|
||||
|
||||
// 密码解密,并使用解密后的赋值
|
||||
originPwd, err := cryptox.DefaultRsaDecrypt(form.Password, true)
|
||||
biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
|
||||
db.Password = originPwd
|
||||
|
||||
// 密码脱敏记录日志
|
||||
form.Password = "****"
|
||||
rc.ReqParam = form
|
||||
|
||||
db.SetBaseInfo(rc.LoginAccount)
|
||||
d.DbApp.Save(db)
|
||||
}
|
||||
|
||||
// 获取数据库实例密码,由于数据库是加密存储,故提供该接口展示原文密码
|
||||
func (d *Db) GetDbPwd(rc *req.Ctx) {
|
||||
dbId := GetDbId(rc.GinCtx)
|
||||
dbEntity := d.DbApp.GetById(dbId, "Password")
|
||||
dbEntity.PwdDecrypt()
|
||||
rc.ResData = dbEntity.Password
|
||||
}
|
||||
|
||||
// 获取数据库实例的所有数据库名
|
||||
func (d *Db) GetDatabaseNames(rc *req.Ctx) {
|
||||
form := &form.DbForm{}
|
||||
ginx.BindJsonAndValid(rc.GinCtx, form)
|
||||
|
||||
db := new(entity.Db)
|
||||
structx.Copy(db, form)
|
||||
|
||||
// 密码解密,并使用解密后的赋值
|
||||
originPwd, err := cryptox.DefaultRsaDecrypt(form.Password, true)
|
||||
biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
|
||||
db.Password = originPwd
|
||||
|
||||
// 如果id不为空,并且密码为空则从数据库查询
|
||||
if form.Id != 0 && db.Password == "" {
|
||||
db = d.DbApp.GetById(form.Id)
|
||||
db.PwdDecrypt()
|
||||
}
|
||||
rc.ResData = d.DbApp.GetDatabases(db)
|
||||
instance := d.InstanceApp.GetById(form.InstanceId, "Password")
|
||||
biz.NotNil(instance, "获取数据库实例错误")
|
||||
instance.PwdDecrypt()
|
||||
rc.ResData = d.InstanceApp.GetDatabases(instance)
|
||||
rc.ResData = d.InstanceApp.GetDatabases(instance)
|
||||
}
|
||||
|
||||
func (d *Db) DeleteDb(rc *req.Ctx) {
|
||||
@@ -115,20 +90,29 @@ func (d *Db) DeleteDb(rc *req.Ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Db) getDbInstance(g *gin.Context) *application.DbInstance {
|
||||
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)
|
||||
}
|
||||
|
||||
func (d *Db) TableInfos(rc *req.Ctx) {
|
||||
rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta().GetTableInfos()
|
||||
rc.ResData = d.getDbInstance(rc.GinCtx).GetMeta().GetTableInfos()
|
||||
}
|
||||
|
||||
func (d *Db) TableIndex(rc *req.Ctx) {
|
||||
tn := rc.GinCtx.Query("tableName")
|
||||
biz.NotEmpty(tn, "tableName不能为空")
|
||||
rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta().GetTableIndex(tn)
|
||||
rc.ResData = d.getDbInstance(rc.GinCtx).GetMeta().GetTableIndex(tn)
|
||||
}
|
||||
|
||||
func (d *Db) GetCreateTableDdl(rc *req.Ctx) {
|
||||
tn := rc.GinCtx.Query("tableName")
|
||||
biz.NotEmpty(tn, "tableName不能为空")
|
||||
rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta().GetCreateTableDdl(tn)
|
||||
rc.ResData = d.getDbInstance(rc.GinCtx).GetMeta().GetCreateTableDdl(tn)
|
||||
}
|
||||
|
||||
func (d *Db) ExecSql(rc *req.Ctx) {
|
||||
@@ -136,9 +120,10 @@ func (d *Db) ExecSql(rc *req.Ctx) {
|
||||
form := &form.DbSqlExecForm{}
|
||||
ginx.BindJsonAndValid(g, form)
|
||||
|
||||
id := GetDbId(g)
|
||||
db := form.Db
|
||||
dbInstance := d.DbApp.GetDbInstance(id, db)
|
||||
dbId := getDbId(g)
|
||||
db := d.DbApp.GetById(dbId)
|
||||
instance := d.InstanceApp.GetById(db.InstanceId)
|
||||
dbInstance := d.DbApp.GetDbInstance(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)
|
||||
@@ -148,8 +133,8 @@ func (d *Db) ExecSql(rc *req.Ctx) {
|
||||
sql := stringx.TrimSpaceAndBr(form.Sql)
|
||||
|
||||
execReq := &application.DbSqlExecReq{
|
||||
DbId: id,
|
||||
Db: db,
|
||||
DbId: dbId,
|
||||
Db: form.Db,
|
||||
Remark: form.Remark,
|
||||
DbInstance: dbInstance,
|
||||
LoginAccount: rc.LoginAccount,
|
||||
@@ -192,9 +177,10 @@ func (d *Db) ExecSqlFile(rc *req.Ctx) {
|
||||
|
||||
file, _ := fileheader.Open()
|
||||
filename := fileheader.Filename
|
||||
dbId, db := GetIdAndDb(g)
|
||||
dbId := getDbId(g)
|
||||
dbName := getDbName(g)
|
||||
|
||||
dbInstance := d.DbApp.GetDbInstance(dbId, db)
|
||||
dbInstance := d.getDbInstance(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)
|
||||
|
||||
@@ -216,7 +202,7 @@ func (d *Db) ExecSqlFile(rc *req.Ctx) {
|
||||
|
||||
execReq := &application.DbSqlExecReq{
|
||||
DbId: dbId,
|
||||
Db: db,
|
||||
Db: dbName,
|
||||
Remark: fileheader.Filename,
|
||||
DbInstance: dbInstance,
|
||||
LoginAccount: rc.LoginAccount,
|
||||
@@ -249,7 +235,7 @@ func (d *Db) ExecSqlFile(rc *req.Ctx) {
|
||||
// 数据库dump
|
||||
func (d *Db) DumpSql(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
dbId, db := GetIdAndDb(g)
|
||||
db := getDbName(g)
|
||||
dumpType := g.Query("type")
|
||||
tablesStr := g.Query("tables")
|
||||
biz.NotEmpty(tablesStr, "请选择要导出的表")
|
||||
@@ -260,7 +246,7 @@ func (d *Db) DumpSql(rc *req.Ctx) {
|
||||
// 是否需要导出数据
|
||||
needData := dumpType == "2" || dumpType == "3"
|
||||
|
||||
dbInstance := d.DbApp.GetDbInstance(dbId, db)
|
||||
dbInstance := d.getDbInstance(rc.GinCtx)
|
||||
biz.ErrIsNilAppendErr(d.TagApp.CanAccess(rc.LoginAccount.Id, dbInstance.Info.TagPath), "%s")
|
||||
|
||||
now := time.Now()
|
||||
@@ -275,7 +261,7 @@ func (d *Db) DumpSql(rc *req.Ctx) {
|
||||
writer.WriteString(fmt.Sprintf("\n-- 导出数据库: %s ", db))
|
||||
writer.WriteString("\n-- ----------------------------\n")
|
||||
|
||||
dbmeta := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta()
|
||||
dbmeta := d.getDbInstance(rc.GinCtx).GetMeta()
|
||||
for _, table := range tables {
|
||||
if needStruct {
|
||||
writer.WriteString(fmt.Sprintf("\n-- ----------------------------\n-- 表结构: %s \n-- ----------------------------\n", table))
|
||||
@@ -329,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.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx))
|
||||
dbi := d.getDbInstance(rc.GinCtx)
|
||||
rc.ResData = dbi.GetMeta().GetTables()
|
||||
}
|
||||
|
||||
@@ -339,13 +325,13 @@ func (d *Db) ColumnMA(rc *req.Ctx) {
|
||||
tn := g.Query("tableName")
|
||||
biz.NotEmpty(tn, "tableName不能为空")
|
||||
|
||||
dbi := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx))
|
||||
dbi := d.getDbInstance(rc.GinCtx)
|
||||
rc.ResData = dbi.GetMeta().GetColumns(tn)
|
||||
}
|
||||
|
||||
// @router /api/db/:dbId/hint-tables [get]
|
||||
func (d *Db) HintTables(rc *req.Ctx) {
|
||||
dbi := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx))
|
||||
dbi := d.getDbInstance(rc.GinCtx)
|
||||
|
||||
dm := dbi.GetMeta()
|
||||
// 获取所有表
|
||||
@@ -391,7 +377,7 @@ func (d *Db) SaveSql(rc *req.Ctx) {
|
||||
ginx.BindJsonAndValid(g, dbSqlForm)
|
||||
rc.ReqParam = dbSqlForm
|
||||
|
||||
dbId := GetDbId(g)
|
||||
dbId := getDbId(g)
|
||||
// 判断dbId是否存在
|
||||
err := gormx.GetById(new(entity.Db), dbId)
|
||||
biz.ErrIsNil(err, "该数据库信息不存在")
|
||||
@@ -413,9 +399,10 @@ func (d *Db) SaveSql(rc *req.Ctx) {
|
||||
|
||||
// 获取所有保存的sql names
|
||||
func (d *Db) GetSqlNames(rc *req.Ctx) {
|
||||
id, db := GetIdAndDb(rc.GinCtx)
|
||||
dbId := getDbId(rc.GinCtx)
|
||||
dbName := getDbName(rc.GinCtx)
|
||||
// 获取用于是否有该dbsql的保存记录,有则更改,否则新增
|
||||
dbSql := &entity.DbSql{Type: 1, DbId: id, Db: db}
|
||||
dbSql := &entity.DbSql{Type: 1, DbId: dbId, Db: dbName}
|
||||
dbSql.CreatorId = rc.LoginAccount.Id
|
||||
var sqls []entity.DbSql
|
||||
gormx.ListBy(dbSql, &sqls, "id", "name")
|
||||
@@ -425,7 +412,7 @@ func (d *Db) GetSqlNames(rc *req.Ctx) {
|
||||
|
||||
// 删除保存的sql
|
||||
func (d *Db) DeleteSql(rc *req.Ctx) {
|
||||
dbSql := &entity.DbSql{Type: 1, DbId: GetDbId(rc.GinCtx)}
|
||||
dbSql := &entity.DbSql{Type: 1, DbId: getDbId(rc.GinCtx)}
|
||||
dbSql.CreatorId = rc.LoginAccount.Id
|
||||
dbSql.Name = rc.GinCtx.Query("name")
|
||||
dbSql.Db = rc.GinCtx.Query("db")
|
||||
@@ -436,9 +423,10 @@ func (d *Db) DeleteSql(rc *req.Ctx) {
|
||||
|
||||
// @router /api/db/:dbId/sql [get]
|
||||
func (d *Db) GetSql(rc *req.Ctx) {
|
||||
id, db := GetIdAndDb(rc.GinCtx)
|
||||
dbId := getDbId(rc.GinCtx)
|
||||
dbName := getDbName(rc.GinCtx)
|
||||
// 根据创建者id, 数据库id,以及sql模板名称查询保存的sql信息
|
||||
dbSql := &entity.DbSql{Type: 1, DbId: id, Db: db}
|
||||
dbSql := &entity.DbSql{Type: 1, DbId: dbId, Db: dbName}
|
||||
dbSql.CreatorId = rc.LoginAccount.Id
|
||||
dbSql.Name = rc.GinCtx.Query("name")
|
||||
|
||||
@@ -449,14 +437,14 @@ func (d *Db) GetSql(rc *req.Ctx) {
|
||||
rc.ResData = dbSql
|
||||
}
|
||||
|
||||
func GetDbId(g *gin.Context) uint64 {
|
||||
func getDbId(g *gin.Context) uint64 {
|
||||
dbId, _ := strconv.Atoi(g.Param("dbId"))
|
||||
biz.IsTrue(dbId > 0, "dbId错误")
|
||||
return uint64(dbId)
|
||||
}
|
||||
|
||||
func GetIdAndDb(g *gin.Context) (uint64, string) {
|
||||
func getDbName(g *gin.Context) string {
|
||||
db := g.Query("db")
|
||||
biz.NotEmpty(db, "db不能为空")
|
||||
return GetDbId(g), db
|
||||
return db
|
||||
}
|
||||
|
||||
@@ -1,19 +1,13 @@
|
||||
package form
|
||||
|
||||
type DbForm struct {
|
||||
Id uint64 `json:"id"`
|
||||
Name string `binding:"required" json:"name"`
|
||||
Type string `binding:"required" json:"type"` // 类型,mysql oracle等
|
||||
Host string `binding:"required" json:"host"`
|
||||
Port int `binding:"required" json:"port"`
|
||||
Username string `binding:"required" json:"username"`
|
||||
Password string `json:"password"`
|
||||
Params string `json:"params"`
|
||||
Database string `json:"database"`
|
||||
Remark string `json:"remark"`
|
||||
TagId uint64 `binding:"required" json:"tagId"`
|
||||
TagPath string `binding:"required" json:"tagPath"`
|
||||
SshTunnelMachineId int `json:"sshTunnelMachineId"`
|
||||
Id uint64 `json:"id"`
|
||||
Name string `binding:"required" json:"name"`
|
||||
Database string `json:"database"`
|
||||
Remark string `json:"remark"`
|
||||
TagId uint64 `binding:"required" json:"tagId"`
|
||||
TagPath string `binding:"required" json:"tagPath"`
|
||||
InstanceId uint64 `binding:"required" json:"instanceId"`
|
||||
}
|
||||
|
||||
type DbSqlSaveForm struct {
|
||||
|
||||
@@ -17,15 +17,19 @@ import (
|
||||
|
||||
type Instance struct {
|
||||
InstanceApp application.Instance
|
||||
DbApp application.Db
|
||||
MsgApp msgapp.Msg
|
||||
}
|
||||
|
||||
// Instances 获取数据库实例信息
|
||||
// @router /api/instances [get]
|
||||
func (d *Instance) Instances(rc *req.Ctx) {
|
||||
queryCond, page := ginx.BindQueryAndPage[*entity.InstanceQuery](rc.GinCtx, new(entity.InstanceQuery))
|
||||
rc.ResData = d.InstanceApp.GetPageList(queryCond, page, new([]vo.SelectDataInstanceVO))
|
||||
}
|
||||
|
||||
// SaveInstance 保存数据库实例信息
|
||||
// @router /api/instances [post]
|
||||
func (d *Instance) SaveInstance(rc *req.Ctx) {
|
||||
form := &form.InstanceForm{}
|
||||
instance := ginx.BindJsonAndCopyTo[*entity.Instance](rc.GinCtx, form, new(entity.Instance))
|
||||
@@ -43,29 +47,57 @@ func (d *Instance) SaveInstance(rc *req.Ctx) {
|
||||
d.InstanceApp.Save(instance)
|
||||
}
|
||||
|
||||
// 获取数据库实例密码,由于数据库是加密存储,故提供该接口展示原文密码
|
||||
func (d *Instance) GetInstancePwd(rc *req.Ctx) {
|
||||
dbId := GetInstanceId(rc.GinCtx)
|
||||
dbEntity := d.InstanceApp.GetById(dbId, "Password")
|
||||
dbEntity.PwdDecrypt()
|
||||
rc.ResData = dbEntity.Password
|
||||
// GetInstance 获取数据库实例密码,由于数据库是加密存储,故提供该接口展示原文密码
|
||||
// @router /api/instances/:instance [GET]
|
||||
func (d *Instance) GetInstance(rc *req.Ctx) {
|
||||
dbId := getInstanceId(rc.GinCtx)
|
||||
dbEntity := d.InstanceApp.GetById(dbId)
|
||||
biz.IsTrue(dbEntity != nil, "获取数据库实例错误")
|
||||
dbEntity.Password = ""
|
||||
rc.ResData = dbEntity
|
||||
}
|
||||
|
||||
// GetInstancePwd 获取数据库实例密码,由于数据库是加密存储,故提供该接口展示原文密码
|
||||
// @router /api/instances/:instance/pwd [GET]
|
||||
func (d *Instance) GetInstancePwd(rc *req.Ctx) {
|
||||
instanceId := getInstanceId(rc.GinCtx)
|
||||
instanceEntity := d.InstanceApp.GetById(instanceId, "Password")
|
||||
biz.IsTrue(instanceEntity != nil, "获取数据库实例错误")
|
||||
instanceEntity.PwdDecrypt()
|
||||
rc.ResData = instanceEntity.Password
|
||||
}
|
||||
|
||||
// DeleteInstance 删除数据库实例信息
|
||||
// @router /api/instances/:instance [DELETE]
|
||||
func (d *Instance) DeleteInstance(rc *req.Ctx) {
|
||||
idsStr := ginx.PathParam(rc.GinCtx, "dbId")
|
||||
idsStr := ginx.PathParam(rc.GinCtx, "instanceId")
|
||||
rc.ReqParam = idsStr
|
||||
ids := strings.Split(idsStr, ",")
|
||||
|
||||
for _, v := range ids {
|
||||
value, err := strconv.Atoi(v)
|
||||
biz.ErrIsNilAppendErr(err, "string类型转换为int异常: %s")
|
||||
dbId := uint64(value)
|
||||
d.InstanceApp.Delete(dbId)
|
||||
instanceId := uint64(value)
|
||||
if d.DbApp.Count(&entity.DbQuery{InstanceId: instanceId}) != 0 {
|
||||
instance := d.InstanceApp.GetById(instanceId, "name")
|
||||
biz.NotNil(instance, "获取数据库实例错误,数据库实例ID为:%d", instance.Id)
|
||||
biz.IsTrue(false, "不能删除数据库实例【%s】,请先删除其关联的数据库资源。", instance.Name)
|
||||
}
|
||||
d.InstanceApp.Delete(instanceId)
|
||||
}
|
||||
}
|
||||
|
||||
func GetInstanceId(g *gin.Context) uint64 {
|
||||
dbId, _ := strconv.Atoi(g.Param("dbId"))
|
||||
biz.IsTrue(dbId > 0, "dbId错误")
|
||||
return uint64(dbId)
|
||||
func getInstanceId(g *gin.Context) uint64 {
|
||||
instanceId, _ := strconv.Atoi(g.Param("instanceId"))
|
||||
biz.IsTrue(instanceId > 0, "instanceId 错误")
|
||||
return uint64(instanceId)
|
||||
}
|
||||
|
||||
// 获取数据库实例的所有数据库名
|
||||
func (d *Instance) GetDatabaseNames(rc *req.Ctx) {
|
||||
instanceId := getInstanceId(rc.GinCtx)
|
||||
instance := d.InstanceApp.GetById(instanceId, "Password")
|
||||
biz.IsTrue(instance != nil, "获取数据库实例错误")
|
||||
instance.PwdDecrypt()
|
||||
rc.ResData = d.InstanceApp.GetDatabases(instance)
|
||||
}
|
||||
|
||||
@@ -4,24 +4,21 @@ import "time"
|
||||
|
||||
type SelectDataDbVO struct {
|
||||
//models.BaseModel
|
||||
Id *int64 `json:"id"`
|
||||
Name *string `json:"name"`
|
||||
Host *string `json:"host"`
|
||||
Port *int `json:"port"`
|
||||
Type *string `json:"type"`
|
||||
Params *string `json:"params"`
|
||||
Database *string `json:"database"`
|
||||
Username *string `json:"username"`
|
||||
Remark *string `json:"remark"`
|
||||
TagId *int64 `json:"tagId"`
|
||||
TagPath *string `json:"tagPath"`
|
||||
Id *int64 `json:"id"`
|
||||
Name *string `json:"name"`
|
||||
Database *string `json:"database"`
|
||||
Remark *string `json:"remark"`
|
||||
TagId *int64 `json:"tagId"`
|
||||
TagPath *string `json:"tagPath"`
|
||||
|
||||
InstanceId *int64 `json:"instanceId"`
|
||||
InstanceName *string `json:"instanceName"`
|
||||
InstanceType *string `json:"type"`
|
||||
|
||||
CreateTime *time.Time `json:"createTime"`
|
||||
Creator *string `json:"creator"`
|
||||
CreatorId *int64 `json:"creatorId"`
|
||||
|
||||
UpdateTime *time.Time `json:"updateTime"`
|
||||
Modifier *string `json:"modifier"`
|
||||
ModifierId *int64 `json:"modifierId"`
|
||||
|
||||
SshTunnelMachineId int `json:"sshTunnelMachineId"`
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"mayfly-go/pkg/logx"
|
||||
"mayfly-go/pkg/model"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
"mayfly-go/pkg/utils/structx"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -40,10 +39,7 @@ type Db interface {
|
||||
// 获取数据库连接实例
|
||||
// @param id 数据库实例id
|
||||
// @param db 数据库
|
||||
GetDbInstance(id uint64, db string) *DbInstance
|
||||
|
||||
// 获取数据库实例的所有数据库列表
|
||||
GetDatabases(entity *entity.Db) []string
|
||||
GetDbInstance(db *entity.Db, instance *entity.Instance, dbName string) *DbInstance
|
||||
}
|
||||
|
||||
func newDbApp(dbRepo repository.Db, dbSqlRepo repository.DbSql) Db {
|
||||
@@ -78,32 +74,19 @@ func (d *dbAppImpl) GetById(id uint64, cols ...string) *entity.Db {
|
||||
}
|
||||
|
||||
func (d *dbAppImpl) Save(dbEntity *entity.Db) {
|
||||
// 默认tcp连接
|
||||
dbEntity.Network = dbEntity.GetNetwork()
|
||||
|
||||
// 测试连接
|
||||
if dbEntity.Password != "" {
|
||||
TestConnection(dbEntity)
|
||||
}
|
||||
|
||||
// 查找是否存在该库
|
||||
oldDb := &entity.Db{Host: dbEntity.Host, Port: dbEntity.Port, Username: dbEntity.Username}
|
||||
if dbEntity.SshTunnelMachineId > 0 {
|
||||
oldDb.SshTunnelMachineId = dbEntity.SshTunnelMachineId
|
||||
}
|
||||
oldDb := &entity.Db{Name: dbEntity.Name}
|
||||
err := d.GetDbBy(oldDb)
|
||||
|
||||
if dbEntity.Id == 0 {
|
||||
biz.NotEmpty(dbEntity.Password, "密码不能为空")
|
||||
biz.IsTrue(err != nil, "该数据库实例已存在")
|
||||
dbEntity.PwdEncrypt()
|
||||
biz.IsTrue(err != nil, "该数据库资源已存在")
|
||||
d.dbRepo.Insert(dbEntity)
|
||||
return
|
||||
}
|
||||
|
||||
// 如果存在该库,则校验修改的库是否为该库
|
||||
if err == nil {
|
||||
biz.IsTrue(oldDb.Id == dbEntity.Id, "该数据库实例已存在")
|
||||
biz.IsTrue(oldDb.Id == dbEntity.Id, "该数据库资源已存在")
|
||||
}
|
||||
|
||||
dbId := dbEntity.Id
|
||||
@@ -129,7 +112,6 @@ func (d *dbAppImpl) Save(dbEntity *entity.Db) {
|
||||
d.dbSqlRepo.DeleteBy(&entity.DbSql{DbId: dbId, Db: v.(string)})
|
||||
}
|
||||
|
||||
dbEntity.PwdEncrypt()
|
||||
d.dbRepo.Update(dbEntity)
|
||||
}
|
||||
|
||||
@@ -145,39 +127,13 @@ func (d *dbAppImpl) Delete(id uint64) {
|
||||
d.dbSqlRepo.DeleteBy(&entity.DbSql{DbId: id})
|
||||
}
|
||||
|
||||
func (d *dbAppImpl) GetDatabases(ed *entity.Db) []string {
|
||||
ed.Network = ed.GetNetwork()
|
||||
databases := make([]string, 0)
|
||||
var dbConn *sql.DB
|
||||
var metaDb string
|
||||
var getDatabasesSql string
|
||||
if ed.Type == entity.DbTypeMysql {
|
||||
metaDb = "information_schema"
|
||||
getDatabasesSql = "SELECT SCHEMA_NAME AS dbname FROM SCHEMATA"
|
||||
} else {
|
||||
metaDb = "postgres"
|
||||
getDatabasesSql = "SELECT datname AS dbname FROM pg_database"
|
||||
}
|
||||
|
||||
dbConn, err := GetDbConn(ed, metaDb)
|
||||
biz.ErrIsNilAppendErr(err, "数据库连接失败: %s")
|
||||
defer dbConn.Close()
|
||||
|
||||
_, res, err := SelectDataByDb(dbConn, getDatabasesSql)
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库列表失败")
|
||||
for _, re := range res {
|
||||
databases = append(databases, re["dbname"].(string))
|
||||
}
|
||||
return databases
|
||||
}
|
||||
|
||||
var mutex sync.Mutex
|
||||
|
||||
func (da *dbAppImpl) GetDbInstance(id uint64, db string) *DbInstance {
|
||||
cacheKey := GetDbCacheKey(id, db)
|
||||
func (d *dbAppImpl) GetDbInstance(db *entity.Db, instance *entity.Instance, dbName string) *DbInstance {
|
||||
cacheKey := GetDbCacheKey(db.Id, dbName)
|
||||
|
||||
// Id不为0,则为需要缓存
|
||||
needCache := id != 0
|
||||
needCache := db.Id != 0
|
||||
if needCache {
|
||||
load, ok := dbCache.Get(cacheKey)
|
||||
if ok {
|
||||
@@ -187,33 +143,32 @@ func (da *dbAppImpl) GetDbInstance(id uint64, db string) *DbInstance {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
d := da.GetById(id)
|
||||
biz.NotNil(d, "数据库信息不存在")
|
||||
biz.IsTrue(strings.Contains(d.Database, db), "未配置该库的操作权限")
|
||||
// 密码解密
|
||||
d.PwdDecrypt()
|
||||
biz.NotNil(db, "数据库信息不存在")
|
||||
biz.IsTrue(strings.Contains(" "+db.Database+" ", " "+dbName+" "), "未配置该库的操作权限")
|
||||
|
||||
dbInfo := new(DbInfo)
|
||||
structx.Copy(dbInfo, d)
|
||||
dbInfo.Database = db
|
||||
// 密码解密
|
||||
instance.PwdDecrypt()
|
||||
|
||||
dbInfo := NewDbInfo(db, instance)
|
||||
dbInfo.Database = dbName
|
||||
dbi := &DbInstance{Id: cacheKey, Info: dbInfo}
|
||||
|
||||
DB, err := GetDbConn(d, db)
|
||||
conn, err := getInstanceConn(instance, dbName)
|
||||
if err != nil {
|
||||
dbi.Close()
|
||||
logx.Errorf("连接db失败: %s:%d/%s", d.Host, d.Port, db)
|
||||
logx.Errorf("连接db失败: %s:%d/%s", dbInfo.Host, dbInfo.Port, dbName)
|
||||
panic(biz.NewBizErr(fmt.Sprintf("数据库连接失败: %s", err.Error())))
|
||||
}
|
||||
|
||||
// 最大连接周期,超过时间的连接就close
|
||||
// DB.SetConnMaxLifetime(100 * time.Second)
|
||||
// conn.SetConnMaxLifetime(100 * time.Second)
|
||||
// 设置最大连接数
|
||||
DB.SetMaxOpenConns(5)
|
||||
conn.SetMaxOpenConns(5)
|
||||
// 设置闲置连接数
|
||||
DB.SetMaxIdleConns(1)
|
||||
conn.SetMaxIdleConns(1)
|
||||
|
||||
dbi.db = DB
|
||||
logx.Infof("连接db: %s:%d/%s", d.Host, d.Port, db)
|
||||
dbi.db = conn
|
||||
logx.Infof("连接db: %s:%d/%s", dbInfo.Host, dbInfo.Port, dbName)
|
||||
if needCache {
|
||||
dbCache.Put(cacheKey, dbi)
|
||||
}
|
||||
@@ -235,6 +190,19 @@ type DbInfo struct {
|
||||
SshTunnelMachineId int
|
||||
}
|
||||
|
||||
func NewDbInfo(db *entity.Db, instance *entity.Instance) *DbInfo {
|
||||
return &DbInfo{
|
||||
Id: db.Id,
|
||||
Name: db.Name,
|
||||
Type: instance.Type,
|
||||
Host: instance.Host,
|
||||
Port: instance.Port,
|
||||
Username: instance.Username,
|
||||
TagPath: db.TagPath,
|
||||
SshTunnelMachineId: instance.SshTunnelMachineId,
|
||||
}
|
||||
}
|
||||
|
||||
// 获取记录日志的描述
|
||||
func (d *DbInfo) GetLogDesc() string {
|
||||
return fmt.Sprintf("DB[id=%d, tag=%s, name=%s, ip=%s:%d, database=%s]", d.Id, d.TagPath, d.Name, d.Host, d.Port, d.Database)
|
||||
@@ -325,35 +293,6 @@ func GetDbInstanceByCache(id string) *DbInstance {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestConnection(d *entity.Db) {
|
||||
// 验证第一个库是否可以连接即可
|
||||
DB, err := GetDbConn(d, strings.Split(d.Database, " ")[0])
|
||||
biz.ErrIsNilAppendErr(err, "数据库连接失败: %s")
|
||||
defer DB.Close()
|
||||
}
|
||||
|
||||
// 获取数据库连接
|
||||
func GetDbConn(d *entity.Db, db string) (*sql.DB, error) {
|
||||
var DB *sql.DB
|
||||
var err error
|
||||
if d.Type == entity.DbTypeMysql {
|
||||
DB, err = getMysqlDB(d, db)
|
||||
} else if d.Type == entity.DbTypePostgres {
|
||||
DB, err = getPgsqlDB(d, db)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = DB.Ping()
|
||||
if err != nil {
|
||||
DB.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return DB, nil
|
||||
}
|
||||
|
||||
func SelectDataByDb(db *sql.DB, selectSql string) ([]string, []map[string]any, error) {
|
||||
rows, err := db.Query(selectSql)
|
||||
if err != nil {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"mayfly-go/internal/db/domain/entity"
|
||||
"mayfly-go/internal/db/domain/repository"
|
||||
"mayfly-go/pkg/biz"
|
||||
@@ -8,21 +9,24 @@ import (
|
||||
)
|
||||
|
||||
type Instance interface {
|
||||
// 分页获取
|
||||
// GetPageList 分页获取数据库实例
|
||||
GetPageList(condition *entity.InstanceQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
|
||||
|
||||
Count(condition *entity.InstanceQuery) int64
|
||||
|
||||
// 根据条件获取
|
||||
// GetInstanceBy 根据条件获取数据库实例
|
||||
GetInstanceBy(condition *entity.Instance, cols ...string) error
|
||||
|
||||
// 根据id获取
|
||||
// GetById 根据id获取数据库实例
|
||||
GetById(id uint64, cols ...string) *entity.Instance
|
||||
|
||||
Save(instanceEntity *entity.Instance)
|
||||
|
||||
// 删除数据库信息
|
||||
// Delete 删除数据库信息
|
||||
Delete(id uint64)
|
||||
|
||||
// GetDatabases 获取数据库实例的所有数据库列表
|
||||
GetDatabases(entity *entity.Instance) []string
|
||||
}
|
||||
|
||||
func newInstanceApp(InstanceRepo repository.Instance) Instance {
|
||||
@@ -35,34 +39,33 @@ type instanceAppImpl struct {
|
||||
instanceRepo repository.Instance
|
||||
}
|
||||
|
||||
// 分页获取数据库信息列表
|
||||
func (d *instanceAppImpl) GetPageList(condition *entity.InstanceQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
|
||||
return d.instanceRepo.GetInstanceList(condition, pageParam, toEntity, orderBy...)
|
||||
// GetPageList 分页获取数据库实例
|
||||
func (app *instanceAppImpl) GetPageList(condition *entity.InstanceQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
|
||||
return app.instanceRepo.GetInstanceList(condition, pageParam, toEntity, orderBy...)
|
||||
}
|
||||
|
||||
func (d *instanceAppImpl) Count(condition *entity.InstanceQuery) int64 {
|
||||
return d.instanceRepo.Count(condition)
|
||||
func (app *instanceAppImpl) Count(condition *entity.InstanceQuery) int64 {
|
||||
return app.instanceRepo.Count(condition)
|
||||
}
|
||||
|
||||
// 根据条件获取
|
||||
func (d *instanceAppImpl) GetInstanceBy(condition *entity.Instance, cols ...string) error {
|
||||
return d.instanceRepo.GetInstance(condition, cols...)
|
||||
// GetInstanceBy 根据条件获取数据库实例
|
||||
func (app *instanceAppImpl) GetInstanceBy(condition *entity.Instance, cols ...string) error {
|
||||
return app.instanceRepo.GetInstance(condition, cols...)
|
||||
}
|
||||
|
||||
// 根据id获取
|
||||
func (d *instanceAppImpl) GetById(id uint64, cols ...string) *entity.Instance {
|
||||
return d.instanceRepo.GetById(id, cols...)
|
||||
// GetById 根据id获取数据库实例
|
||||
func (app *instanceAppImpl) GetById(id uint64, cols ...string) *entity.Instance {
|
||||
return app.instanceRepo.GetById(id, cols...)
|
||||
}
|
||||
|
||||
func (d *instanceAppImpl) Save(instanceEntity *entity.Instance) {
|
||||
func (app *instanceAppImpl) Save(instanceEntity *entity.Instance) {
|
||||
// 默认tcp连接
|
||||
instanceEntity.Network = instanceEntity.GetNetwork()
|
||||
|
||||
// 测试连接
|
||||
// todo 测试数据库连接
|
||||
//if instanceEntity.Password != "" {
|
||||
// TestConnection(instanceEntity)
|
||||
//}
|
||||
if instanceEntity.Password != "" {
|
||||
testConnection(instanceEntity)
|
||||
}
|
||||
|
||||
// 查找是否存在该库
|
||||
oldInstance := &entity.Instance{Host: instanceEntity.Host, Port: instanceEntity.Port, Username: instanceEntity.Username}
|
||||
@@ -70,23 +73,77 @@ func (d *instanceAppImpl) Save(instanceEntity *entity.Instance) {
|
||||
oldInstance.SshTunnelMachineId = instanceEntity.SshTunnelMachineId
|
||||
}
|
||||
|
||||
err := d.GetInstanceBy(oldInstance)
|
||||
err := app.GetInstanceBy(oldInstance)
|
||||
if instanceEntity.Id == 0 {
|
||||
biz.NotEmpty(instanceEntity.Password, "密码不能为空")
|
||||
biz.IsTrue(err != nil, "该数据库实例已存在")
|
||||
instanceEntity.PwdEncrypt()
|
||||
d.instanceRepo.Insert(instanceEntity)
|
||||
app.instanceRepo.Insert(instanceEntity)
|
||||
} else {
|
||||
// 如果存在该库,则校验修改的库是否为该库
|
||||
if err == nil {
|
||||
biz.IsTrue(oldInstance.Id == instanceEntity.Id, "该数据库实例已存在")
|
||||
}
|
||||
instanceEntity.PwdEncrypt()
|
||||
d.instanceRepo.Update(instanceEntity)
|
||||
app.instanceRepo.Update(instanceEntity)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *instanceAppImpl) Delete(id uint64) {
|
||||
// todo 删除数据库库实例前必须删除关联数据库
|
||||
d.instanceRepo.Delete(id)
|
||||
func (app *instanceAppImpl) Delete(id uint64) {
|
||||
app.instanceRepo.Delete(id)
|
||||
}
|
||||
|
||||
// getInstanceConn 获取数据库连接数据库实例
|
||||
func getInstanceConn(instance *entity.Instance, db string) (*sql.DB, error) {
|
||||
var conn *sql.DB
|
||||
var err error
|
||||
if instance.Type == entity.DbTypeMysql {
|
||||
conn, err = getMysqlDB(instance, db)
|
||||
} else if instance.Type == entity.DbTypePostgres {
|
||||
conn, err = getPgsqlDB(instance, db)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = conn.Ping()
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func testConnection(d *entity.Instance) {
|
||||
// 不指定数据库名称
|
||||
conn, err := getInstanceConn(d, "")
|
||||
biz.ErrIsNilAppendErr(err, "数据库连接失败: %s")
|
||||
defer conn.Close()
|
||||
}
|
||||
|
||||
func (app *instanceAppImpl) GetDatabases(ed *entity.Instance) []string {
|
||||
ed.Network = ed.GetNetwork()
|
||||
databases := make([]string, 0)
|
||||
var dbConn *sql.DB
|
||||
var metaDb string
|
||||
var getDatabasesSql string
|
||||
if ed.Type == entity.DbTypeMysql {
|
||||
metaDb = "information_schema"
|
||||
getDatabasesSql = "SELECT SCHEMA_NAME AS dbname FROM SCHEMATA"
|
||||
} else {
|
||||
metaDb = "postgres"
|
||||
getDatabasesSql = "SELECT datname AS dbname FROM pg_database"
|
||||
}
|
||||
|
||||
dbConn, err := getInstanceConn(ed, metaDb)
|
||||
biz.ErrIsNilAppendErr(err, "数据库连接失败: %s")
|
||||
defer dbConn.Close()
|
||||
|
||||
_, res, err := SelectDataByDb(dbConn, getDatabasesSql)
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库列表失败")
|
||||
for _, re := range res {
|
||||
databases = append(databases, re["dbname"].(string))
|
||||
}
|
||||
return databases
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
"github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func getMysqlDB(d *entity.Db, db string) (*sql.DB, error) {
|
||||
func getMysqlDB(d *entity.Instance, db string) (*sql.DB, error) {
|
||||
// SSH Conect
|
||||
if d.SshTunnelMachineId > 0 {
|
||||
sshTunnelMachine := machineapp.GetMachineApp().GetSshTunnelMachine(d.SshTunnelMachineId)
|
||||
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
func getPgsqlDB(d *entity.Db, db string) (*sql.DB, error) {
|
||||
func getPgsqlDB(d *entity.Instance, db string) (*sql.DB, error) {
|
||||
driverName := d.Type
|
||||
// SSH Conect
|
||||
if d.SshTunnelMachineId > 0 {
|
||||
@@ -28,7 +28,11 @@ func getPgsqlDB(d *entity.Db, db string) (*sql.DB, error) {
|
||||
sql.Drivers()
|
||||
}
|
||||
|
||||
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", d.Host, d.Port, d.Username, d.Password, db)
|
||||
var dbParam string
|
||||
if db != "" {
|
||||
dbParam = "dbname=" + db
|
||||
}
|
||||
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s %s sslmode=disable", d.Host, d.Port, d.Username, d.Password, dbParam)
|
||||
if d.Params != "" {
|
||||
dsn = fmt.Sprintf("%s %s", dsn, strings.Join(strings.Split(d.Params, "&"), " "))
|
||||
}
|
||||
|
||||
@@ -1,48 +1,16 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"mayfly-go/internal/common/utils"
|
||||
"mayfly-go/pkg/model"
|
||||
)
|
||||
|
||||
type Db struct {
|
||||
model.Model
|
||||
|
||||
Name string `orm:"column(name)" json:"name"`
|
||||
Type string `orm:"column(type)" json:"type"` // 类型,mysql oracle等
|
||||
Host string `orm:"column(host)" json:"host"`
|
||||
Port int `orm:"column(port)" json:"port"`
|
||||
Network string `orm:"column(network)" json:"network"`
|
||||
Username string `orm:"column(username)" json:"username"`
|
||||
Password string `orm:"column(password)" json:"-"`
|
||||
Database string `orm:"column(database)" json:"database"`
|
||||
Params string `json:"params"`
|
||||
Remark string `json:"remark"`
|
||||
TagId uint64
|
||||
TagPath string
|
||||
SshTunnelMachineId int `orm:"column(ssh_tunnel_machine_id)" json:"sshTunnelMachineId"` // ssh隧道机器id
|
||||
}
|
||||
|
||||
// 获取数据库连接网络, 若没有使用ssh隧道,则直接返回。否则返回拼接的网络需要注册至指定dial
|
||||
func (d *Db) GetNetwork() string {
|
||||
network := d.Network
|
||||
if d.SshTunnelMachineId <= 0 {
|
||||
if network == "" {
|
||||
return "tcp"
|
||||
} else {
|
||||
return network
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%s+ssh:%d", d.Type, d.SshTunnelMachineId)
|
||||
}
|
||||
|
||||
func (d *Db) PwdEncrypt() {
|
||||
// 密码替换为加密后的密码
|
||||
d.Password = utils.PwdAesEncrypt(d.Password)
|
||||
}
|
||||
|
||||
func (d *Db) PwdDecrypt() {
|
||||
// 密码替换为解密后的密码
|
||||
d.Password = utils.PwdAesDecrypt(d.Password)
|
||||
Name string `orm:"column(name)" json:"name"`
|
||||
Database string `orm:"column(database)" json:"database"`
|
||||
Remark string `json:"remark"`
|
||||
TagId uint64
|
||||
TagPath string
|
||||
InstanceId uint64
|
||||
}
|
||||
|
||||
@@ -22,18 +22,13 @@ type DbQuery struct {
|
||||
model.Model
|
||||
|
||||
Name string `orm:"column(name)" json:"name"`
|
||||
Type string `orm:"column(type)" json:"type"` // 类型,mysql oracle等
|
||||
Host string `orm:"column(host)" json:"host"`
|
||||
Port int `orm:"column(port)" json:"port"`
|
||||
Network string `orm:"column(network)" json:"network"`
|
||||
Username string `orm:"column(username)" json:"username"`
|
||||
Password string `orm:"column(password)" json:"-"`
|
||||
Database string `orm:"column(database)" json:"database"`
|
||||
Params string `json:"params"`
|
||||
Remark string `json:"remark"`
|
||||
|
||||
TagIds []uint64
|
||||
TagPath string `form:"tagPath"`
|
||||
TagIds []uint64 `orm:"column(tag_id)"`
|
||||
TagPath string `form:"tagPath"`
|
||||
|
||||
InstanceId uint64 `form:"instanceId"`
|
||||
}
|
||||
|
||||
type DbSqlExecQuery struct {
|
||||
|
||||
@@ -6,12 +6,12 @@ import (
|
||||
)
|
||||
|
||||
type Db interface {
|
||||
// 分页获取机器信息列表
|
||||
// 分页获取数据信息列表
|
||||
GetDbList(condition *entity.DbQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
|
||||
|
||||
Count(condition *entity.DbQuery) int64
|
||||
|
||||
// 根据条件获取账号信息
|
||||
// 根据条件获取数据库信息
|
||||
GetDb(condition *entity.Db, cols ...string) error
|
||||
|
||||
// 根据id获取
|
||||
|
||||
@@ -11,7 +11,7 @@ type Instance interface {
|
||||
|
||||
Count(condition *entity.InstanceQuery) int64
|
||||
|
||||
// 根据条件获取账号信息
|
||||
// 根据条件获取实例信息
|
||||
GetInstance(condition *entity.Instance, cols ...string) error
|
||||
|
||||
// 根据id获取
|
||||
|
||||
@@ -16,12 +16,17 @@ func newDbRepo() repository.Db {
|
||||
|
||||
// 分页获取数据库信息列表
|
||||
func (d *dbRepoImpl) GetDbList(condition *entity.DbQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
|
||||
qd := gormx.NewQuery(new(entity.Db)).
|
||||
Like("host", condition.Host).
|
||||
Like("database", condition.Database).
|
||||
In("tag_id", condition.TagIds).
|
||||
RLike("tag_path", condition.TagPath).
|
||||
OrderByAsc("tag_path")
|
||||
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").
|
||||
Eq("db.instance_id", condition.InstanceId).
|
||||
Like("db.database", condition.Database).
|
||||
In("db.tag_id", condition.TagIds).
|
||||
RLike("db.tag_path", condition.TagPath).
|
||||
Eq0("db."+model.DeletedColumn, model.ModelUndeleted).
|
||||
Eq0("inst."+model.DeletedColumn, model.ModelUndeleted).
|
||||
OrderByAsc("db.tag_path")
|
||||
|
||||
return gormx.PageQuery(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
@@ -30,6 +35,9 @@ func (d *dbRepoImpl) Count(condition *entity.DbQuery) int64 {
|
||||
if len(condition.TagIds) > 0 {
|
||||
where["tag_id"] = condition.TagIds
|
||||
}
|
||||
if condition.InstanceId > 0 {
|
||||
where["instance_id"] = condition.InstanceId
|
||||
}
|
||||
return gormx.CountByCond(new(entity.Db), where)
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,6 @@ func (d *instanceRepoImpl) GetById(id uint64, cols ...string) *entity.Instance {
|
||||
instance := new(entity.Instance)
|
||||
if err := gormx.GetById(instance, id, cols...); err != nil {
|
||||
return nil
|
||||
|
||||
}
|
||||
return instance
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ func InitDbRouter(router *gin.RouterGroup) {
|
||||
db := router.Group("dbs")
|
||||
|
||||
d := &api.Db{
|
||||
InstanceApp: application.GetInstanceApp(),
|
||||
DbApp: application.GetDbApp(),
|
||||
DbSqlExecApp: application.GetDbSqlExecApp(),
|
||||
MsgApp: msgapp.GetMsgApp(),
|
||||
@@ -31,8 +32,6 @@ func InitDbRouter(router *gin.RouterGroup) {
|
||||
// 获取数据库实例的所有数据库名
|
||||
req.NewPost("/databases", d.GetDatabaseNames),
|
||||
|
||||
req.NewGet(":dbId/pwd", d.GetDbPwd),
|
||||
|
||||
req.NewDelete(":dbId", d.DeleteDb).Log(req.NewLogSave("db-删除数据库信息")),
|
||||
|
||||
req.NewGet(":dbId/t-infos", d.TableInfos),
|
||||
|
||||
@@ -14,6 +14,7 @@ func InitInstanceRouter(router *gin.RouterGroup) {
|
||||
|
||||
d := &api.Instance{
|
||||
InstanceApp: application.GetInstanceApp(),
|
||||
DbApp: application.GetDbApp(),
|
||||
MsgApp: msgapp.GetMsgApp(),
|
||||
}
|
||||
|
||||
@@ -23,9 +24,12 @@ func InitInstanceRouter(router *gin.RouterGroup) {
|
||||
|
||||
req.NewPost("", d.SaveInstance).Log(req.NewLogSave("db-保存数据库实例信息")),
|
||||
|
||||
req.NewGet(":dbId/pwd", d.GetInstancePwd),
|
||||
req.NewGet(":instanceId", d.GetInstance),
|
||||
req.NewGet(":instanceId/pwd", d.GetInstancePwd),
|
||||
// 获取数据库实例的所有数据库名
|
||||
req.NewGet(":instanceId/databases", d.GetDatabaseNames),
|
||||
|
||||
req.NewDelete(":dbId", d.DeleteInstance).Log(req.NewLogSave("db-删除数据库实例")),
|
||||
req.NewDelete(":instanceId", d.DeleteInstance).Log(req.NewLogSave("db-删除数据库实例")),
|
||||
}
|
||||
|
||||
req.BatchSetGroup(instances, reqs[:])
|
||||
|
||||
@@ -1,36 +1,61 @@
|
||||
SET NAMES utf8mb4;
|
||||
SET FOREIGN_KEY_CHECKS = 0;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for t_instance
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `t_instance`;
|
||||
CREATE TABLE `t_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,
|
||||
`port` int(8) NOT NULL,
|
||||
`username` varchar(255) COLLATE utf8mb4_bin NOT NULL,
|
||||
`password` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
|
||||
`type` varchar(20) COLLATE utf8mb4_bin NOT NULL COMMENT '数据库实例类型(mysql...)',
|
||||
`params` varchar(125) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '其他连接参数',
|
||||
`network` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
|
||||
`ssh_tunnel_machine_id` bigint(20) DEFAULT NULL COMMENT 'ssh隧道的机器id',
|
||||
`remark` varchar(125) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '备注,描述等',
|
||||
`create_time` datetime DEFAULT NULL,
|
||||
`creator_id` bigint(20) DEFAULT NULL,
|
||||
`creator` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
|
||||
`update_time` datetime DEFAULT NULL,
|
||||
`modifier_id` bigint(20) DEFAULT NULL,
|
||||
`modifier` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
|
||||
`is_deleted` tinyint(8) NOT NULL DEFAULT '0',
|
||||
`delete_time` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='数据库实例信息表';
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of t_instance
|
||||
-- ----------------------------
|
||||
BEGIN;
|
||||
COMMIT;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for t_db
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `t_db`;
|
||||
CREATE TABLE `t_db` (
|
||||
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(32) DEFAULT NULL COMMENT '数据库实例名称',
|
||||
`host` varchar(100) NOT NULL,
|
||||
`port` int(8) NOT NULL,
|
||||
`username` varchar(255) NOT NULL,
|
||||
`password` varchar(255) DEFAULT NULL,
|
||||
`type` varchar(20) NOT NULL COMMENT '数据库实例类型(mysql...)',
|
||||
`database` varchar(1000) DEFAULT NULL COMMENT '数据库,空格分割多个数据库',
|
||||
`params` varchar(125) DEFAULT NULL COMMENT '其他连接参数',
|
||||
`network` varchar(20) DEFAULT NULL,
|
||||
`ssh_tunnel_machine_id` bigint(20) DEFAULT NULL COMMENT 'ssh隧道的机器id',
|
||||
`remark` varchar(125) DEFAULT NULL COMMENT '备注,描述等',
|
||||
`tag_id` bigint(20) DEFAULT NULL COMMENT '标签id',
|
||||
`tag_path` varchar(255) DEFAULT NULL COMMENT '标签路径',
|
||||
`create_time` datetime DEFAULT NULL,
|
||||
`creator_id` bigint(20) DEFAULT NULL,
|
||||
`creator` varchar(32) DEFAULT NULL,
|
||||
`update_time` datetime DEFAULT NULL,
|
||||
`modifier_id` bigint(20) DEFAULT NULL,
|
||||
`modifier` varchar(32) DEFAULT NULL,
|
||||
`is_deleted` tinyint(8) NOT NULL DEFAULT 0,
|
||||
`delete_time` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_path` (`tag_path`) USING BTREE
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='数据库资源信息表';
|
||||
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '数据库实例名称',
|
||||
`database` varchar(1000) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '数据库,空格分割多个数据库',
|
||||
`remark` varchar(125) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '备注,描述等',
|
||||
`tag_id` bigint(20) DEFAULT NULL COMMENT '标签id',
|
||||
`tag_path` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '标签路径',
|
||||
`instance_id` bigint(20) NOT NULL COMMENT '数据库实例 ID',
|
||||
`create_time` datetime DEFAULT NULL,
|
||||
`creator_id` bigint(20) DEFAULT NULL,
|
||||
`creator` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
|
||||
`update_time` datetime DEFAULT NULL,
|
||||
`modifier_id` bigint(20) DEFAULT NULL,
|
||||
`modifier` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
|
||||
`is_deleted` tinyint(8) NOT NULL DEFAULT '0',
|
||||
`delete_time` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='数据库资源信息表';
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of t_db
|
||||
@@ -609,6 +634,10 @@ INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight
|
||||
INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES(130, 2, '12sSjal1/W9XKiabq/', 1, 1, '计划任务', '/machine/cron-job', 1689646396, '{"component":"ops/machine/cronjob/CronJobList","icon":"AlarmClock","isKeepAlive":true,"routeName":"CronJobList"}', 1, 'admin', 1, 'admin', '2023-07-18 10:13:16', '2023-07-18 10:14:06', 0, NULL);
|
||||
INSERT INTO t_sys_resource (id, pid, ui_path, type, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES(134, 80, 'Mongo452/eggago31/3sblw1Wb/', 2, 1, '删除数据', 'mongo:data:del', 1692674964, 'null', 1, 'admin', 1, 'admin', '2023-08-22 11:29:24', '2023-08-22 11:29:24', 0, NULL);
|
||||
INSERT INTO t_sys_resource (id, pid, ui_path, type, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES(133, 80, 'Mongo452/eggago31/xvpKk36u/', 2, 1, '保存数据', 'mongo:data:save', 1692674943, 'null', 1, 'admin', 1, 'admin', '2023-08-22 11:29:04', '2023-08-22 11:29:11', 0, NULL);
|
||||
INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES (135, 36, 'dbms23ax/X0f4BxT0/', 1, 1, '数据库实例管理', 'instances', 1693040706, '{\"component\":\"ops/db/InstanceList\",\"icon\":\"Coin\",\"isKeepAlive\":true,\"routeName\":\"InstanceList\"}', 1, 'admin', 1, 'admin', '2023-08-26 09:05:07', '2023-08-29 22:35:11', 0, NULL);
|
||||
INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES (136, 133, 'dbms23ax/X0f4BxT0/D23fUiBr/', 2, 1, '实例保存', 'instance:save', 1693041001, 'null', 1, 'admin', 1, 'admin', '2023-08-26 09:10:02', '2023-08-26 09:10:02', 0, NULL);
|
||||
INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES (137, 133, 'dbms23ax/X0f4BxT0/mJlBeTCs/', 2, 1, '基本权限', 'instance', 1693041055, 'null', 1, 'admin', 1, 'admin', '2023-08-26 09:10:55', '2023-08-26 09:10:55', 0, NULL);
|
||||
INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES (138, 133, 'dbms23ax/X0f4BxT0/Sgg8uPwz/', 2, 1, '实例删除', 'instance:del', 1693041084, 'null', 1, 'admin', 1, 'admin', '2023-08-26 09:11:24', '2023-08-26 09:11:24', 0, NULL);
|
||||
COMMIT;
|
||||
|
||||
-- ----------------------------
|
||||
@@ -830,7 +859,13 @@ INSERT INTO `t_sys_role_resource` (role_id,resource_id,creator_id,creator,create
|
||||
(1,128,1,'admin','2023-03-16 16:11:25', 0, NULL),
|
||||
(1,130,1,'admin','2023-03-16 16:11:25', 0, NULL),
|
||||
(1,131,1,'admin','2023-03-16 16:11:25', 0, NULL),
|
||||
(1,132,1,'admin','2023-03-16 16:11:25', 0, NULL);
|
||||
(1,132,1,'admin','2023-03-16 16:11:25', 0, NULL),
|
||||
(1,133,1,'admin','2023-08-30 20:17:00', 0, NULL),
|
||||
(1,134,1,'admin','2023-08-30 20:17:00', 0, NULL),
|
||||
(1,135,1,'admin','2023-08-30 20:17:00', 0, NULL),
|
||||
(1,136,1,'admin','2023-08-30 20:17:00', 0, NULL),
|
||||
(1,137,1,'admin','2023-08-30 20:17:00', 0, NULL),
|
||||
(1,138,1,'admin','2023-08-30 20:17:00', 0, NULL);
|
||||
COMMIT;
|
||||
|
||||
-- ----------------------------
|
||||
|
||||
Reference in New Issue
Block a user