Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support nullable types for bulkcopy #192

Merged
merged 7 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions bulkcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mssql
import (
"bytes"
"context"
"database/sql/driver"
"encoding/binary"
"fmt"
"math"
Expand Down Expand Up @@ -323,6 +324,19 @@ func (b *Bulk) makeParam(val DataValue, col columnStruct) (res param, err error)
return
}

switch valuer := val.(type) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switch valuer :=

should this switch be invoked after the switch instead of before it? Is there no implementation of Valuer that's not already handled directly by the switch?

case driver.Valuer:
// If the value has a non-nil value, call MakeParam on its Value
val, e := driver.DefaultParameterConverter.ConvertValue(valuer)
if e != nil {
err = e
return
}
if val != nil {
return b.makeParam(val, col)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else?

}

switch col.ti.TypeId {

case typeInt1, typeInt2, typeInt4, typeInt8, typeIntN:
Expand Down
2 changes: 2 additions & 0 deletions bulkcopy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func TestBulkcopy(t *testing.T) {
{"test_geom", geom, string(geom)},
{"test_uniqueidentifier", uid, string(uid)},
{"test_nulluniqueidentifier", nil, nil},
{"test_nullfloat", sql.NullFloat64{Float64: 64, Valid: true}, 64.0},
// {"test_smallmoney", 1234.56, nil},
// {"test_money", 1234.56, nil},
{"test_decimal_18_0", 1234.0001, "1234"},
Expand Down Expand Up @@ -290,6 +291,7 @@ func setupTable(ctx context.Context, t *testing.T, conn *sql.Conn, tableName str
[test_int16nvarchar] [varchar](4) NULL,
[test_int8nvarchar] [varchar](3) NULL,
[test_intnvarchar] [varchar](4) NULL,
[test_nullfloat] [float] NULL,
CONSTRAINT [PK_` + tableName + `_id] PRIMARY KEY CLUSTERED
(
[id] ASC
Expand Down