From 4943e90d774665cdb0b9d28c313abf16c8de13c0 Mon Sep 17 00:00:00 2001 From: Abolfazl Nourzad Date: Sat, 3 Feb 2024 05:03:58 +0000 Subject: [PATCH] :fire: fix(repository) multiple insert data & remove prepare statement (#28) Reviewed-on: https://git.gocasts.ir/ebhomengo/niki/pulls/28 Co-authored-by: Abolfazl Nourzad Co-committed-by: Abolfazl Nourzad --- repository/mysql/kind_box/kind_box.go | 59 ++++--------------- repository/mysql/kind_box_req/kind_box_req.go | 52 +++------------- 2 files changed, 18 insertions(+), 93 deletions(-) diff --git a/repository/mysql/kind_box/kind_box.go b/repository/mysql/kind_box/kind_box.go index d147645..ee077d3 100644 --- a/repository/mysql/kind_box/kind_box.go +++ b/repository/mysql/kind_box/kind_box.go @@ -4,64 +4,25 @@ import ( "context" "git.gocasts.ir/ebhomengo/niki/entity" - "git.gocasts.ir/ebhomengo/niki/logger" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) error { const op = "mysqlkindbox.AddBatchKindBoxConcurrentlyRollback" - errCh := make(chan error, len(kindBoxes)) - resultCh := make(chan entity.KindBox, len(kindBoxes)) - tx, tErr := d.conn.Conn().Begin() - if tErr != nil { - return richerror.New(op).WithErr(tErr). - WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + + queryStr := "INSERT INTO kind_boxes (kind_box_req_id, benefactor_id, type, serial_number, status) VALUES " + values := []any{} + + for _, kb := range kindBoxes { + queryStr += "(?, ?, ?, ?, ?)," + values = append(values, kb.KindBoxReqID, kb.BenefactorID, kb.Type, kb.SerialNumber, kb.Status.String()) } - for _, kindBox := range kindBoxes { - go func(kb entity.KindBox) { - _, err := tx.ExecContext(ctx, - "insert into kind_boxes (kind_box_req_id, benefactor_id, type, serial_number, status) values (?, ?, ?, ?, ?);", - kb.KindBoxReqID, kb.BenefactorID, kb.Type.String(), kb.SerialNumber, kb.Status.String(), - ) - if err != nil { - errCh <- richerror.New(op).WithErr(err). - WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) - - return - } - - resultCh <- kb - errCh <- nil - }(kindBox) - } - - for i := 0; i < len(kindBoxes); i++ { - select { - case err := <-errCh: - if err != nil { - if err := tx.Rollback(); err != nil { - logger.L().Error("Rollback error: ", err) - } - - return err - } - case <-resultCh: - - case <-ctx.Done(): - if err := tx.Rollback(); err != nil { - logger.L().Error("Rollback error: ", err) - } - - return richerror.New(op).WithErr(ctx.Err()). - WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) - } - } - - if err := tx.Commit(); err != nil { - logger.L().Error("Commit error: ", err) + // trim the last , + queryStr = queryStr[0 : len(queryStr)-1] + if _, err := d.conn.Conn().ExecContext(ctx, queryStr, values...); err != nil { return richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } diff --git a/repository/mysql/kind_box_req/kind_box_req.go b/repository/mysql/kind_box_req/kind_box_req.go index 7605809..823d3c4 100644 --- a/repository/mysql/kind_box_req/kind_box_req.go +++ b/repository/mysql/kind_box_req/kind_box_req.go @@ -28,27 +28,14 @@ func (d DB) AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (en return kindBoxReq, nil } -func (d DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID, countAccepted uint) (finalErr error) { +func (d DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID, countAccepted uint) error { op := richerror.Op("mysqlkindboxreq.AcceptKindBoxReq") - statement, err := d.conn.Conn(). - Prepare(`update kind_box_reqs set count_accepted = ? , status = ? where id = ?`) - - defer func() { - cErr := statement.Close() - if cErr != nil { - finalErr = cErr - } - }() - + _, err := d.conn.Conn().ExecContext(ctx, `update kind_box_reqs set count_accepted = ? , status = ? where id = ?`, + countAccepted, entity.KindBoxReqAcceptedStatus.String(), kindBoxReqID) if err != nil { return richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } - _, eErr := statement.ExecContext(ctx, countAccepted, entity.KindBoxReqAcceptedStatus.String(), kindBoxReqID) - if eErr != nil { - return richerror.New(op).WithErr(eErr). - WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) - } return nil } @@ -81,48 +68,25 @@ func (d DB) KindBoxRequestExist(id uint) (bool, error) { return true, nil } -func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) (finalErr error) { +func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error { op := richerror.Op("mysqlkindboxreq.RejectKindBoxReq") - statement, err := d.conn.Conn(). - Prepare(`update kind_box_reqs set description = ? , status = ? where id = ?`) - defer func() { - cErr := statement.Close() - if cErr != nil { - finalErr = cErr - } - }() + _, err := d.conn.Conn().ExecContext(ctx, `update kind_box_reqs set description = ? , status = ? where id = ?`, + description, entity.KindBoxReqRejectedStatus.String(), kindBoxReqID) if err != nil { return richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } - _, eErr := statement.ExecContext(ctx, description, entity.KindBoxReqRejectedStatus.String(), kindBoxReqID) - if eErr != nil { - return richerror.New(op).WithErr(eErr). - WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) - } return nil } -func (d DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) (finalErr error) { +func (d DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) error { op := richerror.Op("mysqlkindboxreq.RollbackKindBoxRequestStatus") - statement, err := d.conn.Conn(). - Prepare(`update kind_box_reqs set status = ? where id = ?`) - defer func() { - cErr := statement.Close() - if cErr != nil { - finalErr = cErr - } - }() + _, err := d.conn.Conn().ExecContext(ctx, `update kind_box_reqs set status = ? where id = ?`, entity.KindBoxReqPendingStatus.String(), id) if err != nil { return richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } - _, eErr := statement.ExecContext(ctx, entity.KindBoxReqPendingStatus.String(), id) - if eErr != nil { - return richerror.New(op).WithErr(eErr). - WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) - } return nil }