copy_both_response.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package pgproto3
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "encoding/json"
  6. "errors"
  7. "math"
  8. "github.com/jackc/pgx/v5/internal/pgio"
  9. )
  10. type CopyBothResponse struct {
  11. OverallFormat byte
  12. ColumnFormatCodes []uint16
  13. }
  14. // Backend identifies this message as sendable by the PostgreSQL backend.
  15. func (*CopyBothResponse) Backend() {}
  16. // Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message
  17. // type identifier and 4 byte message length.
  18. func (dst *CopyBothResponse) Decode(src []byte) error {
  19. buf := bytes.NewBuffer(src)
  20. if buf.Len() < 3 {
  21. return &invalidMessageFormatErr{messageType: "CopyBothResponse"}
  22. }
  23. overallFormat := buf.Next(1)[0]
  24. columnCount := int(binary.BigEndian.Uint16(buf.Next(2)))
  25. if buf.Len() != columnCount*2 {
  26. return &invalidMessageFormatErr{messageType: "CopyBothResponse"}
  27. }
  28. columnFormatCodes := make([]uint16, columnCount)
  29. for i := range columnCount {
  30. columnFormatCodes[i] = binary.BigEndian.Uint16(buf.Next(2))
  31. }
  32. *dst = CopyBothResponse{OverallFormat: overallFormat, ColumnFormatCodes: columnFormatCodes}
  33. return nil
  34. }
  35. // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
  36. func (src *CopyBothResponse) Encode(dst []byte) ([]byte, error) {
  37. dst, sp := beginMessage(dst, 'W')
  38. dst = append(dst, src.OverallFormat)
  39. if len(src.ColumnFormatCodes) > math.MaxUint16 {
  40. return nil, errors.New("too many column format codes")
  41. }
  42. dst = pgio.AppendUint16(dst, uint16(len(src.ColumnFormatCodes)))
  43. for _, fc := range src.ColumnFormatCodes {
  44. dst = pgio.AppendUint16(dst, fc)
  45. }
  46. return finishMessage(dst, sp)
  47. }
  48. // MarshalJSON implements encoding/json.Marshaler.
  49. func (src CopyBothResponse) MarshalJSON() ([]byte, error) {
  50. return json.Marshal(struct {
  51. Type string
  52. ColumnFormatCodes []uint16
  53. }{
  54. Type: "CopyBothResponse",
  55. ColumnFormatCodes: src.ColumnFormatCodes,
  56. })
  57. }
  58. // UnmarshalJSON implements encoding/json.Unmarshaler.
  59. func (dst *CopyBothResponse) UnmarshalJSON(data []byte) error {
  60. // Ignore null, like in the main JSON package.
  61. if string(data) == "null" {
  62. return nil
  63. }
  64. var msg struct {
  65. OverallFormat string
  66. ColumnFormatCodes []uint16
  67. }
  68. if err := json.Unmarshal(data, &msg); err != nil {
  69. return err
  70. }
  71. if len(msg.OverallFormat) != 1 {
  72. return errors.New("invalid length for CopyBothResponse.OverallFormat")
  73. }
  74. dst.OverallFormat = msg.OverallFormat[0]
  75. dst.ColumnFormatCodes = msg.ColumnFormatCodes
  76. return nil
  77. }