...
1 package pgproto3
2
3 import (
4 "bytes"
5 "encoding/binary"
6 "encoding/json"
7 "errors"
8 "math"
9
10 "github.com/jackc/pgx/v5/internal/pgio"
11 )
12
13 type CopyBothResponse struct {
14 OverallFormat byte
15 ColumnFormatCodes []uint16
16 }
17
18
19 func (*CopyBothResponse) Backend() {}
20
21
22
23 func (dst *CopyBothResponse) Decode(src []byte) error {
24 buf := bytes.NewBuffer(src)
25
26 if buf.Len() < 3 {
27 return &invalidMessageFormatErr{messageType: "CopyBothResponse"}
28 }
29
30 overallFormat := buf.Next(1)[0]
31
32 columnCount := int(binary.BigEndian.Uint16(buf.Next(2)))
33 if buf.Len() != columnCount*2 {
34 return &invalidMessageFormatErr{messageType: "CopyBothResponse"}
35 }
36
37 columnFormatCodes := make([]uint16, columnCount)
38 for i := 0; i < columnCount; i++ {
39 columnFormatCodes[i] = binary.BigEndian.Uint16(buf.Next(2))
40 }
41
42 *dst = CopyBothResponse{OverallFormat: overallFormat, ColumnFormatCodes: columnFormatCodes}
43
44 return nil
45 }
46
47
48 func (src *CopyBothResponse) Encode(dst []byte) ([]byte, error) {
49 dst, sp := beginMessage(dst, 'W')
50 dst = append(dst, src.OverallFormat)
51 if len(src.ColumnFormatCodes) > math.MaxUint16 {
52 return nil, errors.New("too many column format codes")
53 }
54 dst = pgio.AppendUint16(dst, uint16(len(src.ColumnFormatCodes)))
55 for _, fc := range src.ColumnFormatCodes {
56 dst = pgio.AppendUint16(dst, fc)
57 }
58
59 return finishMessage(dst, sp)
60 }
61
62
63 func (src CopyBothResponse) MarshalJSON() ([]byte, error) {
64 return json.Marshal(struct {
65 Type string
66 ColumnFormatCodes []uint16
67 }{
68 Type: "CopyBothResponse",
69 ColumnFormatCodes: src.ColumnFormatCodes,
70 })
71 }
72
73
74 func (dst *CopyBothResponse) UnmarshalJSON(data []byte) error {
75
76 if string(data) == "null" {
77 return nil
78 }
79
80 var msg struct {
81 OverallFormat string
82 ColumnFormatCodes []uint16
83 }
84 if err := json.Unmarshal(data, &msg); err != nil {
85 return err
86 }
87
88 if len(msg.OverallFormat) != 1 {
89 return errors.New("invalid length for CopyBothResponse.OverallFormat")
90 }
91
92 dst.OverallFormat = msg.OverallFormat[0]
93 dst.ColumnFormatCodes = msg.ColumnFormatCodes
94 return nil
95 }
96
View as plain text