Skip to content

Commit

Permalink
fix (CreateItems): call Before/After write ops once
Browse files Browse the repository at this point in the history
Only call WithBeforeWrite(...) and
WithAfterWrite(...) once for all items vs for
every individual item.
  • Loading branch information
jimlambrt committed Jul 29, 2024
1 parent 409452c commit c89680f
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions create.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ func (rw *RW) CreateItems(ctx context.Context, createItems interface{}, opt ...O
}
}
}
}

if opts.WithBeforeWrite != nil {
if err := opts.WithBeforeWrite(valCreateItems.Index(i).Interface()); err != nil {
return fmt.Errorf("%s: error before write: %w", op, err)
}
if opts.WithBeforeWrite != nil {
if err := opts.WithBeforeWrite(createItems); err != nil {
return fmt.Errorf("%s: error before write: %w", op, err)
}
}

Expand Down Expand Up @@ -326,10 +326,8 @@ func (rw *RW) CreateItems(ctx context.Context, createItems interface{}, opt ...O
*opts.WithRowsAffected = tx.RowsAffected
}
if tx.RowsAffected > 0 && opts.WithAfterWrite != nil {
for i := 0; i < valCreateItems.Len(); i++ {
if err := opts.WithAfterWrite(valCreateItems.Index(i).Interface(), int(tx.RowsAffected)); err != nil {
return fmt.Errorf("%s: error after write: %w", op, err)
}
if err := opts.WithAfterWrite(createItems, int(tx.RowsAffected)); err != nil {
return fmt.Errorf("%s: error after write: %w", op, err)
}
}
return nil
Expand Down

0 comments on commit c89680f

Please sign in to comment.