Skip to content

Commit

Permalink
Merge pull request #2780 from actiontech/issue-1824
Browse files Browse the repository at this point in the history
feat:support batch save array or slice
  • Loading branch information
winfredLIN authored Nov 27, 2024
2 parents 5b2bc41 + 0394c9f commit 63f8b06
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
10 changes: 3 additions & 7 deletions sqle/api/controller/v1/instance_audit_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/actiontech/sqle/sqle/model"
"github.com/actiontech/sqle/sqle/pkg/params"
"github.com/actiontech/sqle/sqle/server/auditplan"

"github.com/actiontech/sqle/sqle/utils"
"github.com/labstack/echo/v4"
dry "github.com/ungerik/go-dry"
Expand Down Expand Up @@ -1422,13 +1421,10 @@ func AuditPlanTriggerSqlAudit(c echo.Context) error {
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}
auditedSqls, err := auditplan.BatchAuditSQLs(auditPlanSqls, false)
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}
err = s.Save(auditedSqls)
auditedSqlList, err := auditplan.BatchAuditSQLs(auditPlanSqls, false)
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}
return controller.JSONBaseErrorReq(c, nil)

return controller.JSONBaseErrorReq(c, s.BatchSave(auditedSqlList, 50))
}
38 changes: 38 additions & 0 deletions sqle/model/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,44 @@ func (s *Storage) Save(model interface{}) error {
return errors.New(errors.ConnectStorageError, s.db.Save(model).Error)
}

func (s *Storage) BatchSave(value any, batchSize int) error {
reflectValue := reflect.Indirect(reflect.ValueOf(value))
if reflectValue.Kind() != reflect.Slice && reflectValue.Kind() != reflect.Array {
return s.db.Save(value).Error
}

sliceLen := reflectValue.Len()
if sliceLen == 0 {
return nil
}

txDB := s.db.Begin()
defer func() {
if r := recover(); r != nil {
txDB.Rollback()
}
}()

for i := 0; i < sliceLen; i += batchSize {
end := i + batchSize
if end > sliceLen {
end = sliceLen
}

if err := txDB.Save(reflectValue.Slice(i, end).Interface()).Error; err != nil {
txDB.Rollback()
return errors.ConnectStorageErrWrapper(err)
}
}

if err := txDB.Commit().Error; err != nil {
txDB.Rollback()
return errors.ConnectStorageErrWrapper(err)
}

return nil
}

func (s *Storage) Update(model interface{}, attrs ...interface{}) error {
return errors.New(errors.ConnectStorageError, s.db.Model(model).UpdateColumns(attrs).Error)
}
Expand Down

0 comments on commit 63f8b06

Please sign in to comment.