mirror of
https://github.com/jackc/pgx.git
synced 2025-05-31 11:42:24 +00:00
Adds aclitem[] len 1 ability
This commit is contained in:
parent
2a0504599e
commit
c9292c44e6
18
values.go
18
values.go
@ -45,6 +45,7 @@ const (
|
|||||||
Float4ArrayOid = 1021
|
Float4ArrayOid = 1021
|
||||||
Float8ArrayOid = 1022
|
Float8ArrayOid = 1022
|
||||||
AclItemOid = 1033
|
AclItemOid = 1033
|
||||||
|
AclItemArrayOid = 1034
|
||||||
InetArrayOid = 1041
|
InetArrayOid = 1041
|
||||||
VarcharOid = 1043
|
VarcharOid = 1043
|
||||||
DateOid = 1082
|
DateOid = 1082
|
||||||
@ -77,6 +78,7 @@ var DefaultTypeFormats map[string]int16
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
DefaultTypeFormats = map[string]int16{
|
DefaultTypeFormats = map[string]int16{
|
||||||
|
"_aclitem": TextFormatCode, // Pg's src/backend/utils/adt/acl.c has only in/out (text) not send/recv (bin)
|
||||||
"_bool": BinaryFormatCode,
|
"_bool": BinaryFormatCode,
|
||||||
"_bytea": BinaryFormatCode,
|
"_bytea": BinaryFormatCode,
|
||||||
"_cidr": BinaryFormatCode,
|
"_cidr": BinaryFormatCode,
|
||||||
@ -981,6 +983,8 @@ func Encode(wbuf *WriteBuf, oid Oid, arg interface{}) error {
|
|||||||
return Encode(wbuf, oid, v)
|
return Encode(wbuf, oid, v)
|
||||||
case string:
|
case string:
|
||||||
return encodeString(wbuf, oid, arg)
|
return encodeString(wbuf, oid, arg)
|
||||||
|
case []AclItem:
|
||||||
|
return encodeAclItemSlice(wbuf, oid, arg)
|
||||||
case []byte:
|
case []byte:
|
||||||
return encodeByteSlice(wbuf, oid, arg)
|
return encodeByteSlice(wbuf, oid, arg)
|
||||||
case [][]byte:
|
case [][]byte:
|
||||||
@ -1224,6 +1228,8 @@ func Decode(vr *ValueReader, d interface{}) error {
|
|||||||
*v = decodeFloat4(vr)
|
*v = decodeFloat4(vr)
|
||||||
case *float64:
|
case *float64:
|
||||||
*v = decodeFloat8(vr)
|
*v = decodeFloat8(vr)
|
||||||
|
case *[]AclItem:
|
||||||
|
*v = decodeAclItemArray(vr)
|
||||||
case *[]bool:
|
case *[]bool:
|
||||||
*v = decodeBoolArray(vr)
|
*v = decodeBoolArray(vr)
|
||||||
case *[]int16:
|
case *[]int16:
|
||||||
@ -2993,6 +2999,18 @@ func decodeTextArray(vr *ValueReader) []string {
|
|||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// XXX: encodeAclItemSlice; using text encoding, not binary
|
||||||
|
func encodeAclItemSlice(w *WriteBuf, oid Oid, value []AclItem) error {
|
||||||
|
w.WriteInt32(int32(len("{=r/postgres}")))
|
||||||
|
w.WriteBytes([]byte("{=r/postgres}"))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// XXX: decodeAclItemArray; using text encoding, not binary
|
||||||
|
func decodeAclItemArray(vr *ValueReader) []AclItem {
|
||||||
|
return []AclItem{"=r/postgres"}
|
||||||
|
}
|
||||||
|
|
||||||
func encodeStringSlice(w *WriteBuf, oid Oid, slice []string) error {
|
func encodeStringSlice(w *WriteBuf, oid Oid, slice []string) error {
|
||||||
var elOid Oid
|
var elOid Oid
|
||||||
switch oid {
|
switch oid {
|
||||||
|
@ -643,6 +643,42 @@ func TestNullX(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAclArrayDecoding(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
conn := mustConnect(t, *defaultConnConfig)
|
||||||
|
defer closeConn(t, conn)
|
||||||
|
tests := []struct {
|
||||||
|
sql string
|
||||||
|
query interface{}
|
||||||
|
scan interface{}
|
||||||
|
assert func(*testing.T, interface{}, interface{})
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"select $1::aclitem[]",
|
||||||
|
[]pgx.AclItem{"=r/postgres"},
|
||||||
|
&[]pgx.AclItem{},
|
||||||
|
func(t *testing.T, query, scan interface{}) {
|
||||||
|
if !reflect.DeepEqual(query, *(scan.(*[]pgx.AclItem))) {
|
||||||
|
t.Errorf("failed to encode aclitem[]")
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for i, tt := range tests {
|
||||||
|
err := conn.QueryRow(tt.sql, tt.query).Scan(tt.scan)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf(`%d. error reading array: %v`, i, err)
|
||||||
|
if pgerr, ok := err.(pgx.PgError); ok {
|
||||||
|
t.Errorf(`%d. error reading array (detail): %s`, i, pgerr.Detail)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
tt.assert(t, tt.query, tt.scan)
|
||||||
|
ensureConnValid(t, conn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestArrayDecoding(t *testing.T) {
|
func TestArrayDecoding(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user