Return a verifiable error if no results were returned
All checks were successful
Build / build (push) Successful in 3m9s
All checks were successful
Build / build (push) Successful in 3m9s
This commit is contained in:
parent
12a87a8762
commit
dd6be6b9b6
5
errors.go
Normal file
5
errors.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package mysqlite
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var ErrNoRows = errors.New("mysqlite: no rows returned")
|
2
query.go
2
query.go
@ -116,7 +116,7 @@ func (q *Query) ScanSingle(results ...any) (rerr error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !hasResult {
|
if !hasResult {
|
||||||
return fmt.Errorf("did not return any rows")
|
return ErrNoRows
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scan its columns
|
// Scan its columns
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package mysqlite
|
package mysqlite
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -19,6 +20,14 @@ func TestSimpleQuery(t *testing.T) {
|
|||||||
require.Equal(t, 1, count, "expected empty count")
|
require.Equal(t, 1, count, "expected empty count")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSimpleQueryWithNoResults(t *testing.T) {
|
||||||
|
db := openTestDb(t)
|
||||||
|
var count int
|
||||||
|
err := db.Query("select 1 from mytable where key=999").ScanSingle(&count)
|
||||||
|
require.Equal(t, ErrNoRows, err)
|
||||||
|
require.True(t, errors.Is(err, ErrNoRows))
|
||||||
|
}
|
||||||
|
|
||||||
func TestSimpleQueryWithArgs(t *testing.T) {
|
func TestSimpleQueryWithArgs(t *testing.T) {
|
||||||
db := openTestDb(t)
|
db := openTestDb(t)
|
||||||
var value string
|
var value string
|
||||||
@ -102,7 +111,7 @@ func TestQueryWithPointerStringArguments(t *testing.T) {
|
|||||||
|
|
||||||
func TestQueryWithPointerStringArgumentsCanSetToNull(t *testing.T) {
|
func TestQueryWithPointerStringArgumentsCanSetToNull(t *testing.T) {
|
||||||
db := openTestDb(t)
|
db := openTestDb(t)
|
||||||
db.Query("update mytable set value=NULL where key = 'foo'").MustExec()
|
db.Query("update mytable set value=null where key = 'foo'").MustExec()
|
||||||
myString := "some string"
|
myString := "some string"
|
||||||
var result *string
|
var result *string
|
||||||
result = &myString
|
result = &myString
|
||||||
|
Loading…
x
Reference in New Issue
Block a user