Skip to content

Commit

Permalink
fix: make Rows.Close to drain messages
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Jun 10, 2023
1 parent dacffd2 commit 5ceba07
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
6 changes: 4 additions & 2 deletions driver/pgdriver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ func (r *rows) Close() error {

for {
switch err := r.Next(nil); err {
case nil, io.EOF:
case nil:
// keep going
case io.EOF:
return nil
default: // unexpected error
_ = r.cn.Close()
Expand Down Expand Up @@ -473,7 +475,7 @@ func (r *rows) readDataRow(rd *reader, dest []driver.Value) error {
return err
}

if len(dest) != int(numCol) {
if dest != nil && len(dest) != int(numCol) {
return fmt.Errorf("pgdriver: query returned %d columns, but Scan dest has %d items",
numCol, len(dest))
}
Expand Down
14 changes: 13 additions & 1 deletion driver/pgdriver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,18 @@ func TestStatementTimeout(t *testing.T) {
require.True(t, pgerr.StatementTimeout())
}

func TestPartialScan(t *testing.T) {
db := sqlDB()
defer db.Close()

for i := 0; i < 10; i++ {
var num int
err := db.QueryRow("select generate_series(0, 10)").Scan(&num)
require.NoError(t, err)
require.Equal(t, 0, num)
}
}

func sqlDB() *sql.DB {
db, err := sql.Open("pg", dsn())
if err != nil {
Expand All @@ -287,7 +299,7 @@ func sqlDB() *sql.DB {
func dsn() string {
dsn := os.Getenv("PG")
if dsn == "" {
dsn = "postgres://postgres:@localhost:5432/test?sslmode=disable"
dsn = "postgres://postgres:postgres@localhost:5432/test?sslmode=disable"
}
return dsn
}
1 change: 1 addition & 0 deletions driver/pgdriver/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
)

// https://www.postgresql.org/docs/current/protocol-message-formats.html
//
//nolint:deadcode,varcheck,unused
const (
commandCompleteMsg = 'C'
Expand Down

0 comments on commit 5ceba07

Please sign in to comment.