data_row.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package pgproto3
  2. import (
  3. "encoding/binary"
  4. "encoding/hex"
  5. "encoding/json"
  6. "errors"
  7. "math"
  8. "github.com/jackc/pgx/v5/internal/pgio"
  9. )
  10. type DataRow struct {
  11. Values [][]byte
  12. }
  13. // Backend identifies this message as sendable by the PostgreSQL backend.
  14. func (*DataRow) Backend() {}
  15. // Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message
  16. // type identifier and 4 byte message length.
  17. func (dst *DataRow) Decode(src []byte) error {
  18. if len(src) < 2 {
  19. return &invalidMessageFormatErr{messageType: "DataRow"}
  20. }
  21. rp := 0
  22. fieldCount := int(binary.BigEndian.Uint16(src[rp:]))
  23. rp += 2
  24. // If the capacity of the values slice is too small OR substantially too
  25. // large reallocate. This is too avoid one row with many columns from
  26. // permanently allocating memory.
  27. if cap(dst.Values) < fieldCount || cap(dst.Values)-fieldCount > 32 {
  28. newCap := max(32, fieldCount)
  29. dst.Values = make([][]byte, fieldCount, newCap)
  30. } else {
  31. dst.Values = dst.Values[:fieldCount]
  32. }
  33. for i := range fieldCount {
  34. if len(src[rp:]) < 4 {
  35. return &invalidMessageFormatErr{messageType: "DataRow"}
  36. }
  37. valueLen := int(int32(binary.BigEndian.Uint32(src[rp:])))
  38. rp += 4
  39. // null
  40. if valueLen == -1 {
  41. dst.Values[i] = nil
  42. } else {
  43. if len(src[rp:]) < valueLen || valueLen < 0 {
  44. return &invalidMessageFormatErr{messageType: "DataRow"}
  45. }
  46. dst.Values[i] = src[rp : rp+valueLen : rp+valueLen]
  47. rp += valueLen
  48. }
  49. }
  50. return nil
  51. }
  52. // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
  53. func (src *DataRow) Encode(dst []byte) ([]byte, error) {
  54. dst, sp := beginMessage(dst, 'D')
  55. if len(src.Values) > math.MaxUint16 {
  56. return nil, errors.New("too many values")
  57. }
  58. dst = pgio.AppendUint16(dst, uint16(len(src.Values)))
  59. for _, v := range src.Values {
  60. if v == nil {
  61. dst = pgio.AppendInt32(dst, -1)
  62. continue
  63. }
  64. dst = pgio.AppendInt32(dst, int32(len(v)))
  65. dst = append(dst, v...)
  66. }
  67. return finishMessage(dst, sp)
  68. }
  69. // MarshalJSON implements encoding/json.Marshaler.
  70. func (src DataRow) MarshalJSON() ([]byte, error) {
  71. formattedValues := make([]map[string]string, len(src.Values))
  72. for i, v := range src.Values {
  73. if v == nil {
  74. continue
  75. }
  76. var hasNonPrintable bool
  77. for _, b := range v {
  78. if b < 32 {
  79. hasNonPrintable = true
  80. break
  81. }
  82. }
  83. if hasNonPrintable {
  84. formattedValues[i] = map[string]string{"binary": hex.EncodeToString(v)}
  85. } else {
  86. formattedValues[i] = map[string]string{"text": string(v)}
  87. }
  88. }
  89. return json.Marshal(struct {
  90. Type string
  91. Values []map[string]string
  92. }{
  93. Type: "DataRow",
  94. Values: formattedValues,
  95. })
  96. }
  97. // UnmarshalJSON implements encoding/json.Unmarshaler.
  98. func (dst *DataRow) UnmarshalJSON(data []byte) error {
  99. // Ignore null, like in the main JSON package.
  100. if string(data) == "null" {
  101. return nil
  102. }
  103. var msg struct {
  104. Values []map[string]string
  105. }
  106. if err := json.Unmarshal(data, &msg); err != nil {
  107. return err
  108. }
  109. dst.Values = make([][]byte, len(msg.Values))
  110. for n, parameter := range msg.Values {
  111. var err error
  112. dst.Values[n], err = getValueFromJSON(parameter)
  113. if err != nil {
  114. return err
  115. }
  116. }
  117. return nil
  118. }