From 0394c9f35d840957f3310040e06e078f111bde7e Mon Sep 17 00:00:00 2001 From: taolx0 Date: Mon, 25 Nov 2024 17:50:16 +0800 Subject: [PATCH] feat:support batch save array or slice --- sqle/api/controller/v1/instance_audit_plan.go | 10 ++--- sqle/model/utils.go | 38 +++++++++++++++++++ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/sqle/api/controller/v1/instance_audit_plan.go b/sqle/api/controller/v1/instance_audit_plan.go index f7e93d086..0f9559e7b 100644 --- a/sqle/api/controller/v1/instance_audit_plan.go +++ b/sqle/api/controller/v1/instance_audit_plan.go @@ -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" @@ -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)) } diff --git a/sqle/model/utils.go b/sqle/model/utils.go index 73ee87936..babfd4561 100644 --- a/sqle/model/utils.go +++ b/sqle/model/utils.go @@ -522,6 +522,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) }