feat: 支持执行多条sql

This commit is contained in:
meilin.huang
2022-11-02 19:27:40 +08:00
parent 2c863a2774
commit ad616496d1
6 changed files with 70 additions and 21 deletions

View File

@@ -25,6 +25,21 @@ type DbSqlExecRes struct {
Res []map[string]interface{}
}
// 合并执行结果主要用于执行多条sql使用
func (d *DbSqlExecRes) Merge(execRes *DbSqlExecRes) {
canMerge := len(d.ColNames) == len(execRes.ColNames)
if !canMerge {
return
}
// 列名不一致,则不合并
for i, colName := range d.ColNames {
if execRes.ColNames[i] != colName {
return
}
}
d.Res = append(d.Res, execRes.Res...)
}
type DbSqlExec interface {
// 执行sql
Exec(execSqlReq *DbSqlExecReq) (*DbSqlExecRes, error)
@@ -187,16 +202,19 @@ func doInsert(insert *sqlparser.Insert, execSqlReq *DbSqlExecReq, dbSqlExec *ent
func doExec(sql string, dbInstance *DbInstance) (*DbSqlExecRes, error) {
rowsAffected, err := dbInstance.Exec(sql)
execRes := "success"
if err != nil {
return nil, err
execRes = err.Error()
}
res := make([]map[string]interface{}, 0)
resData := make(map[string]interface{})
resData["影响条数"] = rowsAffected
resData["rowsAffected"] = rowsAffected
resData["sql"] = sql
resData["result"] = execRes
res = append(res, resData)
return &DbSqlExecRes{
ColNames: []string{"影响条数"},
ColNames: []string{"sql", "rowsAffected", "result"},
Res: res,
}, nil
}