QRM returns qrm.ErrNoRows when scanning into struct destination and query result set is empty.

This commit is contained in:
go-jet 2019-10-18 10:15:08 +02:00
parent 0b00a6b12c
commit 15acb1c326
3 changed files with 9 additions and 7 deletions

View file

@ -16,11 +16,11 @@ type Statement interface {
// Query executes statement over database connection db and stores row result in destination. // Query executes statement over database connection db and stores row result in destination.
// Destination can be either pointer to struct or pointer to a slice. // Destination can be either pointer to struct or pointer to a slice.
// If destination is pointer to struct and query result set is empty, method returns sql.ErrNoRows. // If destination is pointer to struct and query result set is empty, method returns qrm.ErrNoRows.
Query(db qrm.DB, destination interface{}) error Query(db qrm.DB, destination interface{}) error
// QueryContext executes statement with a context over database connection db and stores row result in destination. // QueryContext executes statement with a context over database connection db and stores row result in destination.
// Destination can be either pointer to struct or pointer to a slice. // Destination can be either pointer to struct or pointer to a slice.
// If destination is pointer to struct and query result set is empty, method returns sql.ErrNoRows. // If destination is pointer to struct and query result set is empty, method returns qrm.ErrNoRows.
QueryContext(context context.Context, db qrm.DB, destination interface{}) error QueryContext(context context.Context, db qrm.DB, destination interface{}) error
//Exec executes statement over db connection without returning any rows. //Exec executes statement over db connection without returning any rows.

View file

@ -2,15 +2,18 @@ package qrm
import ( import (
"context" "context"
"database/sql" "errors"
"github.com/go-jet/jet/internal/utils" "github.com/go-jet/jet/internal/utils"
"reflect" "reflect"
) )
// ErrNoRows is returned by Query when query result set is empty
var ErrNoRows = errors.New("qrm: no rows in result set")
// Query executes Query Result Mapping (QRM) of `query` with list of parametrized arguments `arg` over database connection `db` // Query executes Query Result Mapping (QRM) of `query` with list of parametrized arguments `arg` over database connection `db`
// using context `ctx` into destination `destPtr`. // using context `ctx` into destination `destPtr`.
// Destination can be either pointer to struct or pointer to slice of structs. // Destination can be either pointer to struct or pointer to slice of structs.
// If destination is pointer to struct and query result set is empty, method returns sql.ErrNoRows. // If destination is pointer to struct and query result set is empty, method returns qrm.ErrNoRows.
func Query(ctx context.Context, db DB, query string, args []interface{}, destPtr interface{}) error { func Query(ctx context.Context, db DB, query string, args []interface{}, destPtr interface{}) error {
utils.MustBeInitializedPtr(db, "jet: db is nil") utils.MustBeInitializedPtr(db, "jet: db is nil")
@ -33,7 +36,7 @@ func Query(ctx context.Context, db DB, query string, args []interface{}, destPtr
} }
if rowsProcessed == 0 { if rowsProcessed == 0 {
return sql.ErrNoRows return ErrNoRows
} }
// edge case when row result set contains only NULLs. // edge case when row result set contains only NULLs.

View file

@ -1,7 +1,6 @@
package postgres package postgres
import ( import (
"database/sql"
"fmt" "fmt"
"github.com/go-jet/jet/internal/testutils" "github.com/go-jet/jet/internal/testutils"
. "github.com/go-jet/jet/postgres" . "github.com/go-jet/jet/postgres"
@ -704,7 +703,7 @@ func TestStructScanErrNoRows(t *testing.T) {
err := query.Query(db, &customer) err := query.Query(db, &customer)
assert.Error(t, err, sql.ErrNoRows.Error()) assert.Error(t, err, qrm.ErrNoRows.Error())
} }
func TestStructScanAllNull(t *testing.T) { func TestStructScanAllNull(t *testing.T) {