From ca63ac22f5b48be1c8a63cbf981b2200673177d0 Mon Sep 17 00:00:00 2001 From: James Cooper Date: Sun, 23 Mar 2014 07:53:33 -0700 Subject: [PATCH] #150 - modify selectVal() to return sql.ErrNoRows, and modify Select* funcs to ignore this error and continue to return zeroVal (per docs). This fixes SelectOne() for pointers to primitive values. --- gorp.go | 21 +++++++++------------ gorp_test.go | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/gorp.go b/gorp.go index 7e447082..f16b032d 100644 --- a/gorp.go +++ b/gorp.go @@ -1227,7 +1227,7 @@ func (t *Transaction) query(query string, args ...interface{}) (*sql.Rows, error func SelectInt(e SqlExecutor, query string, args ...interface{}) (int64, error) { var h int64 err := selectVal(e, &h, query, args...) - if err != nil { + if err != nil && err != sql.ErrNoRows { return 0, err } return h, nil @@ -1239,7 +1239,7 @@ func SelectInt(e SqlExecutor, query string, args ...interface{}) (int64, error) func SelectNullInt(e SqlExecutor, query string, args ...interface{}) (sql.NullInt64, error) { var h sql.NullInt64 err := selectVal(e, &h, query, args...) - if err != nil { + if err != nil && err != sql.ErrNoRows { return h, err } return h, nil @@ -1251,7 +1251,7 @@ func SelectNullInt(e SqlExecutor, query string, args ...interface{}) (sql.NullIn func SelectFloat(e SqlExecutor, query string, args ...interface{}) (float64, error) { var h float64 err := selectVal(e, &h, query, args...) - if err != nil { + if err != nil && err != sql.ErrNoRows { return 0, err } return h, nil @@ -1263,7 +1263,7 @@ func SelectFloat(e SqlExecutor, query string, args ...interface{}) (float64, err func SelectNullFloat(e SqlExecutor, query string, args ...interface{}) (sql.NullFloat64, error) { var h sql.NullFloat64 err := selectVal(e, &h, query, args...) - if err != nil { + if err != nil && err != sql.ErrNoRows { return h, err } return h, nil @@ -1275,7 +1275,7 @@ func SelectNullFloat(e SqlExecutor, query string, args ...interface{}) (sql.Null func SelectStr(e SqlExecutor, query string, args ...interface{}) (string, error) { var h string err := selectVal(e, &h, query, args...) - if err != nil { + if err != nil && err != sql.ErrNoRows { return "", err } return h, nil @@ -1288,7 +1288,7 @@ func SelectStr(e SqlExecutor, query string, args ...interface{}) (string, error) func SelectNullStr(e SqlExecutor, query string, args ...interface{}) (sql.NullString, error) { var h sql.NullString err := selectVal(e, &h, query, args...) - if err != nil { + if err != nil && err != sql.ErrNoRows { return h, err } return h, nil @@ -1352,14 +1352,11 @@ func selectVal(e SqlExecutor, holder interface{}, query string, args ...interfac } defer rows.Close() - if rows.Next() { - err = rows.Scan(holder) - if err != nil { - return err - } + if !rows.Next() { + return sql.ErrNoRows } - return nil + return rows.Scan(holder) } /////////////// diff --git a/gorp_test.go b/gorp_test.go index 377b15ef..08a9ba7a 100644 --- a/gorp_test.go +++ b/gorp_test.go @@ -1408,7 +1408,20 @@ func TestSelectSingleVal(t *testing.T) { _insert(dbmap, &Person{0, 0, 0, "bob", "smith", 0}) err = dbmap.SelectOne(&p2, "select * from person_test where Fname='bob'") if err == nil { - t.Error("Expected nil when two rows found") + t.Error("Expected error when two rows found") + } + + // tests for #150 + var tInt int64 + var tStr string + var tBool bool + var tFloat float64 + primVals := []interface{}{tInt, tStr, tBool, tFloat} + for _, prim := range primVals { + err = dbmap.SelectOne(&prim, "select * from person_test where Id=-123") + if err == nil || err != sql.ErrNoRows { + t.Error("primVals: SelectOne should have returned sql.ErrNoRows") + } } }