diff --git a/pgx/sparsevec.go b/pgx/sparsevec.go index a5f71a0..6738790 100644 --- a/pgx/sparsevec.go +++ b/pgx/sparsevec.go @@ -12,7 +12,7 @@ import ( type SparseVectorCodec struct{} func (SparseVectorCodec) FormatSupported(format int16) bool { - return format == pgx.BinaryFormatCode + return format == pgx.BinaryFormatCode || format == pgx.TextFormatCode } func (SparseVectorCodec) PreferredFormat() int16 { @@ -25,8 +25,11 @@ func (SparseVectorCodec) PlanEncode(m *pgtype.Map, oid uint32, format int16, val return nil } - if format == pgx.BinaryFormatCode { + switch format { + case pgx.BinaryFormatCode: return encodePlanSparseVectorCodecBinary{} + case pgx.TextFormatCode: + return encodePlanSparseVectorCodecText{} } return nil @@ -39,6 +42,13 @@ func (encodePlanSparseVectorCodecBinary) Encode(value any, buf []byte) (newBuf [ return v.EncodeBinary(buf) } +type encodePlanSparseVectorCodecText struct{} + +func (encodePlanSparseVectorCodecText) Encode(value any, buf []byte) (newBuf []byte, err error) { + v := value.(pgvector.SparseVector) + return append(buf, v.String()...), nil +} + func (SparseVectorCodec) PlanScan(m *pgtype.Map, oid uint32, format int16, target any) pgtype.ScanPlan { _, ok := target.(*pgvector.SparseVector) if !ok { diff --git a/pgx/vector.go b/pgx/vector.go index f491cd4..ae16475 100644 --- a/pgx/vector.go +++ b/pgx/vector.go @@ -12,7 +12,7 @@ import ( type VectorCodec struct{} func (VectorCodec) FormatSupported(format int16) bool { - return format == pgx.BinaryFormatCode + return format == pgx.BinaryFormatCode || format == pgx.TextFormatCode } func (VectorCodec) PreferredFormat() int16 { @@ -25,8 +25,11 @@ func (VectorCodec) PlanEncode(m *pgtype.Map, oid uint32, format int16, value any return nil } - if format == pgx.BinaryFormatCode { + switch format { + case pgx.BinaryFormatCode: return encodePlanVectorCodecBinary{} + case pgx.TextFormatCode: + return encodePlanVectorCodecText{} } return nil @@ -39,6 +42,13 @@ func (encodePlanVectorCodecBinary) Encode(value any, buf []byte) (newBuf []byte, return v.EncodeBinary(buf) } +type encodePlanVectorCodecText struct{} + +func (encodePlanVectorCodecText) Encode(value any, buf []byte) (newBuf []byte, err error) { + v := value.(pgvector.Vector) + return append(buf, v.String()...), nil +} + func (VectorCodec) PlanScan(m *pgtype.Map, oid uint32, format int16, target any) pgtype.ScanPlan { _, ok := target.(*pgvector.Vector) if !ok {