From 52c26ba14c333118a001155eb43310683221fecb Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Mon, 1 Jul 2013 16:50:16 -0500 Subject: [PATCH] Treat unknown oid's as text type --- connection.go | 5 ++++- connection_test.go | 3 +++ value_transcoder.go | 4 ++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/connection.go b/connection.go index 9979d7ed..8c2807e9 100644 --- a/connection.go +++ b/connection.go @@ -352,7 +352,7 @@ func (c *Connection) sendPreparedQuery(ps *PreparedStatement, arguments ...inter for _, oid := range ps.ParameterOids { transcoder := valueTranscoders[oid] if transcoder == nil { - panic(fmt.Sprintf("can't encode %#v", oid)) + transcoder = defaultTranscoder } binary.Write(buf, binary.BigEndian, transcoder.EncodeFormat) } @@ -360,6 +360,9 @@ func (c *Connection) sendPreparedQuery(ps *PreparedStatement, arguments ...inter binary.Write(buf, binary.BigEndian, int16(len(arguments))) for i, oid := range ps.ParameterOids { transcoder := valueTranscoders[oid] + if transcoder == nil { + transcoder = defaultTranscoder + } transcoder.EncodeTo(buf, arguments[i]) } binary.Write(buf, binary.BigEndian, int16(0)) diff --git a/connection_test.go b/connection_test.go index d86f9bdd..bf6ab11c 100644 --- a/connection_test.go +++ b/connection_test.go @@ -326,6 +326,9 @@ func TestPrepare(t *testing.T) { testTranscode("select $1::float8", float64(1.23)) testTranscode("select $1::boolean", true) + // Ensure that unknown types are just treated as strings + testTranscode("select $1::point", "(0,0)") + // case []byte: // s = `E'\\x` + hex.EncodeToString(arg) + `'` diff --git a/value_transcoder.go b/value_transcoder.go index 94bd413b..45dfa8f9 100644 --- a/value_transcoder.go +++ b/value_transcoder.go @@ -15,6 +15,7 @@ type valueTranscoder struct { } var valueTranscoders map[oid]*valueTranscoder +var defaultTranscoder *valueTranscoder func init() { valueTranscoders = make(map[oid]*valueTranscoder) @@ -56,6 +57,9 @@ func init() { // varchar -- same as text valueTranscoders[oid(1043)] = valueTranscoders[oid(25)] + + // use text transcoder for anything we don't understand + defaultTranscoder = valueTranscoders[oid(25)] } func decodeBoolFromText(mr *MessageReader, size int32) interface{} {