Skip to content

Commit

Permalink
go-gorp#150 - modify selectVal() to return sql.ErrNoRows, and modify …
Browse files Browse the repository at this point in the history
…Select* funcs to ignore this error and continue to return zeroVal (per docs). This fixes SelectOne() for pointers to primitive values.
  • Loading branch information
James Cooper authored and JonPulfer committed Mar 31, 2014
1 parent 11120f2 commit ca63ac2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
21 changes: 9 additions & 12 deletions gorp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}

///////////////
Expand Down
15 changes: 14 additions & 1 deletion gorp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}

Expand Down

0 comments on commit ca63ac2

Please sign in to comment.