Skip to content

Commit db2a63d

Browse files
committed
Function to bind an [optional] row.
1 parent 2a93c2c commit db2a63d

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

sqldb.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ type DBTx interface {
2525
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
2626
}
2727

28+
// BindOptionalRow receipts the *sql.Row, calls the binder (which probably calls
29+
// the row.Scan() method), and masks the result of sql.ErrNoRows.
30+
func BindOptionalRow(row *sql.Row, binder Binder) error {
31+
err := binder(row)
32+
if errors.Is(err, sql.ErrNoRows) {
33+
return nil
34+
}
35+
return NormalizeErr(err)
36+
}
37+
2838
// BindAll receives the *sql.Rows + error from the QueryContext method of either
2939
// a *sql.DB, a *sql.Tx, or a *sql.Stmt, as well as a binder callback, to be called
3040
// for each record, which gives the caller the opportunity to scan and aggregate values.

0 commit comments

Comments
 (0)