Fix Execute SQL larger than sharedBufferSize

This commit is contained in:
Jack Christensen 2013-07-08 14:32:28 -05:00
parent e99a9b2306
commit fe2f62f034
2 changed files with 12 additions and 3 deletions

View File

@ -76,7 +76,7 @@ func Connect(parameters ConnectionParameters) (c *Connection, err error) {
}
}
c.buf = bytes.NewBuffer(make([]byte, sharedBufferSize))
c.buf = bytes.NewBuffer(make([]byte, 0, sharedBufferSize))
c.runtimeParams = make(map[string]string)
c.preparedStatements = make(map[string]*PreparedStatement)
@ -402,7 +402,6 @@ func (c *Connection) Execute(sql string, arguments ...interface{}) (commandTag s
var t byte
var r *MessageReader
if t, r, err = c.rxMsg(); err == nil {
// fmt.Printf("Execute received: %c\n", t)
switch t {
case readyForQuery:
return
@ -611,7 +610,7 @@ func (c *Connection) txPasswordMessage(password string) (err error) {
func (c *Connection) getBuf() *bytes.Buffer {
c.buf.Reset()
if cap(c.buf.Bytes()) > sharedBufferSize {
c.buf = bytes.NewBuffer(make([]byte, sharedBufferSize))
c.buf = bytes.NewBuffer(make([]byte, 0, sharedBufferSize))
}
return c.buf
}

View File

@ -2,6 +2,7 @@ package pgx
import (
"bytes"
"strings"
"testing"
)
@ -170,6 +171,15 @@ func TestExecute(t *testing.T) {
t.Error("Unexpected results from Execute")
}
// Can execute longer SQL strings than sharedBufferSize
results, err = conn.Execute(strings.Repeat("select 42; ", 1000))
if err != nil {
t.Fatal("Execute failed: " + err.Error())
}
if results != "SELECT 1" {
t.Errorf("Unexpected results from Execute: %v", results)
}
}
func TestSelectFunc(t *testing.T) {