Skip to content

Commit

Permalink
Merge branch 'slice-map-fix' into breaking-api-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoiron committed May 3, 2014
2 parents 213d8de + b1267aa commit f829e0f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Major additional concepts are:
* Common error handling mnemonics (eg. `Execf`, `Execp` (`MustExec`), and `Execl`)
* `LoadFile` for executing statements from a file

Read the usage below to see how sqlx might help you, or check out the [API
There is now some [fairly comprehensive documentation](http://jmoiron.github.io/sqlx/) for sqlx.
You can also read the usage below for a quick sample on how sqlx works, or check out the [API
documentation on godoc](http://godoc.org/github.com/jmoiron/sqlx).

## Important API Stability Note
Expand Down
24 changes: 10 additions & 14 deletions sqlx.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,9 @@ func (r *Row) StructScan(dest interface{}) error {
if v.Kind() != reflect.Ptr {
return errors.New("must pass a pointer, not a value, to StructScan destination")
}
if v.IsNil() {
return errors.New("nil pointer passed to StructScan destination")
}

direct := reflect.Indirect(v)
base, err := BaseStructType(direct.Type())
Expand Down Expand Up @@ -672,7 +675,7 @@ func SliceScan(r ColScanner) ([]interface{}, error) {

values := make([]interface{}, len(columns))
for i := range values {
values[i] = &sql.NullString{}
values[i] = new(interface{})
}

err = r.Scan(values...)
Expand All @@ -682,12 +685,7 @@ func SliceScan(r ColScanner) ([]interface{}, error) {
}

for i := range columns {
ns := *(values[i].(*sql.NullString))
if ns.Valid {
values[i] = ns.String
} else {
values[i] = nil
}
values[i] = *(values[i].(*interface{}))
}

return values, r.Err()
Expand All @@ -712,7 +710,7 @@ func MapScan(r ColScanner, dest map[string]interface{}) error {

values := make([]interface{}, len(columns))
for i := range values {
values[i] = &sql.NullString{}
values[i] = new(interface{})
}

err = r.Scan(values...)
Expand All @@ -721,12 +719,7 @@ func MapScan(r ColScanner, dest map[string]interface{}) error {
}

for i, column := range columns {
ns := *(values[i].(*sql.NullString))
if ns.Valid {
dest[column] = ns.String
} else {
dest[column] = nil
}
dest[column] = *(values[i].(*interface{}))
}

return r.Err()
Expand All @@ -751,6 +744,9 @@ func StructScan(rows rowsi, dest interface{}) error {
if value.Kind() != reflect.Ptr {
return errors.New("must pass a pointer, not a value, to StructScan destination")
}
if value.IsNil() {
return errors.New("nil pointer passed to StructScan destination")
}

direct := reflect.Indirect(value)

Expand Down
45 changes: 45 additions & 0 deletions sqlx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,51 @@ func TestEmbeddedStructs(t *testing.T) {
})
}

func TestSelectSliceMapTime(t *testing.T) {
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T) {
loadDefaultFixture(db, t)
rows, err := db.Queryx("SELECT * FROM person")
if err != nil {
t.Fatal(err)
}
for rows.Next() {
_, err := rows.SliceScan()
if err != nil {
t.Error(err)
}
}

rows, err = db.Queryx("SELECT * FROM person")
if err != nil {
t.Fatal(err)
}
for rows.Next() {
m := map[string]interface{}{}
err := rows.MapScan(m)
if err != nil {
t.Error(err)
}
}

})
}

func TestNilReceiver(t *testing.T) {
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T) {
loadDefaultFixture(db, t)
var p *Person
err := db.Get(p, "SELECT * FROM person LIMIT 1")
if err == nil {
t.Error("Expected error when getting into nil struct ptr.")
}
var pp *[]Person
err = db.Select(pp, "SELECT * FROM person")
if err == nil {
t.Error("Expected an error when selecting into nil slice ptr.")
}
})
}

func TestNamedQuery(t *testing.T) {
var schema = Schema{
create: `
Expand Down

0 comments on commit f829e0f

Please sign in to comment.