feat: 新增数据库sql执行记录&执行前原值等信息

This commit is contained in:
meilin.huang
2022-06-16 15:55:18 +08:00
parent f58331c1c1
commit 9b9173dea7
81 changed files with 559 additions and 1467 deletions

View File

@@ -21,9 +21,10 @@ import (
)
type Db struct {
DbApp application.Db
MsgApp sysApplication.Msg
ProjectApp application.Project
DbApp application.Db
DbSqlExecApp application.DbSqlExec
MsgApp sysApplication.Msg
ProjectApp application.Project
}
// @router /api/dbs [get]
@@ -49,7 +50,10 @@ func (d *Db) Save(rc *ctx.ReqCtx) {
}
func (d *Db) DeleteDb(rc *ctx.ReqCtx) {
d.DbApp.Delete(GetDbId(rc.GinCtx))
dbId := GetDbId(rc.GinCtx)
d.DbApp.Delete(dbId)
// 删除该库的sql执行记录
d.DbSqlExecApp.DeleteBy(&entity.DbSqlExec{DbId: dbId})
}
func (d *Db) TableInfos(rc *ctx.ReqCtx) {
@@ -68,19 +72,22 @@ func (d *Db) GetCreateTableDdl(rc *ctx.ReqCtx) {
rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetCreateTableDdl(tn)
}
// @router /api/db/:dbId/exec-sql [get]
func (d *Db) ExecSql(rc *ctx.ReqCtx) {
g := rc.GinCtx
form := &form.DbSqlExecForm{}
ginx.BindJsonAndValid(g, form)
id, db := GetIdAndDb(g)
id := GetDbId(g)
db := form.Db
dbInstance := d.DbApp.GetDbInstance(id, db)
biz.ErrIsNilAppendErr(d.ProjectApp.CanAccess(rc.LoginAccount.Id, dbInstance.ProjectId), "%s")
// 去除前后空格及换行符
sql := strings.TrimFunc(g.Query("sql"), func(r rune) bool {
sql := strings.TrimFunc(form.Sql, func(r rune) bool {
s := string(r)
return s == " " || s == "\n"
})
rc.ReqParam = fmt.Sprintf("db: %d:%s | sql: %s", id, db, sql)
biz.NotEmpty(sql, "sql不能为空")
@@ -92,6 +99,9 @@ func (d *Db) ExecSql(rc *ctx.ReqCtx) {
colAndRes["res"] = res
rc.ResData = colAndRes
} else {
// 根据执行sql生成执行记录
execRecord := d.DbSqlExecApp.GenExecLog(rc.LoginAccount, id, db, sql, dbInstance)
rowsAffected, err := dbInstance.Exec(sql)
biz.ErrIsNilAppendErr(err, "执行失败: %s")
res := make([]map[string]string, 0)
@@ -104,6 +114,11 @@ func (d *Db) ExecSql(rc *ctx.ReqCtx) {
colAndRes["res"] = res
rc.ResData = colAndRes
// 保存sql执行记录
if res[0]["影响条数"] > "0" {
execRecord.Remark = form.Remark
d.DbSqlExecApp.Save(execRecord)
}
}
}
@@ -180,7 +195,7 @@ func (d *Db) HintTables(rc *ctx.ReqCtx) {
tableNames := make([]string, 0)
for _, v := range tables {
tableNames = append(tableNames, v["tableName"])
tableNames = append(tableNames, v["tableName"].(string))
}
// key = 表名value = 列名数组
res := make(map[string][]string)
@@ -194,7 +209,7 @@ func (d *Db) HintTables(rc *ctx.ReqCtx) {
// 获取所有表下的所有列信息
columnMds := dbi.GetColumnMetadatas(tableNames...)
for _, v := range columnMds {
tName := v["tableName"]
tName := v["tableName"].(string)
if res[tName] == nil {
res[tName] = make([]string, 0)
}

View File

@@ -0,0 +1,23 @@
package api
import (
"mayfly-go/internal/devops/application"
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/pkg/ctx"
"mayfly-go/pkg/ginx"
)
type DbSqlExec struct {
DbSqlExecApp application.DbSqlExec
}
func (d *DbSqlExec) DbSqlExecs(rc *ctx.ReqCtx) {
g := rc.GinCtx
m := &entity.DbSqlExec{DbId: uint64(ginx.QueryInt(g, "dbId", 0)),
Db: g.Query("db"),
Table: g.Query("table"),
Type: int8(ginx.QueryInt(g, "type", 0)),
}
m.CreatorId = rc.LoginAccount.Id
rc.ResData = d.DbSqlExecApp.GetPageList(m, ginx.GetPageParam(rc.GinCtx), new([]entity.DbSqlExec))
}

View File

@@ -14,3 +14,10 @@ type DbForm struct {
Env string `json:"env"`
EnvId uint64 `binding:"required" json:"envId"`
}
// 数据库SQL执行表单
type DbSqlExecForm struct {
Db string `binding:"required" json:"db"` //数据库名
Sql string `binding:"required" json:"sql"` // 执行sql
Remark string `json:"remark"` // 执行备注
}

View File

@@ -12,6 +12,7 @@ import (
"mayfly-go/pkg/global"
"mayfly-go/pkg/model"
"mayfly-go/pkg/utils"
"reflect"
"strconv"
"strings"
"sync"
@@ -19,7 +20,7 @@ import (
)
type Db interface {
// 分页获取机器脚本信息列表
// 分页获取
GetPageList(condition *entity.Db, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult
Count(condition *entity.Db) int64
@@ -218,20 +219,20 @@ type DbInstance struct {
// 执行查询语句
// 依次返回 列名数组结果map错误
func (d *DbInstance) SelectData(sql string) ([]string, []map[string]string, error) {
sql = strings.Trim(sql, " ")
isSelect := strings.HasPrefix(sql, "SELECT") || strings.HasPrefix(sql, "select")
isShow := strings.HasPrefix(sql, "show")
func (d *DbInstance) SelectData(execSql string) ([]string, []map[string]interface{}, error) {
execSql = strings.Trim(execSql, " ")
isSelect := strings.HasPrefix(execSql, "SELECT") || strings.HasPrefix(execSql, "select")
isShow := strings.HasPrefix(execSql, "show")
if !isSelect && !isShow {
return nil, nil, errors.New("该sql非查询语句")
}
// 没加limit则默认限制50条
if isSelect && !strings.Contains(sql, "limit") && !strings.Contains(sql, "LIMIT") {
sql = sql + " LIMIT 50"
if isSelect && !strings.Contains(execSql, "limit") && !strings.Contains(execSql, "LIMIT") {
execSql = execSql + " LIMIT 50"
}
rows, err := d.db.Query(sql)
rows, err := d.db.Query(execSql)
if err != nil {
return nil, nil, err
}
@@ -242,18 +243,18 @@ func (d *DbInstance) SelectData(sql string) ([]string, []map[string]string, erro
rows.Close()
}
}()
cols, _ := rows.Columns()
colTypes, _ := rows.ColumnTypes()
// 这里表示一行填充数据
scans := make([]interface{}, len(cols))
scans := make([]interface{}, len(colTypes))
// 这里表示一行所有列的值,用[]byte表示
vals := make([][]byte, len(cols))
vals := make([][]byte, len(colTypes))
// 这里scans引用vals把数据填充到[]byte里
for k := range vals {
scans[k] = &vals[k]
}
result := make([]map[string]string, 0)
// 列名
result := make([]map[string]interface{}, 0)
// 列名用于前端表头名称按照数据库与查询字段顺序显示
colNames := make([]string, 0)
// 是否第一次遍历,列名数组只需第一次遍历时加入
isFirst := true
@@ -264,21 +265,53 @@ func (d *DbInstance) SelectData(sql string) ([]string, []map[string]string, erro
return nil, nil, err
}
// 每行数据
rowData := make(map[string]string)
rowData := make(map[string]interface{})
// 把vals中的数据复制到row中
for k, v := range vals {
key := cols[k]
for i, v := range vals {
colType := colTypes[i]
colName := colType.Name()
// 字段类型名
colScanType := colType.ScanType().Name()
// 如果是密码字段,则脱敏显示
if key == "password" {
if colName == "password" {
v = []byte("******")
}
if isFirst {
colNames = append(colNames, key)
colNames = append(colNames, colName)
}
// 这里把[]byte数据转成string
rowData[key] = string(v)
stringV := string(v)
if stringV == "" {
rowData[colName] = stringV
continue
}
if strings.Contains(colScanType, "int") || strings.Contains(colScanType, "Int") {
intV, _ := strconv.Atoi(stringV)
switch colType.ScanType().Kind() {
case reflect.Int8:
rowData[colName] = int8(intV)
case reflect.Uint8:
rowData[colName] = uint8(intV)
case reflect.Int64:
rowData[colName] = int64(intV)
case reflect.Uint64:
rowData[colName] = uint64(intV)
case reflect.Uint:
rowData[colName] = uint(intV)
default:
rowData[colName] = intV
}
continue
}
if strings.Contains(colScanType, "float") || strings.Contains(colScanType, "Float") {
floatV, _ := strconv.ParseFloat(stringV, 64)
rowData[colName] = floatV
} else {
rowData[colName] = stringV
}
}
//放入结果集
// 放入结果集
result = append(result, rowData)
isFirst = false
}
@@ -346,7 +379,7 @@ const (
WHERE table_name in (%s) AND table_schema = (SELECT database())`
)
func (d *DbInstance) GetTableMetedatas() []map[string]string {
func (d *DbInstance) GetTableMetedatas() []map[string]interface{} {
var sql string
if d.Type == "mysql" {
sql = MYSQL_TABLE_MA
@@ -355,7 +388,7 @@ func (d *DbInstance) GetTableMetedatas() []map[string]string {
return res
}
func (d *DbInstance) GetColumnMetadatas(tableNames ...string) []map[string]string {
func (d *DbInstance) GetColumnMetadatas(tableNames ...string) []map[string]interface{} {
var sql, tableName string
for i := 0; i < len(tableNames); i++ {
if i != 0 {
@@ -374,14 +407,15 @@ func (d *DbInstance) GetColumnMetadatas(tableNames ...string) []map[string]strin
countSql := fmt.Sprintf(countSqlTmp, tableName)
_, countRes, _ := d.SelectData(countSql)
// 查询出所有列信息总数,手动分页获取所有数据
maCount, _ := strconv.Atoi(countRes[0]["maNum"])
// maCount, _ := strconv.Atoi(countRes[0]["maNum"].(string))
maCount := int(countRes[0]["maNum"].(int64))
// 计算需要查询的页数
pageNum := maCount / DEFAULT_COLUMN_SIZE
if maCount%DEFAULT_COLUMN_SIZE > 0 {
pageNum++
}
res := make([]map[string]string, 0)
res := make([]map[string]interface{}, 0)
for index := 0; index < pageNum; index++ {
sql = fmt.Sprintf(sqlTmp, tableName, index*DEFAULT_COLUMN_SIZE, DEFAULT_COLUMN_SIZE)
_, result, err := d.SelectData(sql)
@@ -391,7 +425,12 @@ func (d *DbInstance) GetColumnMetadatas(tableNames ...string) []map[string]strin
return res
}
func (d *DbInstance) GetTableInfos() []map[string]string {
// 获取表的主键,目前默认第一列为主键
func (d *DbInstance) GetPrimaryKey(tablename string) string {
return d.GetColumnMetadatas(tablename)[0]["columnName"].(string)
}
func (d *DbInstance) GetTableInfos() []map[string]interface{} {
var sql string
if d.Type == "mysql" {
sql = MYSQL_TABLE_INFO
@@ -400,7 +439,7 @@ func (d *DbInstance) GetTableInfos() []map[string]string {
return res
}
func (d *DbInstance) GetTableIndex(tableName string) []map[string]string {
func (d *DbInstance) GetTableIndex(tableName string) []map[string]interface{} {
var sql string
if d.Type == "mysql" {
sql = fmt.Sprintf(MYSQL_INDEX_INFO, tableName)
@@ -409,7 +448,7 @@ func (d *DbInstance) GetTableIndex(tableName string) []map[string]string {
return res
}
func (d *DbInstance) GetCreateTableDdl(tableName string) []map[string]string {
func (d *DbInstance) GetCreateTableDdl(tableName string) []map[string]interface{} {
var sql string
if d.Type == "mysql" {
sql = fmt.Sprintf("show create table %s ", tableName)

View File

@@ -0,0 +1,127 @@
package application
import (
"encoding/json"
"fmt"
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/internal/devops/infrastructure/persistence"
"mayfly-go/pkg/global"
"mayfly-go/pkg/model"
"strings"
"github.com/xwb1989/sqlparser"
)
type DbSqlExec interface {
// 生成sql执行记录
GenExecLog(loginAccount *model.LoginAccount, dbId uint64, db string, sql string, dbInstance *DbInstance) *entity.DbSqlExec
// 保存sql执行记录
Save(*entity.DbSqlExec)
// 根据条件删除sql执行记录
DeleteBy(condition *entity.DbSqlExec)
// 分页获取
GetPageList(condition *entity.DbSqlExec, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult
}
type dbSqlExecAppImpl struct {
dbSqlExecRepo repository.DbSqlExec
}
var DbSqlExecApp DbSqlExec = &dbSqlExecAppImpl{
dbSqlExecRepo: persistence.DbSqlExecDao,
}
func (d *dbSqlExecAppImpl) GenExecLog(loginAccount *model.LoginAccount, dbId uint64, db string, sql string, dbInstance *DbInstance) *entity.DbSqlExec {
defer func() {
if err := recover(); err != nil {
global.Log.Error("生成sql执行记录失败", err)
}
}()
stmt, err := sqlparser.Parse(sql)
if err != nil {
global.Log.Error("记录数据库sql执行记录失败", err)
}
dbSqlExecRecord := new(entity.DbSqlExec)
dbSqlExecRecord.DbId = dbId
dbSqlExecRecord.Db = db
dbSqlExecRecord.Sql = sql
dbSqlExecRecord.SetBaseInfo(loginAccount)
switch stmt := stmt.(type) {
case *sqlparser.Update:
doUpdate(stmt, dbInstance, dbSqlExecRecord)
case *sqlparser.Delete:
doDelete(stmt, dbInstance, dbSqlExecRecord)
case *sqlparser.Insert:
doInsert(stmt, dbSqlExecRecord)
}
return dbSqlExecRecord
}
func (d *dbSqlExecAppImpl) Save(dse *entity.DbSqlExec) {
d.dbSqlExecRepo.Insert(dse)
}
func (d *dbSqlExecAppImpl) DeleteBy(condition *entity.DbSqlExec) {
d.dbSqlExecRepo.DeleteBy(condition)
}
func (d *dbSqlExecAppImpl) GetPageList(condition *entity.DbSqlExec, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
return d.dbSqlExecRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
}
func doUpdate(update *sqlparser.Update, dbInstance *DbInstance, dbSqlExec *entity.DbSqlExec) {
tableStr := sqlparser.String(update.TableExprs)
// 可能使用别名,故空格切割
tableName := strings.Split(tableStr, " ")[0]
where := sqlparser.String(update.Where)
updateExprs := update.Exprs
updateColumns := make([]string, 0)
for _, v := range updateExprs {
updateColumns = append(updateColumns, v.Name.Name.String())
}
// 获取表主键列名,排除使用别名
primaryKey := dbInstance.GetPrimaryKey(tableName)
updateColumnsAndPrimaryKey := strings.Join(updateColumns, ",") + "," + primaryKey
// 查询要更新字段数据的旧值,以及主键值
selectSql := fmt.Sprintf("SELECT %s FROM %s %s LIMIT 200", updateColumnsAndPrimaryKey, tableStr, where)
_, res, _ := dbInstance.SelectData(selectSql)
bytes, _ := json.Marshal(res)
dbSqlExec.OldValue = string(bytes)
dbSqlExec.Table = tableName
dbSqlExec.Type = entity.DbSqlExecTypeUpdate
}
func doDelete(delete *sqlparser.Delete, dbInstance *DbInstance, dbSqlExec *entity.DbSqlExec) {
tableStr := sqlparser.String(delete.TableExprs)
// 可能使用别名,故空格切割
table := strings.Split(tableStr, " ")[0]
where := sqlparser.String(delete.Where)
// 查询删除数据
selectSql := fmt.Sprintf("SELECT * FROM %s %s LIMIT 200", tableStr, where)
_, res, _ := dbInstance.SelectData(selectSql)
bytes, _ := json.Marshal(res)
dbSqlExec.OldValue = string(bytes)
dbSqlExec.Table = table
dbSqlExec.Type = entity.DbSqlExecTypeDelete
}
func doInsert(insert *sqlparser.Insert, dbSqlExec *entity.DbSqlExec) {
tableStr := sqlparser.String(insert.Table)
// 可能使用别名,故空格切割
table := strings.Split(tableStr, " ")[0]
dbSqlExec.Table = table
dbSqlExec.Type = entity.DbSqlExecTypeInsert
}

View File

@@ -0,0 +1,22 @@
package entity
import "mayfly-go/pkg/model"
// 数据库sql执行记录
type DbSqlExec struct {
model.Model `orm:"-"`
DbId uint64 `json:"dbId"`
Db string `json:"db"`
Table string `json:"table"`
Type int8 `json:"type"` // 类型
Sql string `json:"sql"` // 执行的sql
OldValue string `json:"oldValue"`
Remark string `json:"remark"`
}
const (
DbSqlExecTypeUpdate int8 = 1 // 更新类型
DbSqlExecTypeDelete int8 = 2 // 删除类型
DbSqlExecTypeInsert int8 = 3 // 插入类型
)

View File

@@ -0,0 +1,15 @@
package repository
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/pkg/model"
)
type DbSqlExec interface {
Insert(d *entity.DbSqlExec)
DeleteBy(condition *entity.DbSqlExec)
// 分页获取
GetPageList(condition *entity.DbSqlExec, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult
}

View File

@@ -0,0 +1,25 @@
package persistence
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type dbSqlExecRepo struct{}
var DbSqlExecDao repository.DbSqlExec = &dbSqlExecRepo{}
func (d *dbSqlExecRepo) Insert(dse *entity.DbSqlExec) {
model.Insert(dse)
}
func (d *dbSqlExecRepo) DeleteBy(condition *entity.DbSqlExec) {
biz.ErrIsNil(model.DeleteByCondition(condition), "删除sql执行记录失败")
}
// 分页获取
func (d *dbSqlExecRepo) GetPageList(condition *entity.DbSqlExec, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
return model.GetPage(pageParam, condition, toEntity, orderBy...)
}

View File

@@ -13,9 +13,10 @@ func InitDbRouter(router *gin.RouterGroup) {
db := router.Group("dbs")
{
d := &api.Db{
DbApp: application.DbApp,
MsgApp: sysApplication.MsgApp,
ProjectApp: application.ProjectApp,
DbApp: application.DbApp,
DbSqlExecApp: application.DbSqlExecApp,
MsgApp: sysApplication.MsgApp,
ProjectApp: application.ProjectApp,
}
// 获取所有数据库列表
db.GET("", func(c *gin.Context) {
@@ -50,7 +51,7 @@ func InitDbRouter(router *gin.RouterGroup) {
})
// db.GET(":dbId/exec-sql", controllers.SelectData)
db.GET(":dbId/exec-sql", func(g *gin.Context) {
db.POST(":dbId/exec-sql", func(g *gin.Context) {
rc := ctx.NewReqCtxWithGin(g).WithLog(ctx.NewLogInfo("执行Sql语句"))
rc.Handle(d.ExecSql)
})

View File

@@ -0,0 +1,23 @@
package router
import (
"mayfly-go/internal/devops/api"
"mayfly-go/internal/devops/application"
"mayfly-go/pkg/ctx"
"github.com/gin-gonic/gin"
)
func InitDbSqlExecRouter(router *gin.RouterGroup) {
db := router.Group("/dbs/:dbId/sql-execs")
{
d := &api.DbSqlExec{
DbSqlExecApp: application.DbSqlExecApp,
}
// 获取所有数据库sql执行记录列表
db.GET("", func(c *gin.Context) {
rc := ctx.NewReqCtxWithGin(c)
rc.Handle(d.DbSqlExecs)
})
}
}