mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
database/sql: add method Err on sql.Row
The Row.Err method is intended to assist wrapping sql.DB. Because sql.Row is a struct with private fields, a wrapper in an existing code base cannot easily provide users with a different implementation without large rewrites. Adding this method allows query level errors to be handled centrally. Fixes #35804 Change-Id: I94e6329de89a7ee1284ce9ef76af4363d2d081f9 Reviewed-on: https://go-review.googlesource.com/c/go/+/214317 Reviewed-by: Daniel Theophanes <kardianos@gmail.com> Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
b3b174ffcf
commit
f9f57c4443
2 changed files with 26 additions and 0 deletions
|
|
@ -3174,6 +3174,14 @@ func (r *Row) Scan(dest ...interface{}) error {
|
||||||
return r.rows.Close()
|
return r.rows.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Err provides a way for wrapping packages to check for
|
||||||
|
// query errors without calling Scan.
|
||||||
|
// Err returns the error, if any, that was encountered while running the query.
|
||||||
|
// If this error is not nil, this error will also be returned from Scan.
|
||||||
|
func (r *Row) Err() error {
|
||||||
|
return r.err
|
||||||
|
}
|
||||||
|
|
||||||
// A Result summarizes an executed SQL command.
|
// A Result summarizes an executed SQL command.
|
||||||
type Result interface {
|
type Result interface {
|
||||||
// LastInsertId returns the integer generated by the database
|
// LastInsertId returns the integer generated by the database
|
||||||
|
|
|
||||||
|
|
@ -788,6 +788,24 @@ func TestQueryRow(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRowErr(t *testing.T) {
|
||||||
|
db := newTestDB(t, "people")
|
||||||
|
|
||||||
|
err := db.QueryRowContext(context.Background(), "SELECT|people|bdate|age=?", 3).Err()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected err = %v; want %v", err, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
err = db.QueryRowContext(ctx, "SELECT|people|bdate|age=?", 3).Err()
|
||||||
|
exp := "context canceled"
|
||||||
|
if err == nil || !strings.Contains(err.Error(), exp) {
|
||||||
|
t.Errorf("Expected err = %v; got %v", exp, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTxRollbackCommitErr(t *testing.T) {
|
func TestTxRollbackCommitErr(t *testing.T) {
|
||||||
db := newTestDB(t, "people")
|
db := newTestDB(t, "people")
|
||||||
defer closeDB(t, db)
|
defer closeDB(t, db)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue