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 a6510f3
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 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 All @@ -604,21 +610,6 @@ func swapSectors(tx txn, contractID int64, i, j uint64) error {
return fmt.Errorf("expected 1 row affected, got %v", rows)
}

func() {
rows, err := tx.Query(`SELECT sector_id, root_index FROM contract_sector_roots WHERE contract_id=$1 AND root_index IN ($2, $3) ORDER BY root_index ASC;`, contractID, i, j)
if err != nil {
panic(fmt.Errorf("failed to query sector IDs: %w", err))
}
defer rows.Close()
for rows.Next() {
var id int64
var index int64
if err := rows.Scan(&id, &index); err != nil {
panic(fmt.Errorf("failed to scan sector ID: %w", err))
}
}
}()

return nil
}

Expand Down

0 comments on commit a6510f3

Please sign in to comment.