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

Fixed issues with TVPs #622

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion tvp_go19.go
Expand Up @@ -54,7 +54,7 @@ func (tvp TVP) check() error {
if valueOf.IsNil() {
return ErrorTypeSliceIsEmpty
}
if reflect.TypeOf(tvp.Value).Elem().Kind() != reflect.Struct {
if elem := reflect.TypeOf(tvp.Value).Elem(); (elem.Kind() == reflect.Ptr && elem.Elem().Kind() != reflect.Struct) && (elem.Kind() != reflect.Struct) {
return ErrorTypeSlice
}
return nil
Expand Down Expand Up @@ -153,6 +153,9 @@ func (tvp TVP) columnTypes() ([]columnStruct, []int, error) {
if IsSkipField(tvpTagValue, isTvpTag, jsonTagValue, isJsonTag) {
continue
}
if field.PkgPath == "" {
continue
}
tvpFieldIndexes = append(tvpFieldIndexes, i)
if field.Type.Kind() == reflect.Ptr {
v := reflect.New(field.Type.Elem())
Expand Down
38 changes: 35 additions & 3 deletions tvp_go19_db_test.go
Expand Up @@ -340,6 +340,33 @@ func TestTVPGoSQLTypes(t *testing.T) {
},
}

param2 := []*TvpGoSQLTypes{
{
PBool: sql.NullBool{
Bool: true,
Valid: true,
},
PBoolNull: sql.NullBool{},
PFloat64: sql.NullFloat64{
Float64: 14.33,
Valid: true,
},
PFloat64Null: sql.NullFloat64{},
PInt64: sql.NullInt64{
Int64: 777,
Valid: true,
},
PInt64Null: sql.NullInt64{},
PString: sql.NullString{
String: "test=tvp",
Valid: true,
},
PStringNull: sql.NullString{},
},
}

testResult := param1[:]
testResult = append(testResult, param1...)
tvpType := TVP{
TypeName: "tvpGoSQLTypes",
Value: param1,
Expand All @@ -348,12 +375,17 @@ func TestTVPGoSQLTypes(t *testing.T) {
TypeName: "tvpGoSQLTypes",
Value: []TvpGoSQLTypes{},
}
tvpPointerType := TVP{
TypeName: "tvpGoSQLTypes",
Value: param2,
}

rows, err := db.QueryContext(ctx,
"exec spwithtvpGoSQLTypes @param1, @param2, @param3",
"exec spwithtvpGoSQLTypes @param1, @param2, @param3, @param4",
sql.Named("param1", tvpType),
sql.Named("param2", tvpTypeEmpty),
sql.Named("param3", "test"),
sql.Named("param4", tvpPointerType),
)

if err != nil {
Expand All @@ -380,8 +412,8 @@ func TestTVPGoSQLTypes(t *testing.T) {
result1 = append(result1, val)
}

if !reflect.DeepEqual(param1, result1) {
t.Logf("expected: %+v", param1)
if !reflect.DeepEqual(testResult, result1) {
t.Logf("expected: %+v", testResult)
t.Logf("actual: %+v", result1)
t.Errorf("first resultset did not match param1")
}
Expand Down
62 changes: 31 additions & 31 deletions tvp_go19_test.go
Expand Up @@ -182,12 +182,12 @@ func TestTVPType_check(t *testing.T) {
wantErr: true,
},
{
name: "Value isn't right",
name: "Value is pointer",
fields: fields{
TVPName: "Test",
TVPValue: []*fields{},
},
wantErr: true,
wantErr: false,
},
{
name: "Value is right",
Expand Down Expand Up @@ -331,10 +331,10 @@ func BenchmarkColumnTypes(b *testing.B) {

func TestIsSkipField(t *testing.T) {
type args struct {
tvpTagValue string
isTvpValue bool
jsonTagValue string
isJsonTagValue bool
TvpTagValue string
IsTvpValue bool
JsonTagValue string
IsJsonTagValue bool
}
tests := []struct {
name string
Expand All @@ -349,78 +349,78 @@ func TestIsSkipField(t *testing.T) {
name: "tvp is skip",
want: true,
args: args{
isTvpValue: true,
tvpTagValue: skipTagValue,
IsTvpValue: true,
TvpTagValue: skipTagValue,
},
},
{
name: "tvp is any",
want: false,
args: args{
isTvpValue: true,
tvpTagValue: "tvp",
IsTvpValue: true,
TvpTagValue: "tvp",
},
},
{
name: "Json is skip",
want: true,
args: args{
isJsonTagValue: true,
jsonTagValue: skipTagValue,
IsJsonTagValue: true,
JsonTagValue: skipTagValue,
},
},
{
name: "Json is any",
want: false,
args: args{
isJsonTagValue: true,
jsonTagValue: "any",
IsJsonTagValue: true,
JsonTagValue: "any",
},
},
{
name: "Json is skip tvp is skip",
want: true,
args: args{
isJsonTagValue: true,
jsonTagValue: skipTagValue,
isTvpValue: true,
tvpTagValue: skipTagValue,
IsJsonTagValue: true,
JsonTagValue: skipTagValue,
IsTvpValue: true,
TvpTagValue: skipTagValue,
},
},
{
name: "Json is skip tvp is any",
want: false,
args: args{
isJsonTagValue: true,
jsonTagValue: skipTagValue,
isTvpValue: true,
tvpTagValue: "tvp",
IsJsonTagValue: true,
JsonTagValue: skipTagValue,
IsTvpValue: true,
TvpTagValue: "tvp",
},
},
{
name: "Json is any tvp is skip",
want: true,
args: args{
isJsonTagValue: true,
jsonTagValue: "json",
isTvpValue: true,
tvpTagValue: skipTagValue,
IsJsonTagValue: true,
JsonTagValue: "json",
IsTvpValue: true,
TvpTagValue: skipTagValue,
},
},
{
name: "Json is any tvp is skip",
want: false,
args: args{
isJsonTagValue: true,
jsonTagValue: "json",
isTvpValue: true,
tvpTagValue: "tvp",
IsJsonTagValue: true,
JsonTagValue: "json",
IsTvpValue: true,
TvpTagValue: "tvp",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsSkipField(tt.args.tvpTagValue, tt.args.isTvpValue, tt.args.jsonTagValue, tt.args.isJsonTagValue); got != tt.want {
if got := IsSkipField(tt.args.TvpTagValue, tt.args.IsTvpValue, tt.args.JsonTagValue, tt.args.IsJsonTagValue); got != tt.want {
t.Errorf("IsSkipField() = %v, schema %v", got, tt.want)
}
})
Expand Down