row_description.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. const (
  11. TextFormat = 0
  12. BinaryFormat = 1
  13. )
  14. type FieldDescription struct {
  15. Name []byte
  16. TableOID uint32
  17. TableAttributeNumber uint16
  18. DataTypeOID uint32
  19. DataTypeSize int16
  20. TypeModifier int32
  21. Format int16
  22. }
  23. // MarshalJSON implements encoding/json.Marshaler.
  24. func (fd FieldDescription) MarshalJSON() ([]byte, error) {
  25. return json.Marshal(struct {
  26. Name string
  27. TableOID uint32
  28. TableAttributeNumber uint16
  29. DataTypeOID uint32
  30. DataTypeSize int16
  31. TypeModifier int32
  32. Format int16
  33. }{
  34. Name: string(fd.Name),
  35. TableOID: fd.TableOID,
  36. TableAttributeNumber: fd.TableAttributeNumber,
  37. DataTypeOID: fd.DataTypeOID,
  38. DataTypeSize: fd.DataTypeSize,
  39. TypeModifier: fd.TypeModifier,
  40. Format: fd.Format,
  41. })
  42. }
  43. type RowDescription struct {
  44. Fields []FieldDescription
  45. }
  46. // Backend identifies this message as sendable by the PostgreSQL backend.
  47. func (*RowDescription) Backend() {}
  48. // Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message
  49. // type identifier and 4 byte message length.
  50. func (dst *RowDescription) Decode(src []byte) error {
  51. if len(src) < 2 {
  52. return &invalidMessageFormatErr{messageType: "RowDescription"}
  53. }
  54. fieldCount := int(binary.BigEndian.Uint16(src))
  55. rp := 2
  56. dst.Fields = dst.Fields[0:0]
  57. for range fieldCount {
  58. var fd FieldDescription
  59. idx := bytes.IndexByte(src[rp:], 0)
  60. if idx < 0 {
  61. return &invalidMessageFormatErr{messageType: "RowDescription"}
  62. }
  63. fd.Name = src[rp : rp+idx]
  64. rp += idx + 1
  65. // Since buf.Next() doesn't return an error if we hit the end of the buffer
  66. // check Len ahead of time
  67. if len(src[rp:]) < 18 {
  68. return &invalidMessageFormatErr{messageType: "RowDescription"}
  69. }
  70. fd.TableOID = binary.BigEndian.Uint32(src[rp:])
  71. rp += 4
  72. fd.TableAttributeNumber = binary.BigEndian.Uint16(src[rp:])
  73. rp += 2
  74. fd.DataTypeOID = binary.BigEndian.Uint32(src[rp:])
  75. rp += 4
  76. fd.DataTypeSize = int16(binary.BigEndian.Uint16(src[rp:]))
  77. rp += 2
  78. fd.TypeModifier = int32(binary.BigEndian.Uint32(src[rp:]))
  79. rp += 4
  80. fd.Format = int16(binary.BigEndian.Uint16(src[rp:]))
  81. rp += 2
  82. dst.Fields = append(dst.Fields, fd)
  83. }
  84. return nil
  85. }
  86. // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
  87. func (src *RowDescription) Encode(dst []byte) ([]byte, error) {
  88. dst, sp := beginMessage(dst, 'T')
  89. if len(src.Fields) > math.MaxUint16 {
  90. return nil, errors.New("too many fields")
  91. }
  92. dst = pgio.AppendUint16(dst, uint16(len(src.Fields)))
  93. for _, fd := range src.Fields {
  94. dst = append(dst, fd.Name...)
  95. dst = append(dst, 0)
  96. dst = pgio.AppendUint32(dst, fd.TableOID)
  97. dst = pgio.AppendUint16(dst, fd.TableAttributeNumber)
  98. dst = pgio.AppendUint32(dst, fd.DataTypeOID)
  99. dst = pgio.AppendInt16(dst, fd.DataTypeSize)
  100. dst = pgio.AppendInt32(dst, fd.TypeModifier)
  101. dst = pgio.AppendInt16(dst, fd.Format)
  102. }
  103. return finishMessage(dst, sp)
  104. }
  105. // MarshalJSON implements encoding/json.Marshaler.
  106. func (src RowDescription) MarshalJSON() ([]byte, error) {
  107. return json.Marshal(struct {
  108. Type string
  109. Fields []FieldDescription
  110. }{
  111. Type: "RowDescription",
  112. Fields: src.Fields,
  113. })
  114. }
  115. // UnmarshalJSON implements encoding/json.Unmarshaler.
  116. func (dst *RowDescription) UnmarshalJSON(data []byte) error {
  117. var msg struct {
  118. Fields []struct {
  119. Name string
  120. TableOID uint32
  121. TableAttributeNumber uint16
  122. DataTypeOID uint32
  123. DataTypeSize int16
  124. TypeModifier int32
  125. Format int16
  126. }
  127. }
  128. if err := json.Unmarshal(data, &msg); err != nil {
  129. return err
  130. }
  131. dst.Fields = make([]FieldDescription, len(msg.Fields))
  132. for n, field := range msg.Fields {
  133. dst.Fields[n] = FieldDescription{
  134. Name: []byte(field.Name),
  135. TableOID: field.TableOID,
  136. TableAttributeNumber: field.TableAttributeNumber,
  137. DataTypeOID: field.DataTypeOID,
  138. DataTypeSize: field.DataTypeSize,
  139. TypeModifier: field.TypeModifier,
  140. Format: field.Format,
  141. }
  142. }
  143. return nil
  144. }