Fix: Prepared statements now support NULL arguments

pgx-vs-pq
Jack Christensen 2014-04-02 15:52:33 -04:00
parent d4c2a2f18b
commit 9e5a3ef0d2
2 changed files with 32 additions and 4 deletions

View File

@ -622,11 +622,15 @@ func (c *Connection) sendPreparedQuery(ps *preparedStatement, arguments ...inter
w.Write(int16(len(arguments)))
for i, oid := range ps.ParameterOids {
transcoder := ValueTranscoders[oid]
if transcoder == nil {
transcoder = defaultTranscoder
if arguments[i] != nil {
transcoder := ValueTranscoders[oid]
if transcoder == nil {
transcoder = defaultTranscoder
}
transcoder.EncodeTo(w, arguments[i])
} else {
w.Write(int32(-1))
}
transcoder.EncodeTo(w, arguments[i])
}
w.Write(int16(len(ps.FieldDescriptions)))

View File

@ -5,6 +5,30 @@ import (
"time"
)
func TestNilTranscode(t *testing.T) {
conn := getSharedConnection(t)
var inputNil interface{}
inputNil = nil
result := mustSelectValue(t, conn, "select $1::integer", inputNil)
if result != nil {
t.Errorf("Did not transcode nil successfully for normal query: %v", result)
}
mustPrepare(t, conn, "testTranscode", "select $1::integer")
defer func() {
if err := conn.Deallocate("testTranscode"); err != nil {
t.Fatalf("Unable to deallocate prepared statement: %v", err)
}
}()
result = mustSelectValue(t, conn, "testTranscode", inputNil)
if result != nil {
t.Errorf("Did not transcode nil successfully for prepared query: %v", result)
}
}
func TestDateTranscode(t *testing.T) {
conn := getSharedConnection(t)