From 4f0658d52bee135450f0e0fadb9701f57972e4b9 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Tue, 1 Jan 2019 13:16:50 -0600 Subject: [PATCH] Extract bufferLastResult Buffered exec methods need to read until pending ready for queries is 0. Factor this common logic out. Add stress test for PgConn. --- pgconn/pgconn.go | 54 ++++++------------------------------------------ 1 file changed, 6 insertions(+), 48 deletions(-) diff --git a/pgconn/pgconn.go b/pgconn/pgconn.go index 9aeba757..ec5413de 100644 --- a/pgconn/pgconn.go +++ b/pgconn/pgconn.go @@ -799,6 +799,10 @@ func (pgConn *PgConn) Exec(ctx context.Context, sql string) (*PgResult, error) { return nil, err } + return pgConn.bufferLastResult(ctx) +} + +func (pgConn *PgConn) bufferLastResult(ctx context.Context) (*PgResult, error) { var result *PgResult for resultReader := pgConn.GetResult(ctx); resultReader != nil; resultReader = pgConn.GetResult(ctx) { @@ -844,30 +848,7 @@ func (pgConn *PgConn) ExecParams(ctx context.Context, sql string, paramValues [] return nil, err } - resultReader := pgConn.GetResult(ctx) - if resultReader == nil { - return nil, errors.New("unexpected missing result") - } - - var result *PgResult - rows := [][][]byte{} - for resultReader.NextRow() { - row := make([][]byte, len(resultReader.Values())) - copy(row, resultReader.Values()) - rows = append(rows, row) - } - - commandTag, err := resultReader.Close() - if err != nil { - return nil, err - } - - result = &PgResult{ - Rows: rows, - CommandTag: commandTag, - } - - return result, nil + return pgConn.bufferLastResult(ctx) } // ExecPrepared executes a prepared statement via the PostgreSQL extended query protocol, buffers the entire result, and @@ -888,30 +869,7 @@ func (pgConn *PgConn) ExecPrepared(ctx context.Context, stmtName string, paramVa return nil, err } - resultReader := pgConn.GetResult(ctx) - if resultReader == nil { - return nil, errors.New("unexpected missing result") - } - - var result *PgResult - rows := [][][]byte{} - for resultReader.NextRow() { - row := make([][]byte, len(resultReader.Values())) - copy(row, resultReader.Values()) - rows = append(rows, row) - } - - commandTag, err := resultReader.Close() - if err != nil { - return nil, err - } - - result = &PgResult{ - Rows: rows, - CommandTag: commandTag, - } - - return result, nil + return pgConn.bufferLastResult(ctx) } // Prepare creates a prepared statement.