2021-09-11 14:04:09 +08:00
|
|
|
|
package api
|
2021-01-08 15:37:32 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
2022-06-30 16:42:25 +08:00
|
|
|
|
"io"
|
2022-09-09 18:26:08 +08:00
|
|
|
|
"mayfly-go/internal/db/api/form"
|
|
|
|
|
|
"mayfly-go/internal/db/api/vo"
|
|
|
|
|
|
"mayfly-go/internal/db/application"
|
|
|
|
|
|
"mayfly-go/internal/db/domain/entity"
|
2023-07-03 21:42:04 +08:00
|
|
|
|
msgapp "mayfly-go/internal/msg/application"
|
2022-10-26 20:49:29 +08:00
|
|
|
|
tagapp "mayfly-go/internal/tag/application"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/biz"
|
|
|
|
|
|
"mayfly-go/pkg/ginx"
|
2023-07-01 14:34:42 +08:00
|
|
|
|
"mayfly-go/pkg/gormx"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/model"
|
2023-01-14 16:29:52 +08:00
|
|
|
|
"mayfly-go/pkg/req"
|
2023-07-21 17:07:04 +08:00
|
|
|
|
"mayfly-go/pkg/utils/cryptox"
|
|
|
|
|
|
"mayfly-go/pkg/utils/stringx"
|
|
|
|
|
|
"mayfly-go/pkg/utils/structx"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/ws"
|
2021-01-08 15:37:32 +08:00
|
|
|
|
"strconv"
|
2021-04-21 10:22:09 +08:00
|
|
|
|
"strings"
|
2022-06-30 16:42:25 +08:00
|
|
|
|
"time"
|
2021-01-08 15:37:32 +08:00
|
|
|
|
|
2021-04-16 15:10:07 +08:00
|
|
|
|
"github.com/gin-gonic/gin"
|
2022-06-30 16:42:25 +08:00
|
|
|
|
"github.com/xwb1989/sqlparser"
|
2021-04-16 15:10:07 +08:00
|
|
|
|
)
|
2021-01-08 15:37:32 +08:00
|
|
|
|
|
2021-05-08 18:00:33 +08:00
|
|
|
|
type Db struct {
|
2022-06-16 15:55:18 +08:00
|
|
|
|
DbApp application.Db
|
|
|
|
|
|
DbSqlExecApp application.DbSqlExec
|
2023-07-03 21:42:04 +08:00
|
|
|
|
MsgApp msgapp.Msg
|
2022-10-26 20:49:29 +08:00
|
|
|
|
TagApp tagapp.TagTree
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-22 15:54:53 +08:00
|
|
|
|
const DEFAULT_ROW_SIZE = 5000
|
2022-06-30 16:42:25 +08:00
|
|
|
|
|
2021-01-08 15:37:32 +08:00
|
|
|
|
// @router /api/dbs [get]
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (d *Db) Dbs(rc *req.Ctx) {
|
2023-07-08 20:05:55 +08:00
|
|
|
|
queryCond, page := ginx.BindQueryAndPage[*entity.DbQuery](rc.GinCtx, new(entity.DbQuery))
|
2022-10-26 20:49:29 +08:00
|
|
|
|
|
|
|
|
|
|
// 不存在可访问标签id,即没有可操作数据
|
|
|
|
|
|
tagIds := d.TagApp.ListTagIdByAccountId(rc.LoginAccount.Id)
|
|
|
|
|
|
if len(tagIds) == 0 {
|
2023-07-01 14:34:42 +08:00
|
|
|
|
rc.ResData = model.EmptyPageResult[any]()
|
2022-10-26 20:49:29 +08:00
|
|
|
|
return
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
2023-07-08 20:05:55 +08:00
|
|
|
|
|
|
|
|
|
|
queryCond.TagIds = tagIds
|
|
|
|
|
|
rc.ResData = d.DbApp.GetPageList(queryCond, page, new([]vo.SelectDataDbVO))
|
2021-01-08 15:37:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-20 22:41:13 +08:00
|
|
|
|
func (d *Db) DbTags(rc *req.Ctx) {
|
|
|
|
|
|
rc.ResData = d.TagApp.ListTagByAccountIdAndResource(rc.LoginAccount.Id, new(entity.Db))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (d *Db) Save(rc *req.Ctx) {
|
2021-07-28 18:03:19 +08:00
|
|
|
|
form := &form.DbForm{}
|
2023-07-08 20:05:55 +08:00
|
|
|
|
db := ginx.BindJsonAndCopyTo[*entity.Db](rc.GinCtx, form, new(entity.Db))
|
2022-07-18 20:36:31 +08:00
|
|
|
|
|
|
|
|
|
|
// 密码解密,并使用解密后的赋值
|
2023-07-21 17:07:04 +08:00
|
|
|
|
originPwd, err := cryptox.DefaultRsaDecrypt(form.Password, true)
|
2022-07-18 20:36:31 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
|
|
|
|
|
|
db.Password = originPwd
|
|
|
|
|
|
|
2022-07-14 11:39:12 +08:00
|
|
|
|
// 密码脱敏记录日志
|
|
|
|
|
|
form.Password = "****"
|
|
|
|
|
|
rc.ReqParam = form
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
db.SetBaseInfo(rc.LoginAccount)
|
|
|
|
|
|
d.DbApp.Save(db)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-02 21:44:01 +08:00
|
|
|
|
// 获取数据库实例密码,由于数据库是加密存储,故提供该接口展示原文密码
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (d *Db) GetDbPwd(rc *req.Ctx) {
|
2022-08-02 21:44:01 +08:00
|
|
|
|
dbId := GetDbId(rc.GinCtx)
|
|
|
|
|
|
dbEntity := d.DbApp.GetById(dbId, "Password")
|
|
|
|
|
|
dbEntity.PwdDecrypt()
|
|
|
|
|
|
rc.ResData = dbEntity.Password
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-07-23 16:41:04 +08:00
|
|
|
|
// 获取数据库实例的所有数据库名
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (d *Db) GetDatabaseNames(rc *req.Ctx) {
|
2022-07-23 16:41:04 +08:00
|
|
|
|
form := &form.DbForm{}
|
|
|
|
|
|
ginx.BindJsonAndValid(rc.GinCtx, form)
|
|
|
|
|
|
|
|
|
|
|
|
db := new(entity.Db)
|
2023-07-21 17:07:04 +08:00
|
|
|
|
structx.Copy(db, form)
|
2022-07-23 16:41:04 +08:00
|
|
|
|
|
|
|
|
|
|
// 密码解密,并使用解密后的赋值
|
2023-07-21 17:07:04 +08:00
|
|
|
|
originPwd, err := cryptox.DefaultRsaDecrypt(form.Password, true)
|
2022-07-23 16:41:04 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
|
|
|
|
|
|
db.Password = originPwd
|
|
|
|
|
|
|
|
|
|
|
|
// 如果id不为空,并且密码为空则从数据库查询
|
|
|
|
|
|
if form.Id != 0 && db.Password == "" {
|
|
|
|
|
|
db = d.DbApp.GetById(form.Id)
|
2023-07-16 11:13:52 +08:00
|
|
|
|
db.PwdDecrypt()
|
2022-07-23 16:41:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
rc.ResData = d.DbApp.GetDatabases(db)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (d *Db) DeleteDb(rc *req.Ctx) {
|
2023-07-01 14:34:42 +08:00
|
|
|
|
idsStr := ginx.PathParam(rc.GinCtx, "dbId")
|
|
|
|
|
|
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.DbApp.Delete(dbId)
|
|
|
|
|
|
// 删除该库的sql执行记录
|
|
|
|
|
|
d.DbSqlExecApp.DeleteBy(&entity.DbSqlExec{DbId: dbId})
|
|
|
|
|
|
}
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (d *Db) TableInfos(rc *req.Ctx) {
|
2022-08-10 19:46:17 +08:00
|
|
|
|
rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta().GetTableInfos()
|
2021-08-18 17:57:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (d *Db) TableIndex(rc *req.Ctx) {
|
2021-08-18 17:57:33 +08:00
|
|
|
|
tn := rc.GinCtx.Query("tableName")
|
|
|
|
|
|
biz.NotEmpty(tn, "tableName不能为空")
|
2022-08-10 19:46:17 +08:00
|
|
|
|
rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta().GetTableIndex(tn)
|
2021-08-18 17:57:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (d *Db) GetCreateTableDdl(rc *req.Ctx) {
|
2021-08-18 17:57:33 +08:00
|
|
|
|
tn := rc.GinCtx.Query("tableName")
|
|
|
|
|
|
biz.NotEmpty(tn, "tableName不能为空")
|
2022-08-10 19:46:17 +08:00
|
|
|
|
rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta().GetCreateTableDdl(tn)
|
2021-08-18 17:57:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (d *Db) ExecSql(rc *req.Ctx) {
|
2021-04-16 15:10:07 +08:00
|
|
|
|
g := rc.GinCtx
|
2022-06-16 15:55:18 +08:00
|
|
|
|
form := &form.DbSqlExecForm{}
|
|
|
|
|
|
ginx.BindJsonAndValid(g, form)
|
2022-04-22 17:49:21 +08:00
|
|
|
|
|
2022-06-16 15:55:18 +08:00
|
|
|
|
id := GetDbId(g)
|
|
|
|
|
|
db := form.Db
|
2022-05-08 14:10:57 +08:00
|
|
|
|
dbInstance := d.DbApp.GetDbInstance(id, db)
|
2022-11-18 17:52:30 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(d.TagApp.CanAccess(rc.LoginAccount.Id, dbInstance.Info.TagPath), "%s")
|
2022-04-22 17:49:21 +08:00
|
|
|
|
|
2022-12-17 22:24:21 +08:00
|
|
|
|
rc.ReqParam = fmt.Sprintf("%s\n-> %s", dbInstance.Info.GetLogDesc(), form.Sql)
|
2022-11-02 19:27:40 +08:00
|
|
|
|
biz.NotEmpty(form.Sql, "sql不能为空")
|
2022-06-16 15:55:18 +08:00
|
|
|
|
|
2022-11-02 19:27:40 +08:00
|
|
|
|
// 去除前后空格及换行符
|
2023-07-21 17:07:04 +08:00
|
|
|
|
sql := stringx.TrimSpaceAndBr(form.Sql)
|
2022-09-22 11:56:21 +08:00
|
|
|
|
|
2022-11-02 19:27:40 +08:00
|
|
|
|
execReq := &application.DbSqlExecReq{
|
2022-09-22 11:56:21 +08:00
|
|
|
|
DbId: id,
|
|
|
|
|
|
Db: db,
|
|
|
|
|
|
Remark: form.Remark,
|
|
|
|
|
|
DbInstance: dbInstance,
|
|
|
|
|
|
LoginAccount: rc.LoginAccount,
|
2022-11-02 19:27:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-17 08:59:37 +08:00
|
|
|
|
sqls, err := sqlparser.SplitStatementToPieces(sql)
|
|
|
|
|
|
biz.ErrIsNil(err, "SQL解析错误,请检查您的执行SQL")
|
2022-11-02 19:27:40 +08:00
|
|
|
|
isMulti := len(sqls) > 1
|
|
|
|
|
|
var execResAll *application.DbSqlExecRes
|
|
|
|
|
|
for _, s := range sqls {
|
2023-07-21 17:07:04 +08:00
|
|
|
|
s = stringx.TrimSpaceAndBr(s)
|
2022-11-02 19:27:40 +08:00
|
|
|
|
// 多条执行,如果有查询语句,则跳过
|
|
|
|
|
|
if isMulti && strings.HasPrefix(strings.ToLower(s), "select") {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
execReq.Sql = s
|
|
|
|
|
|
execRes, err := d.DbSqlExecApp.Exec(execReq)
|
2022-12-17 22:24:21 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, fmt.Sprintf("[%s] -> 执行失败: ", s)+"%s")
|
|
|
|
|
|
}
|
2022-11-02 19:27:40 +08:00
|
|
|
|
|
|
|
|
|
|
if execResAll == nil {
|
|
|
|
|
|
execResAll = execRes
|
|
|
|
|
|
} else {
|
|
|
|
|
|
execResAll.Merge(execRes)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-01 12:31:32 +08:00
|
|
|
|
colAndRes := make(map[string]any)
|
2022-11-02 19:27:40 +08:00
|
|
|
|
colAndRes["colNames"] = execResAll.ColNames
|
|
|
|
|
|
colAndRes["res"] = execResAll.Res
|
2022-09-22 11:56:21 +08:00
|
|
|
|
rc.ResData = colAndRes
|
2021-01-08 15:37:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-25 14:34:15 +08:00
|
|
|
|
// 执行sql文件
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (d *Db) ExecSqlFile(rc *req.Ctx) {
|
2021-11-25 14:34:15 +08:00
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
fileheader, err := g.FormFile("file")
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "读取sql文件失败: %s")
|
|
|
|
|
|
|
|
|
|
|
|
file, _ := fileheader.Open()
|
|
|
|
|
|
filename := fileheader.Filename
|
2022-05-08 14:10:57 +08:00
|
|
|
|
dbId, db := GetIdAndDb(g)
|
2021-11-25 14:34:15 +08:00
|
|
|
|
|
2022-12-17 22:24:21 +08:00
|
|
|
|
dbInstance := d.DbApp.GetDbInstance(dbId, db)
|
|
|
|
|
|
biz.ErrIsNilAppendErr(d.TagApp.CanAccess(rc.LoginAccount.Id, dbInstance.Info.TagPath), "%s")
|
|
|
|
|
|
rc.ReqParam = fmt.Sprintf("%s -> filename: %s", dbInstance.Info.GetLogDesc(), filename)
|
2022-01-12 16:00:31 +08:00
|
|
|
|
|
2022-12-17 22:24:21 +08:00
|
|
|
|
logExecRecord := true
|
|
|
|
|
|
// 如果执行sql文件大于该值则不记录sql执行记录
|
2023-02-22 15:54:53 +08:00
|
|
|
|
if fileheader.Size > 50*1024 {
|
2022-12-17 22:24:21 +08:00
|
|
|
|
logExecRecord = false
|
|
|
|
|
|
}
|
2022-01-12 16:00:31 +08:00
|
|
|
|
|
2022-12-17 22:24:21 +08:00
|
|
|
|
go func() {
|
2021-12-02 10:35:48 +08:00
|
|
|
|
defer func() {
|
|
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
|
|
switch t := err.(type) {
|
|
|
|
|
|
case *biz.BizError:
|
2022-12-17 22:24:21 +08:00
|
|
|
|
d.MsgApp.CreateAndSend(rc.LoginAccount, ws.ErrMsg("sql脚本执行失败", fmt.Sprintf("[%s]%s执行失败: [%s]", filename, dbInstance.Info.GetLogDesc(), t.Error())))
|
2021-12-02 10:35:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
2022-12-17 22:24:21 +08:00
|
|
|
|
execReq := &application.DbSqlExecReq{
|
|
|
|
|
|
DbId: dbId,
|
|
|
|
|
|
Db: db,
|
|
|
|
|
|
Remark: fileheader.Filename,
|
|
|
|
|
|
DbInstance: dbInstance,
|
|
|
|
|
|
LoginAccount: rc.LoginAccount,
|
|
|
|
|
|
}
|
2022-04-22 17:49:21 +08:00
|
|
|
|
|
2022-06-30 16:42:25 +08:00
|
|
|
|
tokens := sqlparser.NewTokenizer(file)
|
|
|
|
|
|
for {
|
|
|
|
|
|
stmt, err := sqlparser.ParseNext(tokens)
|
|
|
|
|
|
if err == io.EOF {
|
|
|
|
|
|
break
|
2021-11-25 14:34:15 +08:00
|
|
|
|
}
|
2022-06-30 16:42:25 +08:00
|
|
|
|
sql := sqlparser.String(stmt)
|
2022-12-17 22:24:21 +08:00
|
|
|
|
execReq.Sql = sql
|
|
|
|
|
|
// 需要记录执行记录
|
|
|
|
|
|
if logExecRecord {
|
|
|
|
|
|
_, err = d.DbSqlExecApp.Exec(execReq)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
_, err = dbInstance.Exec(sql)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-25 14:34:15 +08:00
|
|
|
|
if err != nil {
|
2022-12-17 22:24:21 +08:00
|
|
|
|
d.MsgApp.CreateAndSend(rc.LoginAccount, ws.ErrMsg("sql脚本执行失败", fmt.Sprintf("[%s][%s] -> sql=[%s] 执行失败: [%s]", filename, dbInstance.Info.GetLogDesc(), sql, err.Error())))
|
2021-11-25 14:34:15 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-12-17 22:24:21 +08:00
|
|
|
|
d.MsgApp.CreateAndSend(rc.LoginAccount, ws.SuccessMsg("sql脚本执行成功", fmt.Sprintf("[%s]执行完成 -> %s", filename, dbInstance.Info.GetLogDesc())))
|
2021-11-25 14:34:15 +08:00
|
|
|
|
}()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-06-30 16:42:25 +08:00
|
|
|
|
// 数据库dump
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (d *Db) DumpSql(rc *req.Ctx) {
|
2022-06-30 16:42:25 +08:00
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
dbId, db := GetIdAndDb(g)
|
|
|
|
|
|
dumpType := g.Query("type")
|
|
|
|
|
|
tablesStr := g.Query("tables")
|
|
|
|
|
|
biz.NotEmpty(tablesStr, "请选择要导出的表")
|
|
|
|
|
|
tables := strings.Split(tablesStr, ",")
|
|
|
|
|
|
|
|
|
|
|
|
// 是否需要导出表结构
|
|
|
|
|
|
needStruct := dumpType == "1" || dumpType == "3"
|
|
|
|
|
|
// 是否需要导出数据
|
|
|
|
|
|
needData := dumpType == "2" || dumpType == "3"
|
|
|
|
|
|
|
2022-07-10 12:14:06 +08:00
|
|
|
|
dbInstance := d.DbApp.GetDbInstance(dbId, db)
|
2022-11-18 17:52:30 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(d.TagApp.CanAccess(rc.LoginAccount.Id, dbInstance.Info.TagPath), "%s")
|
2022-07-10 12:14:06 +08:00
|
|
|
|
|
2022-06-30 16:42:25 +08:00
|
|
|
|
now := time.Now()
|
|
|
|
|
|
filename := fmt.Sprintf("%s.%s.sql", db, now.Format("200601021504"))
|
|
|
|
|
|
g.Header("Content-Type", "application/octet-stream")
|
|
|
|
|
|
g.Header("Content-Disposition", "attachment; filename="+filename)
|
|
|
|
|
|
|
|
|
|
|
|
writer := g.Writer
|
|
|
|
|
|
writer.WriteString("-- ----------------------------")
|
|
|
|
|
|
writer.WriteString("\n-- 导出平台: mayfly-go")
|
|
|
|
|
|
writer.WriteString(fmt.Sprintf("\n-- 导出时间: %s ", now.Format("2006-01-02 15:04:05")))
|
|
|
|
|
|
writer.WriteString(fmt.Sprintf("\n-- 导出数据库: %s ", db))
|
|
|
|
|
|
writer.WriteString("\n-- ----------------------------\n")
|
|
|
|
|
|
|
2022-08-10 19:46:17 +08:00
|
|
|
|
dbmeta := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta()
|
2022-06-30 16:42:25 +08:00
|
|
|
|
for _, table := range tables {
|
|
|
|
|
|
if needStruct {
|
|
|
|
|
|
writer.WriteString(fmt.Sprintf("\n-- ----------------------------\n-- 表结构: %s \n-- ----------------------------\n", table))
|
|
|
|
|
|
writer.WriteString(fmt.Sprintf("DROP TABLE IF EXISTS `%s`;\n", table))
|
2023-05-24 12:32:17 +08:00
|
|
|
|
writer.WriteString(dbmeta.GetCreateTableDdl(table) + ";\n")
|
2022-06-30 16:42:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !needData {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
writer.WriteString(fmt.Sprintf("\n-- ----------------------------\n-- 表记录: %s \n-- ----------------------------\n", table))
|
|
|
|
|
|
writer.WriteString("BEGIN;\n")
|
|
|
|
|
|
|
2022-12-17 22:24:21 +08:00
|
|
|
|
pageNum := 1
|
|
|
|
|
|
for {
|
|
|
|
|
|
columns, result, _ := dbmeta.GetTableRecord(table, pageNum, DEFAULT_ROW_SIZE)
|
|
|
|
|
|
resultLen := len(result)
|
|
|
|
|
|
if resultLen == 0 {
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
2022-06-30 16:42:25 +08:00
|
|
|
|
insertSql := "INSERT INTO `%s` VALUES (%s);\n"
|
|
|
|
|
|
for _, res := range result {
|
|
|
|
|
|
var values []string
|
|
|
|
|
|
for _, column := range columns {
|
|
|
|
|
|
value := res[column]
|
|
|
|
|
|
if value == nil {
|
|
|
|
|
|
values = append(values, "NULL")
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
strValue, ok := value.(string)
|
|
|
|
|
|
if ok {
|
|
|
|
|
|
values = append(values, fmt.Sprintf("%#v", strValue))
|
|
|
|
|
|
} else {
|
2023-07-21 17:07:04 +08:00
|
|
|
|
values = append(values, stringx.AnyToStr(value))
|
2022-06-30 16:42:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
writer.WriteString(fmt.Sprintf(insertSql, table, strings.Join(values, ", ")))
|
|
|
|
|
|
}
|
2022-12-17 22:24:21 +08:00
|
|
|
|
if resultLen < DEFAULT_ROW_SIZE {
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
pageNum++
|
2022-06-30 16:42:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
writer.WriteString("COMMIT;\n")
|
|
|
|
|
|
}
|
2022-07-14 11:39:12 +08:00
|
|
|
|
|
2022-12-17 22:24:21 +08:00
|
|
|
|
rc.ReqParam = fmt.Sprintf("%s, tables: %s, dumpType: %s", dbInstance.Info.GetLogDesc(), tablesStr, dumpType)
|
2022-06-30 16:42:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-08 15:37:32 +08:00
|
|
|
|
// @router /api/db/:dbId/t-metadata [get]
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (d *Db) TableMA(rc *req.Ctx) {
|
2022-05-08 14:10:57 +08:00
|
|
|
|
dbi := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx))
|
2022-08-10 19:46:17 +08:00
|
|
|
|
rc.ResData = dbi.GetMeta().GetTables()
|
2021-01-08 15:37:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// @router /api/db/:dbId/c-metadata [get]
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (d *Db) ColumnMA(rc *req.Ctx) {
|
2021-04-16 15:10:07 +08:00
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
tn := g.Query("tableName")
|
|
|
|
|
|
biz.NotEmpty(tn, "tableName不能为空")
|
2022-04-22 17:49:21 +08:00
|
|
|
|
|
2022-05-08 14:10:57 +08:00
|
|
|
|
dbi := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx))
|
2022-08-10 19:46:17 +08:00
|
|
|
|
rc.ResData = dbi.GetMeta().GetColumns(tn)
|
2021-01-08 15:37:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// @router /api/db/:dbId/hint-tables [get]
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (d *Db) HintTables(rc *req.Ctx) {
|
2022-05-08 14:10:57 +08:00
|
|
|
|
dbi := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx))
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
2022-08-10 19:46:17 +08:00
|
|
|
|
dm := dbi.GetMeta()
|
|
|
|
|
|
// 获取所有表
|
|
|
|
|
|
tables := dm.GetTables()
|
2021-07-28 18:03:19 +08:00
|
|
|
|
tableNames := make([]string, 0)
|
2021-04-16 15:10:07 +08:00
|
|
|
|
for _, v := range tables {
|
2023-05-24 12:32:17 +08:00
|
|
|
|
tableNames = append(tableNames, v.TableName)
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
// key = 表名,value = 列名数组
|
|
|
|
|
|
res := make(map[string][]string)
|
|
|
|
|
|
|
2022-03-24 17:50:44 +08:00
|
|
|
|
// 表为空,则直接返回
|
|
|
|
|
|
if len(tableNames) == 0 {
|
|
|
|
|
|
rc.ResData = res
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取所有表下的所有列信息
|
2022-08-10 19:46:17 +08:00
|
|
|
|
columnMds := dm.GetColumns(tableNames...)
|
2021-07-28 18:03:19 +08:00
|
|
|
|
for _, v := range columnMds {
|
2023-05-24 12:32:17 +08:00
|
|
|
|
tName := v.TableName
|
2021-07-28 18:03:19 +08:00
|
|
|
|
if res[tName] == nil {
|
|
|
|
|
|
res[tName] = make([]string, 0)
|
2021-01-08 15:37:32 +08:00
|
|
|
|
}
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
2023-05-24 12:32:17 +08:00
|
|
|
|
columnName := fmt.Sprintf("%s [%s]", v.ColumnName, v.ColumnType)
|
|
|
|
|
|
comment := v.ColumnComment
|
2021-07-28 18:03:19 +08:00
|
|
|
|
// 如果字段备注不为空,则加上备注信息
|
2023-05-24 12:32:17 +08:00
|
|
|
|
if comment != "" {
|
2021-07-28 18:03:19 +08:00
|
|
|
|
columnName = fmt.Sprintf("%s[%s]", columnName, comment)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
res[tName] = append(res[tName], columnName)
|
2021-04-16 15:10:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
rc.ResData = res
|
2021-01-08 15:37:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// @router /api/db/:dbId/sql [post]
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (d *Db) SaveSql(rc *req.Ctx) {
|
2021-04-16 15:10:07 +08:00
|
|
|
|
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是否存在
|
2023-07-01 14:34:42 +08:00
|
|
|
|
err := gormx.GetById(new(entity.Db), dbId)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
biz.ErrIsNil(err, "该数据库信息不存在")
|
2021-01-08 15:37:32 +08:00
|
|
|
|
|
2021-04-16 15:10:07 +08:00
|
|
|
|
// 获取用于是否有该dbsql的保存记录,有则更改,否则新增
|
2022-05-08 14:10:57 +08:00
|
|
|
|
dbSql := &entity.DbSql{Type: dbSqlForm.Type, DbId: dbId, Name: dbSqlForm.Name, Db: dbSqlForm.Db}
|
2021-04-16 15:10:07 +08:00
|
|
|
|
dbSql.CreatorId = account.Id
|
2023-07-01 14:34:42 +08:00
|
|
|
|
e := gormx.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 {
|
2023-07-01 14:34:42 +08:00
|
|
|
|
gormx.UpdateById(dbSql)
|
2021-04-16 15:10:07 +08:00
|
|
|
|
} else {
|
2023-07-01 14:34:42 +08:00
|
|
|
|
gormx.Insert(dbSql)
|
2021-04-16 15:10:07 +08:00
|
|
|
|
}
|
2021-01-08 15:37:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-20 20:42:52 +08:00
|
|
|
|
// 获取所有保存的sql names
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (d *Db) GetSqlNames(rc *req.Ctx) {
|
2022-05-08 14:10:57 +08:00
|
|
|
|
id, db := GetIdAndDb(rc.GinCtx)
|
2021-12-20 20:42:52 +08:00
|
|
|
|
// 获取用于是否有该dbsql的保存记录,有则更改,否则新增
|
2022-05-08 14:10:57 +08:00
|
|
|
|
dbSql := &entity.DbSql{Type: 1, DbId: id, Db: db}
|
2021-12-20 20:42:52 +08:00
|
|
|
|
dbSql.CreatorId = rc.LoginAccount.Id
|
|
|
|
|
|
var sqls []entity.DbSql
|
2023-07-01 14:34:42 +08:00
|
|
|
|
gormx.ListBy(dbSql, &sqls, "id", "name")
|
2021-12-20 20:42:52 +08:00
|
|
|
|
|
|
|
|
|
|
rc.ResData = sqls
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 删除保存的sql
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (d *Db) DeleteSql(rc *req.Ctx) {
|
2021-12-20 20:42:52 +08:00
|
|
|
|
dbSql := &entity.DbSql{Type: 1, DbId: GetDbId(rc.GinCtx)}
|
|
|
|
|
|
dbSql.CreatorId = rc.LoginAccount.Id
|
|
|
|
|
|
dbSql.Name = rc.GinCtx.Query("name")
|
2023-02-13 21:11:16 +08:00
|
|
|
|
dbSql.Db = rc.GinCtx.Query("db")
|
2021-12-20 20:42:52 +08:00
|
|
|
|
|
2023-07-01 14:34:42 +08:00
|
|
|
|
gormx.DeleteByCondition(dbSql)
|
2021-12-20 20:42:52 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-08 15:37:32 +08:00
|
|
|
|
// @router /api/db/:dbId/sql [get]
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (d *Db) GetSql(rc *req.Ctx) {
|
2022-05-08 14:10:57 +08:00
|
|
|
|
id, db := GetIdAndDb(rc.GinCtx)
|
2021-12-20 20:42:52 +08:00
|
|
|
|
// 根据创建者id, 数据库id,以及sql模板名称查询保存的sql信息
|
2022-05-08 14:10:57 +08:00
|
|
|
|
dbSql := &entity.DbSql{Type: 1, DbId: id, Db: db}
|
2021-04-16 15:10:07 +08:00
|
|
|
|
dbSql.CreatorId = rc.LoginAccount.Id
|
2021-12-20 20:42:52 +08:00
|
|
|
|
dbSql.Name = rc.GinCtx.Query("name")
|
|
|
|
|
|
|
2023-07-01 14:34:42 +08:00
|
|
|
|
e := gormx.GetBy(dbSql)
|
2021-04-16 15:10:07 +08:00
|
|
|
|
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)
|
|
|
|
|
|
}
|
2022-05-08 14:10:57 +08:00
|
|
|
|
|
|
|
|
|
|
func GetIdAndDb(g *gin.Context) (uint64, string) {
|
|
|
|
|
|
db := g.Query("db")
|
|
|
|
|
|
biz.NotEmpty(db, "db不能为空")
|
|
|
|
|
|
return GetDbId(g), db
|
|
|
|
|
|
}
|