1
0
mirror of https://github.com/golang/go synced 2024-11-23 05:40:04 -07:00

database/sql: fix close rows error ignored in Next

Change-Id: I19f0d764e2a6122307f3f26a6dd3be7b1155c73b
GitHub-Last-Rev: 9f1f883c45
GitHub-Pull-Request: golang/go#52756
Reviewed-on: https://go-review.googlesource.com/c/go/+/404794
Reviewed-by: Daniel Theophanes <kardianos@gmail.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Run-TryBot: Daniel Theophanes <kardianos@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Jinzhu 2022-05-19 02:44:00 +00:00 committed by Daniel Theophanes
parent 97ce98ac20
commit 29057b707d
3 changed files with 39 additions and 1 deletions

View File

@ -1089,6 +1089,9 @@ type rowsCursor struct {
// This is separate from the fakeConn.line to allow for drivers that // This is separate from the fakeConn.line to allow for drivers that
// can start multiple queries on the same transaction at the same time. // can start multiple queries on the same transaction at the same time.
line int64 line int64
// closeErr is returned when rowsCursor.Close
closeErr error
} }
func (rc *rowsCursor) touchMem() { func (rc *rowsCursor) touchMem() {
@ -1100,7 +1103,7 @@ func (rc *rowsCursor) Close() error {
rc.touchMem() rc.touchMem()
rc.parentMem.touchMem() rc.parentMem.touchMem()
rc.closed = true rc.closed = true
return nil return rc.closeErr
} }
func (rc *rowsCursor) Columns() []string { func (rc *rowsCursor) Columns() []string {

View File

@ -3331,6 +3331,8 @@ func (rs *Rows) close(err error) error {
rs.closeStmt.Close() rs.closeStmt.Close()
} }
rs.releaseConn(err) rs.releaseConn(err)
rs.lasterr = rs.lasterrOrErrLocked(err)
return err return err
} }

View File

@ -2636,6 +2636,39 @@ func TestRowsImplicitClose(t *testing.T) {
} }
} }
func TestRowsCloseError(t *testing.T) {
db := newTestDB(t, "people")
defer db.Close()
rows, err := db.Query("SELECT|people|age,name|")
if err != nil {
t.Fatalf("Query: %v", err)
}
type row struct {
age int
name string
}
got := []row{}
rc, ok := rows.rowsi.(*rowsCursor)
if !ok {
t.Fatal("not using *rowsCursor")
}
rc.closeErr = errors.New("rowsCursor: failed to close")
for rows.Next() {
var r row
err = rows.Scan(&r.age, &r.name)
if err != nil {
t.Fatalf("Scan: %v", err)
}
got = append(got, r)
}
err = rows.Err()
if err != rc.closeErr {
t.Fatalf("unexpected err: got %v, want %v", err, rc.closeErr)
}
}
func TestStmtCloseOrder(t *testing.T) { func TestStmtCloseOrder(t *testing.T) {
db := newTestDB(t, "people") db := newTestDB(t, "people")
defer closeDB(t, db) defer closeDB(t, db)