作者 yangfu

feat: row batch delete

package command
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type RowsDeleteCommand struct {
// 表Id
TableId int `cname:"表Id" json:"tableId" valid:"Required"`
Where domain.Where `json:"where"`
}
func (cmd *RowsDeleteCommand) Valid(validation *validation.Validation) {
}
func (cmd *RowsDeleteCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(cmd)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(cmd).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
... ... @@ -97,6 +97,42 @@ func (tableService *TableService) RowEditV2(ctx *domain.Context, cmd *command.Ro
return struct{}{}, nil
}
func (tableService *TableService) RowsDelete(ctx *domain.Context, cmd *command.RowsDeleteCommand) (interface{}, error) {
if err := cmd.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
//if err := transactionContext.StartTransaction(); err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
//}
//defer func() {
// transactionContext.RollbackTransaction()
//}()
var table *domain.Table
_, table, err = factory.FastPgTable(transactionContext, cmd.TableId)
if err != nil {
return nil, factory.FastError(err)
}
var options = starrocks.QueryOptions{
TableName: table.SQLName,
Select: []*domain.Field{domain.PK()}, //table.Fields(true),
Where: []starrocks.Condition{},
}
options.SetCondition(cmd.Where.Conditions)
total, err := starrocks.WrapDeleteFuncWithDB(starrocks.DB)(options)
//if err := transactionContext.CommitTransaction(); err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
//}
return map[string]interface{}{
"rowsAffected": total,
}, nil
}
func MapArrayToFieldValues(list []map[string]string, table *domain.Table, dataTable *domain.DataTable, mustMatch bool) []*domain.FieldValues {
var result = make([]*domain.FieldValues, 0)
history := dto.ToFieldDataByPK(table, dataTable)
... ...
... ... @@ -342,6 +342,43 @@ func WrapQueryFuncWithDB(db *gorm.DB) func(QueryOptions) (*sql.Rows, error) {
}
}
func WrapDeleteFuncWithDB(db *gorm.DB) func(QueryOptions) (int64, error) {
return func(params QueryOptions) (int64, error) {
query := db.Table(params.TableName)
queryWithoutLimitOffset(query, params)
//if params.Offset > 0 {
// query.Offset(params.Offset)
//}
//if params.Limit > 0 {
// query.Limit(params.Limit)
//}
//if params.Context != nil {
// query.Where(fmt.Sprintf("context->>'companyId'='%v'", params.Context.CompanyId))
//}
rows, err := query.Rows()
defer rows.Close()
if err != nil {
return 0, err
}
dataTable := &domain.DataTable{}
dataTable.Data, err = ScanRows(rows)
idList := make([]string, 0)
for _, row := range dataTable.Data {
if len(row) == 0 {
continue
}
idList = append(idList, row[0])
}
if len(idList) == 0 {
return 0, nil
}
c := Condition{}
sql := fmt.Sprintf("delete from %v where id in %v", params.TableName, c.InArgs(idList))
query = db.Exec(sql)
return int64(len(idList)), query.Error
}
}
func SetTable(query *gorm.DB, tableName string) {
query.Statement.Table = tableName
}
... ...
... ... @@ -279,6 +279,14 @@ func (controller *TableController) RowEdit() {
controller.Response(data, err)
}
func (controller *TableController) RowsDelete() {
tableService := service.NewTableService(nil)
cmd := &command.RowsDeleteCommand{}
Must(controller.Unmarshal(cmd))
data, err := tableService.RowsDelete(ParseContext(controller.BaseController), cmd)
controller.Response(data, err)
}
func (controller *TableController) DependencyGraph() {
tableService := service.NewTableService(nil)
updateTableCommand := &query.GetTableQuery{}
... ...
... ... @@ -34,6 +34,7 @@ func init() {
web.Router("/data/tables/table-preview", tableController, "Post:TablePreview")
web.Router("/data/tables/row-edit", tableController, "Post:RowEdit")
web.Router("/data/tables/row-delete", tableController, "Post:RowsDelete")
web.Router("/data/field-optional-values", tableController, "Post:FieldOptionalValues")
web.Router("/data/table-object-search", tableController, "Post:TableObjectSearch")
... ...