Add cidr support
This commit is contained in:
@@ -751,7 +751,7 @@ func (c *Conn) sendPreparedQuery(ps *PreparedStatement, arguments ...interface{}
|
||||
err = encodeTimestampTz(wbuf, arguments[i])
|
||||
case TimestampOid:
|
||||
err = encodeTimestamp(wbuf, arguments[i])
|
||||
case InetOid:
|
||||
case InetOid, CidrOid:
|
||||
err = encodeInet(wbuf, arguments[i])
|
||||
case BoolArrayOid:
|
||||
err = encodeBoolArray(wbuf, arguments[i])
|
||||
|
||||
@@ -358,7 +358,7 @@ func (rows *Rows) Values() ([]interface{}, error) {
|
||||
values = append(values, decodeTimestampTz(vr))
|
||||
case TimestampOid:
|
||||
values = append(values, decodeTimestamp(vr))
|
||||
case InetOid:
|
||||
case InetOid, CidrOid:
|
||||
values = append(values, decodeInet(vr))
|
||||
default:
|
||||
rows.Fatal(errors.New("Values cannot handle binary format non-intrinsic types"))
|
||||
|
||||
@@ -19,6 +19,7 @@ const (
|
||||
Int4Oid = 23
|
||||
TextOid = 25
|
||||
OidOid = 26
|
||||
CidrOid = 650
|
||||
Float4Oid = 700
|
||||
Float8Oid = 701
|
||||
InetOid = 869
|
||||
@@ -1111,8 +1112,9 @@ func decodeInet(vr *ValueReader) net.IPNet {
|
||||
return zero
|
||||
}
|
||||
|
||||
if vr.Type().DataType != InetOid {
|
||||
vr.Fatal(ProtocolError(fmt.Sprintf("Cannot decode oid %v into inet", vr.Type().DataType)))
|
||||
pgType := vr.Type()
|
||||
if pgType.DataType != InetOid && pgType.DataType != CidrOid {
|
||||
vr.Fatal(ProtocolError(fmt.Sprintf("Cannot decode oid %v into %s", pgType.DataType, vr.Type().Name)))
|
||||
return zero
|
||||
}
|
||||
|
||||
|
||||
+11
-1
@@ -99,7 +99,7 @@ func mustParseCIDR(t *testing.T, s string) net.IPNet {
|
||||
return *ipnet
|
||||
}
|
||||
|
||||
func TestInetTranscode(t *testing.T) {
|
||||
func TestInetCidrTranscode(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
conn := mustConnect(t, *defaultConnConfig)
|
||||
@@ -119,6 +119,16 @@ func TestInetTranscode(t *testing.T) {
|
||||
{"select $1::inet", mustParseCIDR(t, "::/0")},
|
||||
{"select $1::inet", mustParseCIDR(t, "::1/128")},
|
||||
{"select $1::inet", mustParseCIDR(t, "2607:f8b0:4009:80b::200e/128")},
|
||||
{"select $1::cidr", mustParseCIDR(t, "0.0.0.0/32")},
|
||||
{"select $1::cidr", mustParseCIDR(t, "127.0.0.1/32")},
|
||||
{"select $1::cidr", mustParseCIDR(t, "12.34.56.0/32")},
|
||||
{"select $1::cidr", mustParseCIDR(t, "192.168.1.0/24")},
|
||||
{"select $1::cidr", mustParseCIDR(t, "255.0.0.0/8")},
|
||||
{"select $1::cidr", mustParseCIDR(t, "255.255.255.255/32")},
|
||||
{"select $1::cidr", mustParseCIDR(t, "::/128")},
|
||||
{"select $1::cidr", mustParseCIDR(t, "::/0")},
|
||||
{"select $1::cidr", mustParseCIDR(t, "::1/128")},
|
||||
{"select $1::cidr", mustParseCIDR(t, "2607:f8b0:4009:80b::200e/128")},
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
|
||||
Reference in New Issue
Block a user