2021-01-08 15:37:32 +08:00
|
|
|
|
package controllers
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
2021-03-24 17:18:39 +08:00
|
|
|
|
"mayfly-go/base/biz"
|
2021-01-08 15:37:32 +08:00
|
|
|
|
"mayfly-go/base/ctx"
|
2021-04-16 15:10:07 +08:00
|
|
|
|
"mayfly-go/base/ginx"
|
2021-01-08 15:37:32 +08:00
|
|
|
|
"mayfly-go/base/model"
|
2021-03-24 17:18:39 +08:00
|
|
|
|
"mayfly-go/devops/controllers/form"
|
|
|
|
|
|
"mayfly-go/devops/controllers/vo"
|
|
|
|
|
|
"mayfly-go/devops/db"
|
|
|
|
|
|
"mayfly-go/devops/models"
|
2021-01-08 15:37:32 +08:00
|
|
|
|
"strconv"
|
2021-04-21 10:22:09 +08:00
|
|
|
|
"strings"
|
2021-01-08 15:37:32 +08:00
|
|
|
|
|
2021-04-16 15:10:07 +08:00
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
)
|
2021-01-08 15:37:32 +08:00
|
|
|
|
|
|
|
|
|
|
// @router /api/dbs [get]
|
2021-04-16 15:10:07 +08:00
|
|
|
|
func Dbs(rc *ctx.ReqCtx) {
|
|
|
|
|
|
m := new([]models.Db)
|
|
|
|
|
|
rc.ResData = model.GetPage(ginx.GetPageParam(rc.GinCtx), m, new([]vo.SelectDataDbVO))
|
2021-01-08 15:37:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// @router /api/db/:dbId/select [get]
|
2021-04-16 15:10:07 +08:00
|
|
|
|
func SelectData(rc *ctx.ReqCtx) {
|
|
|
|
|
|
g := rc.GinCtx
|
2021-04-21 10:22:09 +08:00
|
|
|
|
// 去除前后空格及换行符
|
|
|
|
|
|
selectSql := strings.TrimFunc(g.Query("selectSql"), func(r rune) bool {
|
|
|
|
|
|
s := string(r)
|
|
|
|
|
|
return s == " " || s == "\n"
|
|
|
|
|
|
})
|
2021-04-16 15:10:07 +08:00
|
|
|
|
rc.ReqParam = selectSql
|
2021-04-21 10:22:09 +08:00
|
|
|
|
|
2021-04-16 15:10:07 +08:00
|
|
|
|
biz.NotEmpty(selectSql, "selectSql不能为空")
|
|
|
|
|
|
res, err := db.GetDbInstance(GetDbId(g)).SelectData(selectSql)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
panic(biz.NewBizErr(fmt.Sprintf("查询失败: %s", err.Error())))
|
|
|
|
|
|
}
|
|
|
|
|
|
rc.ResData = res
|
2021-01-08 15:37:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// @router /api/db/:dbId/exec-sql [post]
|
2021-04-16 15:10:07 +08:00
|
|
|
|
func ExecSql(g *gin.Context) {
|
|
|
|
|
|
rc := ctx.NewReqCtxWithGin(g).WithLog(ctx.NewLogInfo("sql执行"))
|
|
|
|
|
|
rc.Handle(func(rc *ctx.ReqCtx) {
|
|
|
|
|
|
selectSql := g.Query("sql")
|
2021-03-24 17:18:39 +08:00
|
|
|
|
biz.NotEmpty(selectSql, "sql不能为空")
|
2021-04-16 15:10:07 +08:00
|
|
|
|
num, err := db.GetDbInstance(GetDbId(g)).Exec(selectSql)
|
2021-01-08 15:37:32 +08:00
|
|
|
|
if err != nil {
|
2021-03-24 17:18:39 +08:00
|
|
|
|
panic(biz.NewBizErr(fmt.Sprintf("执行失败: %s", err.Error())))
|
2021-01-08 15:37:32 +08:00
|
|
|
|
}
|
2021-04-16 15:10:07 +08:00
|
|
|
|
rc.ResData = num
|
2021-01-08 15:37:32 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// @router /api/db/:dbId/t-metadata [get]
|
2021-04-16 15:10:07 +08:00
|
|
|
|
func TableMA(rc *ctx.ReqCtx) {
|
|
|
|
|
|
rc.ResData = db.GetDbInstance(GetDbId(rc.GinCtx)).GetTableMetedatas()
|
2021-01-08 15:37:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// @router /api/db/:dbId/c-metadata [get]
|
2021-04-16 15:10:07 +08:00
|
|
|
|
func ColumnMA(rc *ctx.ReqCtx) {
|
|
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
tn := g.Query("tableName")
|
|
|
|
|
|
biz.NotEmpty(tn, "tableName不能为空")
|
|
|
|
|
|
rc.ResData = db.GetDbInstance(GetDbId(g)).GetColumnMetadatas(tn)
|
2021-01-08 15:37:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// @router /api/db/:dbId/hint-tables [get]
|
2021-04-16 15:10:07 +08:00
|
|
|
|
func HintTables(rc *ctx.ReqCtx) {
|
|
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
dbi := db.GetDbInstance(GetDbId(g))
|
|
|
|
|
|
tables := dbi.GetTableMetedatas()
|
|
|
|
|
|
res := make(map[string][]string)
|
|
|
|
|
|
for _, v := range tables {
|
|
|
|
|
|
tableName := v["tableName"]
|
|
|
|
|
|
columnMds := dbi.GetColumnMetadatas(tableName)
|
|
|
|
|
|
columnNames := make([]string, len(columnMds))
|
|
|
|
|
|
for i, v := range columnMds {
|
|
|
|
|
|
comment := v["columnComment"]
|
|
|
|
|
|
if comment != "" {
|
|
|
|
|
|
columnNames[i] = v["columnName"] + " [" + comment + "]"
|
|
|
|
|
|
} else {
|
|
|
|
|
|
columnNames[i] = v["columnName"]
|
2021-01-08 15:37:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-04-16 15:10:07 +08:00
|
|
|
|
res[tableName] = columnNames
|
|
|
|
|
|
}
|
|
|
|
|
|
rc.ResData = res
|
2021-01-08 15:37:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// @router /api/db/:dbId/sql [post]
|
2021-04-16 15:10:07 +08:00
|
|
|
|
func SaveSql(rc *ctx.ReqCtx) {
|
|
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
account := rc.LoginAccount
|
|
|
|
|
|
dbSqlForm := &form.DbSqlSaveForm{}
|
|
|
|
|
|
ginx.BindJsonAndValid(g, dbSqlForm)
|
|
|
|
|
|
rc.ReqParam = dbSqlForm
|
2021-01-08 15:37:32 +08:00
|
|
|
|
|
2021-04-16 15:10:07 +08:00
|
|
|
|
dbId := GetDbId(g)
|
|
|
|
|
|
// 判断dbId是否存在
|
|
|
|
|
|
err := model.GetById(new(models.Db), dbId)
|
|
|
|
|
|
biz.BizErrIsNil(err, "该数据库信息不存在")
|
2021-01-08 15:37:32 +08:00
|
|
|
|
|
2021-04-16 15:10:07 +08:00
|
|
|
|
// 获取用于是否有该dbsql的保存记录,有则更改,否则新增
|
|
|
|
|
|
dbSql := &models.DbSql{Type: dbSqlForm.Type, DbId: dbId}
|
|
|
|
|
|
dbSql.CreatorId = account.Id
|
|
|
|
|
|
e := model.GetBy(dbSql)
|
2021-01-08 15:37:32 +08:00
|
|
|
|
|
2021-04-16 15:10:07 +08:00
|
|
|
|
dbSql.SetBaseInfo(account)
|
|
|
|
|
|
// 更新sql信息
|
|
|
|
|
|
dbSql.Sql = dbSqlForm.Sql
|
|
|
|
|
|
if e == nil {
|
|
|
|
|
|
model.UpdateById(dbSql)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
model.Insert(dbSql)
|
|
|
|
|
|
}
|
2021-01-08 15:37:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// @router /api/db/:dbId/sql [get]
|
2021-04-16 15:10:07 +08:00
|
|
|
|
func GetSql(rc *ctx.ReqCtx) {
|
|
|
|
|
|
// 获取用于是否有该dbsql的保存记录,有则更改,否则新增
|
|
|
|
|
|
dbSql := &models.DbSql{Type: 1, DbId: GetDbId(rc.GinCtx)}
|
|
|
|
|
|
dbSql.CreatorId = rc.LoginAccount.Id
|
|
|
|
|
|
e := model.GetBy(dbSql)
|
|
|
|
|
|
if e != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
rc.ResData = dbSql
|
2021-01-08 15:37:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-16 15:10:07 +08:00
|
|
|
|
func GetDbId(g *gin.Context) uint64 {
|
|
|
|
|
|
dbId, _ := strconv.Atoi(g.Param("dbId"))
|
2021-03-24 17:18:39 +08:00
|
|
|
|
biz.IsTrue(dbId > 0, "dbId错误")
|
2021-01-08 15:37:32 +08:00
|
|
|
|
return uint64(dbId)
|
|
|
|
|
|
}
|