database/sql: make Rows.Next returning false always implicitly call

Rows.Close.

Previously, callers that followed the example code (but not call
rows.Close after "for rows.Next() { ... }") could leak statements if
the driver returned an error other than io.EOF.

R=bradfitz, alex.brainman
CC=golang-dev, rsc
https://golang.org/cl/12677050
This commit is contained in:
Nigel Tao 2013-08-16 11:23:35 +10:00
parent b75a08d03c
commit bc21265074
3 changed files with 49 additions and 16 deletions

View file

@ -608,9 +608,10 @@ rows:
}
cursor := &rowsCursor{
pos: -1,
rows: mrows,
cols: s.colName,
pos: -1,
rows: mrows,
cols: s.colName,
errPos: -1,
}
return cursor, nil
}
@ -635,6 +636,10 @@ type rowsCursor struct {
rows []*row
closed bool
// errPos and err are for making Next return early with error.
errPos int
err error
// a clone of slices to give out to clients, indexed by the
// the original slice's first byte address. we clone them
// just so we're able to corrupt them on close.
@ -660,6 +665,9 @@ func (rc *rowsCursor) Next(dest []driver.Value) error {
return errors.New("fakedb: cursor is closed")
}
rc.pos++
if rc.pos == rc.errPos {
return rc.err
}
if rc.pos >= len(rc.rows) {
return io.EOF // per interface spec
}