Skip to content

Commit

Permalink
sqlite: use statement for swap
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Oct 4, 2023
1 parent 96ba098 commit 5a25e57
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions persist/sqlite/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,13 @@ func swapSectors(tx txn, contractID int64, i, j uint64) error {
return errors.New("failed to find both sectors")
}

res, err := tx.Exec(`UPDATE contract_sector_roots SET sector_id=$1 WHERE id=$2`, records[1].sectorID, records[0].dbID)
stmt, err := tx.Prepare(`UPDATE contract_sector_roots SET sector_id=$1 WHERE id=$2`)
if err != nil {
return fmt.Errorf("failed to prepare update statement: %w", err)
}
defer stmt.Close()

res, err := stmt.Exec(records[1].sectorID, records[0].dbID)
if err != nil {
return fmt.Errorf("failed to update sector ID: %w", err)
} else if rows, err := res.RowsAffected(); err != nil {
Expand All @@ -595,7 +601,7 @@ func swapSectors(tx txn, contractID int64, i, j uint64) error {
return fmt.Errorf("expected 1 row affected, got %v", rows)
}

res, err = tx.Exec(`UPDATE contract_sector_roots SET sector_id=$1 WHERE id=$2`, records[0].sectorID, records[1].dbID)
res, err = stmt.Exec(records[0].sectorID, records[1].dbID)
if err != nil {
return fmt.Errorf("failed to update sector ID: %w", err)
} else if rows, err := res.RowsAffected(); err != nil {
Expand Down

0 comments on commit 5a25e57

Please sign in to comment.