Skip to content

Commit

Permalink
Code review fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
popescu-af committed Jul 18, 2023
1 parent 954942c commit d4965de
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions row.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,22 @@ func (r *Row) scanCurrentRow() error {
return createError(fmt.Errorf("unknown columns to map to: %v", r.unknownDestinations), r.unknownDestinations...)
}

targets := makeTargets(len(r.columns))
targets, err := r.createTargets()
if err != nil {
return err
}

err = r.r.Scan(targets...)
if err != nil {
return err
}

r.scanDestinations = make([]reflect.Value, len(r.columns))
return nil
}

func (r *Row) createTargets() ([]any, error) {
targets := make([]any, len(r.columns))

for i, name := range r.columns {
dest := r.scanDestinations[i]
Expand All @@ -76,26 +91,14 @@ func (r *Row) scanCurrentRow() error {

if !r.allowUnknown {
err := fmt.Errorf("No destination for column %s", name)
return createError(err, "no destination", name)
return nil, createError(err, "no destination", name)
}
}

err := r.r.Scan(targets...)
if err != nil {
return err
}

r.scanDestinations = make([]reflect.Value, len(r.columns))
return nil
}

// See https://github.com/golang/go/issues/41607:
// Some drivers cannot work with nil values, so valid pointers should be
// used for all column targets, even if they are discarded afterwards.
func makeTargets(length int) []any {
targets := make([]any, length)
for i := range targets {
// See https://github.com/golang/go/issues/41607:
// Some drivers cannot work with nil values, so valid pointers should be
// used for all column targets, even if they are discarded afterwards.
targets[i] = new(interface{})
}
return targets

return targets, nil
}

0 comments on commit d4965de

Please sign in to comment.