private/dbutil/pgutil: add UUIDArray helper

Change-Id: I469fa2381b7c1a731880a742787bb4842af9b902
This commit is contained in:
Egon Elbre 2021-02-25 13:38:02 +02:00
parent 1d28dc314c
commit 4a2c6f6208

View File

@ -9,6 +9,7 @@ import (
"github.com/jackc/pgtype"
"storj.io/common/storj"
"storj.io/common/uuid"
)
// The following XArray() helper methods exist alongside similar methods in the
@ -134,3 +135,22 @@ func NodeIDArray(nodeIDs []storj.NodeID) *pgtype.ByteaArray {
Status: pgtype.Present,
}
}
// UUIDArray returns an object usable by pg drivers for passing a []uuid.UUID
// slice into a database as type BYTEA[].
func UUIDArray(uuids []uuid.UUID) *pgtype.ByteaArray {
if uuids == nil {
return &pgtype.ByteaArray{Status: pgtype.Null}
}
pgtypeByteaArray := make([]pgtype.Bytea, len(uuids))
for i, uuid := range uuids {
uuidCopy := uuid
pgtypeByteaArray[i].Bytes = uuidCopy[:]
pgtypeByteaArray[i].Status = pgtype.Present
}
return &pgtype.ByteaArray{
Elements: pgtypeByteaArray,
Dimensions: []pgtype.ArrayDimension{{Length: int32(len(uuids)), LowerBound: 1}},
Status: pgtype.Present,
}
}