Skip to content

Commit

Permalink
return the address of non-struct pointer fields rather than the point…
Browse files Browse the repository at this point in the history
…ers; fixes problems scanning into pointer fields in structs
  • Loading branch information
jmoiron committed Jan 5, 2014
1 parent ed0ab52 commit 0d135e4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sqlx.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ func getValues(v reflect.Value) []interface{} {
queue = append(queue, v)
}
} else {
if isPtr || !returnAddrs {
if !returnAddrs {
values[i] = v.Interface()
} else if returnAddrs {
values[i] = v.Addr().Interface()
Expand Down
13 changes: 13 additions & 0 deletions sqlx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ type Place struct {
TelCode int
}

type PlacePtr struct {
Country string
City *string
TelCode int
}

type PersonPlace struct {
Person
Place
Expand Down Expand Up @@ -435,6 +441,13 @@ func TestUsage(t *testing.T) {
t.Errorf("Expected integer telcodes to work, got %#v", places)
}

placesptr := []PlacePtr{}
err = db.Select(&placesptr, "SELECT * FROM place ORDER BY telcode ASC")
if err != nil {
t.Error(err)
}
//fmt.Printf("%#v\n%#v\n%#v\n", placesptr[0], placesptr[1], placesptr[2])

// if you have null fields and use SELECT *, you must use sql.Null* in your struct
// this test also verifies that you can use either a []Struct{} or a []*Struct{}
places2 := []Place{}
Expand Down

0 comments on commit 0d135e4

Please sign in to comment.