feature: 执行 SQL 脚本时显示执行进度

This commit is contained in:
kanzihuang
2023-10-15 10:36:01 +08:00
committed by wanli
parent a64b894b08
commit 361eafedae
5 changed files with 36 additions and 17 deletions

View File

@@ -45,7 +45,7 @@ export default {
notification: undefined,
}
}
progress.props.progress.sqlFileName = content.sqlFileName
progress.props.progress.title = content.title
progress.props.progress.executedStatements = content.executedStatements
if (!notifyMap.has(id)) {
const vNodeMessage = createVNode(

View File

@@ -1,7 +1,7 @@
export const buildProgressProps = (): any => {
return {
progress: {
sqlFileName: {
title: {
type: String,
},
executedStatements: {

View File

@@ -1,5 +1,5 @@
<template>
<el-descriptions border size="small" :title="`${progress.sqlFileName}`">
<el-descriptions border size="small" :title="`${progress.title}`">
<el-descriptions-item label="时间">{{ state.elapsedTime }}</el-descriptions-item>
<el-descriptions-item label="已处理">{{ progress.executedStatements }}</el-descriptions-item>
</el-descriptions>

View File

@@ -125,7 +125,30 @@ func (d *Db) ExecSql(rc *req.Ctx) {
biz.ErrIsNil(err, "SQL解析错误,请检查您的执行SQL")
isMulti := len(sqls) > 1
var execResAll *application.DbSqlExecRes
progressId := uniqueid.IncrementID()
executedStatements := 0
progressTitle := fmt.Sprintf("%s/%s", dbConn.Info.Name, dbConn.Info.Database)
defer ws.SendJsonMsg(rc.LoginAccount.Id, msgdto.InfoSysMsg("sql脚本执行进度", &progressMsg{
Id: progressId,
Title: progressTitle,
ExecutedStatements: executedStatements,
Terminated: true,
}).WithCategory(progressCategory))
ticker := time.NewTicker(time.Second * 1)
defer ticker.Stop()
for _, s := range sqls {
select {
case <-ticker.C:
ws.SendJsonMsg(rc.LoginAccount.Id, msgdto.InfoSysMsg("sql脚本执行进度", &progressMsg{
Id: progressId,
Title: progressTitle,
ExecutedStatements: executedStatements,
Terminated: false,
}).WithCategory(progressCategory))
default:
}
executedStatements++
s = stringx.TrimSpaceAndBr(s)
// 多条执行,如果有查询语句,则跳过
if isMulti && strings.HasPrefix(strings.ToLower(s), "select") {
@@ -156,7 +179,7 @@ const progressCategory = "execSqlFileProgress"
// progressMsg sql文件执行进度消息
type progressMsg struct {
Id uint64 `json:"id"`
SqlFileName string `json:"sqlFileName"`
Title string `json:"title"`
ExecutedStatements int `json:"executedStatements"`
Terminated bool `json:"terminated"`
}
@@ -198,17 +221,17 @@ func (d *Db) ExecSqlFile(rc *req.Ctx) {
LoginAccount: rc.LoginAccount,
}
var sql string
tokenizer := sqlparser.NewReaderTokenizer(file, sqlparser.WithCacheInBuffer())
progressId := uniqueid.IncrementID()
executedStatements := 0
defer ws.SendJsonMsg(rc.LoginAccount.Id, msgdto.InfoSysMsg("sql脚本执行进度", &progressMsg{
Id: progressId,
SqlFileName: filename,
Title: filename,
ExecutedStatements: executedStatements,
Terminated: true,
}).WithCategory(progressCategory))
var sql string
tokenizer := sqlparser.NewReaderTokenizer(file, sqlparser.WithCacheInBuffer())
ticker := time.NewTicker(time.Second * 1)
defer ticker.Stop()
for {
@@ -216,12 +239,13 @@ func (d *Db) ExecSqlFile(rc *req.Ctx) {
case <-ticker.C:
ws.SendJsonMsg(rc.LoginAccount.Id, msgdto.InfoSysMsg("sql脚本执行进度", &progressMsg{
Id: progressId,
SqlFileName: filename,
Title: filename,
ExecutedStatements: executedStatements,
Terminated: false,
}).WithCategory(progressCategory))
default:
}
executedStatements++
sql, err = sqlparser.SplitNext(tokenizer)
if err == io.EOF {
break
@@ -259,7 +283,6 @@ func (d *Db) ExecSqlFile(rc *req.Ctx) {
d.MsgApp.CreateAndSend(rc.LoginAccount, msgdto.ErrSysMsg("sql脚本执行失败", fmt.Sprintf("[%s][%s] -> sql=[%s] 执行失败: [%s]", filename, dbConn.Info.GetLogDesc(), sql, err.Error())))
return
}
executedStatements++
}
d.MsgApp.CreateAndSend(rc.LoginAccount, msgdto.SuccessSysMsg("sql脚本执行成功", fmt.Sprintf("sql脚本执行完成%s", rc.ReqParam)))
}

View File

@@ -1,14 +1,12 @@
package entity
import (
"testing"
"github.com/stretchr/testify/require"
"testing"
)
func Test_escapeSql(t *testing.T) {
func Test_QuoteLiteral(t *testing.T) {
tests := []struct {
name string
dbType DbType
sql string
want string
@@ -24,7 +22,6 @@ func Test_escapeSql(t *testing.T) {
want: "'''a'''",
},
{
name: "不间断空格",
dbType: DbTypeMysql,
sql: "a\u00A0b",
want: "'a\u00A0b'",
@@ -40,14 +37,13 @@ func Test_escapeSql(t *testing.T) {
want: "'''a'''",
},
{
name: "不间断空格",
dbType: DbTypePostgres,
sql: "a\u00A0b",
want: "'a\u00A0b'",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Run(string(tt.dbType)+"_"+tt.sql, func(t *testing.T) {
got := tt.dbType.QuoteLiteral(tt.sql)
require.Equal(t, tt.want, got)
})