Skip to content

Commit

Permalink
Batch: Fix for nil values (no type)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgulacsi committed Sep 30, 2024
1 parent 5137822 commit 3e2a435
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions batch.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017, 2022 The Godror Authors
// Copyright 2017, 2024 The Godror Authors
//
//
// SPDX-License-Identifier: UPL-1.0 OR Apache-2.0
Expand Down Expand Up @@ -34,9 +34,6 @@ func (b *Batch) Add(ctx context.Context, values ...interface{}) error {
b.Limit = DefaultBatchLimit
}
b.rValues = make([]reflect.Value, len(values))
for i := range values {
b.rValues[i] = reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(values[i])), 0, b.Limit)
}
}
func() {
var i int
Expand All @@ -47,7 +44,18 @@ func (b *Batch) Add(ctx context.Context, values ...interface{}) error {
}
}()
for i, v = range values {
if !b.rValues[i].IsValid() {
if v == nil { // a nil value has no type
continue
}
b.rValues[i] = reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(v)), b.size, b.Limit)
}
if v == nil { // assume it has the same type as the other elements
b.rValues[i] = reflect.Append(b.rValues[i], reflect.Zero(b.rValues[i].Type().Elem()))
continue
}
rv := reflect.ValueOf(v)

// type mismatch
if rv.Type().Kind() != reflect.String &&
b.rValues[i].Type().Elem().Kind() == reflect.String {
Expand Down Expand Up @@ -86,13 +94,19 @@ func (b *Batch) Flush(ctx context.Context) error {
b.values = make([]interface{}, len(b.rValues))
}
for i, v := range b.rValues {
b.values[i] = v.Interface()
if !v.IsValid() {
b.values[i] = make([]string, b.size) // empty string == NULL
} else {
b.values[i] = v.Interface()
}
}
if _, err := b.Stmt.ExecContext(ctx, b.values...); err != nil {
return err
}
for i, v := range b.rValues {
b.rValues[i] = v.Slice(0, 0)
if v.IsValid() {
b.rValues[i] = v.Slice(0, 0)
}
}
b.size = 0
return nil
Expand Down

0 comments on commit 3e2a435

Please sign in to comment.