diff --git a/conn.go b/conn.go index b5f0dc5d..e41d4869 100644 --- a/conn.go +++ b/conn.go @@ -455,7 +455,7 @@ func (c *Conn) sendPreparedQuery(ps *PreparedStatement, arguments ...interface{} switch arg := arguments[i].(type) { case BinaryEncoder: - err = arg.EncodeBinary(wbuf, &ps.FieldDescriptions[i]) + err = arg.EncodeBinary(wbuf, oid) case TextEncoder: var s string var status byte diff --git a/values.go b/values.go index 1b8d6eab..403e0238 100644 --- a/values.go +++ b/values.go @@ -66,12 +66,12 @@ type TextEncoder interface { type BinaryEncoder interface { // EncodeBinary writes the binary value to w. // - // EncodeBinary MUST check fd.DataType to see if the parameter data type is + // EncodeBinary MUST check oid to see if the parameter data type is // compatible. If this is not done, the PostgreSQL server may detect the // error if the expected data size or format of the encoded data does not // match. But if the encoded data is a valid representation of the data type // PostgreSQL expects such as date and int4, incorrect data may be stored. - EncodeBinary(w *WriteBuf, fd *FieldDescription) error + EncodeBinary(w *WriteBuf, oid Oid) error } // NullFloat32 represents an float4 that may be null. @@ -103,9 +103,9 @@ func (n NullFloat32) EncodeText() (string, byte, error) { } } -func (n NullFloat32) EncodeBinary(w *WriteBuf, fd *FieldDescription) error { - if fd.DataType != Float4Oid { - return SerializationError(fmt.Sprintf("NullFloat32.EncodeBinary cannot encode into OID %d", fd.DataType)) +func (n NullFloat32) EncodeBinary(w *WriteBuf, oid Oid) error { + if oid != Float4Oid { + return SerializationError(fmt.Sprintf("NullFloat32.EncodeBinary cannot encode into OID %d", oid)) } if !n.Valid { @@ -145,9 +145,9 @@ func (n NullFloat64) EncodeText() (string, byte, error) { } } -func (n NullFloat64) EncodeBinary(w *WriteBuf, fd *FieldDescription) error { - if fd.DataType != Float8Oid { - return SerializationError(fmt.Sprintf("NullFloat64.EncodeBinary cannot encode into OID %d", fd.DataType)) +func (n NullFloat64) EncodeBinary(w *WriteBuf, oid Oid) error { + if oid != Float8Oid { + return SerializationError(fmt.Sprintf("NullFloat64.EncodeBinary cannot encode into OID %d", oid)) } if !n.Valid { @@ -216,9 +216,9 @@ func (n NullInt16) EncodeText() (string, byte, error) { } } -func (n NullInt16) EncodeBinary(w *WriteBuf, fd *FieldDescription) error { - if fd.DataType != Int2Oid { - return SerializationError(fmt.Sprintf("NullInt16.EncodeBinary cannot encode into OID %d", fd.DataType)) +func (n NullInt16) EncodeBinary(w *WriteBuf, oid Oid) error { + if oid != Int2Oid { + return SerializationError(fmt.Sprintf("NullInt16.EncodeBinary cannot encode into OID %d", oid)) } if !n.Valid { @@ -258,9 +258,9 @@ func (n NullInt32) EncodeText() (string, byte, error) { } } -func (n NullInt32) EncodeBinary(w *WriteBuf, fd *FieldDescription) error { - if fd.DataType != Int4Oid { - return SerializationError(fmt.Sprintf("NullInt32.EncodeBinary cannot encode into OID %d", fd.DataType)) +func (n NullInt32) EncodeBinary(w *WriteBuf, oid Oid) error { + if oid != Int4Oid { + return SerializationError(fmt.Sprintf("NullInt32.EncodeBinary cannot encode into OID %d", oid)) } if !n.Valid { @@ -300,9 +300,9 @@ func (n NullInt64) EncodeText() (string, byte, error) { } } -func (n NullInt64) EncodeBinary(w *WriteBuf, fd *FieldDescription) error { - if fd.DataType != Int8Oid { - return SerializationError(fmt.Sprintf("NullInt64.EncodeBinary cannot encode into OID %d", fd.DataType)) +func (n NullInt64) EncodeBinary(w *WriteBuf, oid Oid) error { + if oid != Int8Oid { + return SerializationError(fmt.Sprintf("NullInt64.EncodeBinary cannot encode into OID %d", oid)) } if !n.Valid { @@ -342,9 +342,9 @@ func (n NullBool) EncodeText() (string, byte, error) { } } -func (n NullBool) EncodeBinary(w *WriteBuf, fd *FieldDescription) error { - if fd.DataType != BoolOid { - return SerializationError(fmt.Sprintf("NullBool.EncodeBinary cannot encode into OID %d", fd.DataType)) +func (n NullBool) EncodeBinary(w *WriteBuf, oid Oid) error { + if oid != BoolOid { + return SerializationError(fmt.Sprintf("NullBool.EncodeBinary cannot encode into OID %d", oid)) } if !n.Valid { @@ -390,9 +390,9 @@ func (n NullTime) EncodeText() (string, byte, error) { } } -func (n NullTime) EncodeBinary(w *WriteBuf, fd *FieldDescription) error { - if fd.DataType != TimestampTzOid { - return SerializationError(fmt.Sprintf("NullTime.EncodeBinary cannot encode into OID %d", fd.DataType)) +func (n NullTime) EncodeBinary(w *WriteBuf, oid Oid) error { + if oid != TimestampTzOid { + return SerializationError(fmt.Sprintf("NullTime.EncodeBinary cannot encode into OID %d", oid)) } if !n.Valid { diff --git a/values_test.go b/values_test.go index fba87f56..6bc9c1a3 100644 --- a/values_test.go +++ b/values_test.go @@ -200,6 +200,7 @@ func TestNullX(t *testing.T) { {"select $1::bool", []interface{}{pgx.NullBool{Bool: true, Valid: false}}, []interface{}{&actual.b}, allTypes{b: pgx.NullBool{Bool: false, Valid: false}}}, {"select $1::timestamptz", []interface{}{pgx.NullTime{Time: time.Unix(123, 5000), Valid: true}}, []interface{}{&actual.t}, allTypes{t: pgx.NullTime{Time: time.Unix(123, 5000), Valid: true}}}, {"select $1::timestamptz", []interface{}{pgx.NullTime{Time: time.Unix(123, 5000), Valid: false}}, []interface{}{&actual.b}, allTypes{t: pgx.NullTime{Time: time.Time{}, Valid: false}}}, + {"select 42::int4, $1::float8", []interface{}{pgx.NullFloat64{Float64: 1.23, Valid: true}}, []interface{}{&actual.i32, &actual.f64}, allTypes{i32: pgx.NullInt32{Int32: 42, Valid: true}, f64: pgx.NullFloat64{Float64: 1.23, Valid: true}}}, } for i, tt := range tests {