Add support for varchar[]

This commit is contained in:
Jack Christensen 2014-07-31 13:35:44 -05:00
parent 56d0518e04
commit fb55203324
4 changed files with 31 additions and 25 deletions

View File

@ -471,7 +471,7 @@ func (c *Conn) sendPreparedQuery(ps *PreparedStatement, arguments ...interface{}
wbuf.WriteInt16(TextFormatCode) wbuf.WriteInt16(TextFormatCode)
default: default:
switch oid { switch oid {
case BoolOid, ByteaOid, Int2Oid, Int4Oid, Int8Oid, Float4Oid, Float8Oid, TimestampTzOid, Int2ArrayOid, Int4ArrayOid, Int8ArrayOid, Float4ArrayOid, Float8ArrayOid, TextArrayOid: case BoolOid, ByteaOid, Int2Oid, Int4Oid, Int8Oid, Float4Oid, Float8Oid, TimestampTzOid, Int2ArrayOid, Int4ArrayOid, Int8ArrayOid, Float4ArrayOid, Float8ArrayOid, TextArrayOid, VarcharArrayOid:
wbuf.WriteInt16(BinaryFormatCode) wbuf.WriteInt16(BinaryFormatCode)
default: default:
wbuf.WriteInt16(TextFormatCode) wbuf.WriteInt16(TextFormatCode)
@ -526,7 +526,9 @@ func (c *Conn) sendPreparedQuery(ps *PreparedStatement, arguments ...interface{}
case Float8ArrayOid: case Float8ArrayOid:
err = encodeFloat8Array(wbuf, arguments[i]) err = encodeFloat8Array(wbuf, arguments[i])
case TextArrayOid: case TextArrayOid:
err = encodeTextArray(wbuf, arguments[i]) err = encodeTextArray(wbuf, arguments[i], TextOid)
case VarcharArrayOid:
err = encodeTextArray(wbuf, arguments[i], VarcharOid)
default: default:
return SerializationError(fmt.Sprintf("%T is not a core type and it does not implement Encoder", arg)) return SerializationError(fmt.Sprintf("%T is not a core type and it does not implement Encoder", arg))
} }

View File

@ -306,7 +306,7 @@ func (rows *Rows) Values() ([]interface{}, error) {
values = append(values, decodeFloat4Array(vr)) values = append(values, decodeFloat4Array(vr))
case Float8ArrayOid: case Float8ArrayOid:
values = append(values, decodeFloat8Array(vr)) values = append(values, decodeFloat8Array(vr))
case TextArrayOid: case TextArrayOid, VarcharArrayOid:
values = append(values, decodeTextArray(vr)) values = append(values, decodeTextArray(vr))
case DateOid: case DateOid:
values = append(values, decodeDate(vr)) values = append(values, decodeDate(vr))

View File

@ -738,6 +738,8 @@ func TestQueryRowCoreStringSlice(t *testing.T) {
}{ }{
{"select $1::text[]", []string{"Adam", "Eve", "UTF-8 Characters Å Æ Ë Ͽ"}}, {"select $1::text[]", []string{"Adam", "Eve", "UTF-8 Characters Å Æ Ë Ͽ"}},
{"select $1::text[]", []string{}}, {"select $1::text[]", []string{}},
{"select $1::varchar[]", []string{"Adam", "Eve", "UTF-8 Characters Å Æ Ë Ͽ"}},
{"select $1::varchar[]", []string{}},
} }
for i, tt := range tests { for i, tt := range tests {

View File

@ -11,24 +11,25 @@ import (
// PostgreSQL oids for common types // PostgreSQL oids for common types
const ( const (
BoolOid = 16 BoolOid = 16
ByteaOid = 17 ByteaOid = 17
Int8Oid = 20 Int8Oid = 20
Int2Oid = 21 Int2Oid = 21
Int4Oid = 23 Int4Oid = 23
TextOid = 25 TextOid = 25
Float4Oid = 700 Float4Oid = 700
Float8Oid = 701 Float8Oid = 701
Int2ArrayOid = 1005 Int2ArrayOid = 1005
Int4ArrayOid = 1007 Int4ArrayOid = 1007
TextArrayOid = 1009 TextArrayOid = 1009
Int8ArrayOid = 1016 VarcharArrayOid = 1015
Float4ArrayOid = 1021 Int8ArrayOid = 1016
Float8ArrayOid = 1022 Float4ArrayOid = 1021
VarcharOid = 1043 Float8ArrayOid = 1022
DateOid = 1082 VarcharOid = 1043
TimestampOid = 1114 DateOid = 1082
TimestampTzOid = 1184 TimestampOid = 1114
TimestampTzOid = 1184
) )
// PostgreSQL format codes // PostgreSQL format codes
@ -56,6 +57,7 @@ func init() {
DefaultOidFormats[Float4ArrayOid] = BinaryFormatCode DefaultOidFormats[Float4ArrayOid] = BinaryFormatCode
DefaultOidFormats[Float8ArrayOid] = BinaryFormatCode DefaultOidFormats[Float8ArrayOid] = BinaryFormatCode
DefaultOidFormats[TextArrayOid] = BinaryFormatCode DefaultOidFormats[TextArrayOid] = BinaryFormatCode
DefaultOidFormats[VarcharArrayOid] = BinaryFormatCode
} }
type SerializationError string type SerializationError string
@ -1318,8 +1320,8 @@ func decodeTextArray(vr *ValueReader) []string {
return nil return nil
} }
if vr.Type().DataType != TextArrayOid { if vr.Type().DataType != TextArrayOid && vr.Type().DataType != VarcharArrayOid {
vr.Fatal(ProtocolError(fmt.Sprintf("Expected type oid %v but received type oid %v", TextArrayOid, vr.Type().DataType))) vr.Fatal(ProtocolError(fmt.Sprintf("Expected type oid %v or %v but received type oid %v", TextArrayOid, VarcharArrayOid, vr.Type().DataType)))
return nil return nil
} }
@ -1348,7 +1350,7 @@ func decodeTextArray(vr *ValueReader) []string {
return a return a
} }
func encodeTextArray(w *WriteBuf, value interface{}) error { func encodeTextArray(w *WriteBuf, value interface{}, elOid Oid) error {
slice, ok := value.([]string) slice, ok := value.([]string)
if !ok { if !ok {
return fmt.Errorf("Expected []string, received %T", value) return fmt.Errorf("Expected []string, received %T", value)
@ -1364,7 +1366,7 @@ func encodeTextArray(w *WriteBuf, value interface{}) error {
w.WriteInt32(1) // number of dimensions w.WriteInt32(1) // number of dimensions
w.WriteInt32(0) // no nulls w.WriteInt32(0) // no nulls
w.WriteInt32(TextOid) // type of elements w.WriteInt32(int32(elOid)) // type of elements
w.WriteInt32(int32(len(slice))) // number of elements w.WriteInt32(int32(len(slice))) // number of elements
w.WriteInt32(1) // index of first element w.WriteInt32(1) // index of first element