diff --git a/connection.go b/connection.go index e0e01ee2..c0faa067 100644 --- a/connection.go +++ b/connection.go @@ -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))) diff --git a/value_transcoder_test.go b/value_transcoder_test.go index cce7cd62..9f9d6be5 100644 --- a/value_transcoder_test.go +++ b/value_transcoder_test.go @@ -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)