Skip to content

Commit

Permalink
fix: usage of rows.Err and rows.Close
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed Sep 17, 2024
1 parent b593fc6 commit 99a010c
Showing 1 changed file with 2 additions and 13 deletions.
15 changes: 2 additions & 13 deletions persistence/sql/batch/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func Create[T any](ctx context.Context, p *TracerConnection, models []*T, opts .
// NOTHING, meaning that MySQL will always fail the whole transaction on a single
// record conflict.
if conn.Dialect.Name() == dbal.DriverMySQL {
return sqlcon.HandleError(rows.Close())
return nil
}

if options.partialInserts {
Expand All @@ -282,9 +282,6 @@ func Create[T any](ctx context.Context, p *TracerConnection, models []*T, opts .
func handleFullInserts[T any](models []*T, rows *sql.Rows) error {
// Hydrate the models from the RETURNING clause.
for i := 0; rows.Next(); i++ {
if err := rows.Err(); err != nil {
return sqlcon.HandleError(err)
}
var id uuid.UUID
if err := rows.Scan(&id); err != nil {
return errors.WithStack(err)
Expand All @@ -297,16 +294,13 @@ func handleFullInserts[T any](models []*T, rows *sql.Rows) error {
return sqlcon.HandleError(err)
}

return sqlcon.HandleError(rows.Close())
return nil
}

func handlePartialInserts[T any](queryArgs insertQueryArgs, values []any, models []*T, rows *sql.Rows) error {
// Hydrate the models from the RETURNING clause.
idsInDB := make(map[uuid.UUID]struct{})
for rows.Next() {
if err := rows.Err(); err != nil {
return sqlcon.HandleError(err)
}
var id uuid.UUID
if err := rows.Scan(&id); err != nil {
return errors.WithStack(err)
Expand All @@ -317,10 +311,6 @@ func handlePartialInserts[T any](queryArgs insertQueryArgs, values []any, models
return sqlcon.HandleError(err)
}

if err := rows.Close(); err != nil {
return sqlcon.HandleError(err)
}

idIdx := slices.Index(queryArgs.Columns, "id")
if idIdx == -1 {
return errors.New("id column not found")
Expand All @@ -346,7 +336,6 @@ func handlePartialInserts[T any](queryArgs insertQueryArgs, values []any, models
}

return nil

}

// setModelID sets the id field of the model to the id.
Expand Down

0 comments on commit 99a010c

Please sign in to comment.