Skip to content

Commit

Permalink
Merge pull request #5 from popescu-af/4/pass_allow_unknown_flag
Browse files Browse the repository at this point in the history
Pass allow unknown flag to row scanner.
  • Loading branch information
stephenafamo committed Jul 18, 2023
2 parents 6903067 + dbc972b commit 1c204ee
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
33 changes: 32 additions & 1 deletion exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func createQuery(tb testing.TB, cols []string) string {
}

type queryCase[T any] struct {
ctx context.Context
columns strstr
rows rows
query []string // columns to select
Expand All @@ -76,7 +77,10 @@ func testQuery[T any](t *testing.T, name string, tc queryCase[T]) {
t.Helper()

t.Run(name, func(t *testing.T) {
ctx := context.Background()
ctx := tc.ctx
if ctx == nil {
ctx = context.Background()
}

ex, clean := createDB(t, tc.columns)
defer clean()
Expand Down Expand Up @@ -283,3 +287,30 @@ func TestStruct(t *testing.T) {
},
})
}

func TestAllowUnknownColumns(t *testing.T) {
type testStruct struct {
ID int64
Int int64
}

// fails when context does not have CtxKeyAllowUnknownColumns set to true
testQuery(t, "unknowncolumnsnotallowed", queryCase[testStruct]{
columns: strstr{{"id", "int64"}, {"ignored_int", "int64"}, {"int", "int64"}},
rows: rows{{1, 10, 1}, {2, 20, 2}},
query: []string{"id", "ignored_int", "int"},
mapper: StructMapper[testStruct](),
expectedErr: createError(fmt.Errorf("No destination for column ignored_int"), "no destination", "ignored_int"),
})

// succeeds when context has CtxKeyAllowUnknownColumns set to true
testQuery(t, "unknowncolumnsallowed", queryCase[testStruct]{
ctx: context.WithValue(context.Background(), CtxKeyAllowUnknownColumns, true),
columns: strstr{{"id", "int64"}, {"ignored_int", "int64"}, {"int", "int64"}},
rows: rows{{1, 10, 1}, {2, 20, 2}},
query: []string{"id", "ignored_int", "int"},
mapper: StructMapper[testStruct](),
expectOne: testStruct{ID: 1, Int: 1},
expectAll: []testStruct{{ID: 1, Int: 1}, {ID: 2, Int: 2}},
})
}
29 changes: 22 additions & 7 deletions row.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func wrapRows(r Rows, allowUnknown bool) (*Row, error) {
r: r,
columns: cols,
scanDestinations: make([]reflect.Value, len(cols)),
allowUnknown: allowUnknown,
}, nil
}

Expand Down Expand Up @@ -64,6 +65,21 @@ func (r *Row) scanCurrentRow() error {
return createError(fmt.Errorf("unknown columns to map to: %v", r.unknownDestinations), r.unknownDestinations...)
}

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 {
Expand All @@ -75,15 +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
// 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{})
}

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

0 comments on commit 1c204ee

Please sign in to comment.