fix: UpdateStmt doesn't update the statement correctly in sqlx/bulkinserter.go (#3607)

This commit is contained in:
Kevin Wan
2023-10-09 21:57:26 +08:00
committed by GitHub
parent 5aedd9c076
commit abd1fa96a9
3 changed files with 69 additions and 4 deletions

View File

@@ -4,6 +4,7 @@ import (
"database/sql"
"fmt"
"strings"
"sync"
"time"
"github.com/zeromicro/go-zero/core/executors"
@@ -30,6 +31,7 @@ type (
executor *executors.PeriodicalExecutor
inserter *dbInserter
stmt bulkStmt
lock sync.RWMutex // guards stmt
}
bulkStmt struct {
@@ -65,6 +67,9 @@ func (bi *BulkInserter) Flush() {
// Insert inserts given args.
func (bi *BulkInserter) Insert(args ...any) error {
bi.lock.RLock()
defer bi.lock.RUnlock()
value, err := format(bi.stmt.valueFormat, args...)
if err != nil {
return err
@@ -95,6 +100,11 @@ func (bi *BulkInserter) UpdateStmt(stmt string) error {
return err
}
bi.lock.Lock()
defer bi.lock.Unlock()
// with write lock, it doesn't matter what's the order of setting bi.stmt and calling flush.
bi.stmt = bkStmt
bi.executor.Flush()
bi.executor.Sync(func() {
bi.inserter.stmt = bkStmt