bind.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package pgproto3
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "encoding/hex"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "math"
  10. "github.com/jackc/pgx/v5/internal/pgio"
  11. )
  12. type Bind struct {
  13. DestinationPortal string
  14. PreparedStatement string
  15. ParameterFormatCodes []int16
  16. Parameters [][]byte
  17. ResultFormatCodes []int16
  18. }
  19. // Frontend identifies this message as sendable by a PostgreSQL frontend.
  20. func (*Bind) Frontend() {}
  21. // Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message
  22. // type identifier and 4 byte message length.
  23. func (dst *Bind) Decode(src []byte) error {
  24. *dst = Bind{}
  25. idx := bytes.IndexByte(src, 0)
  26. if idx < 0 {
  27. return &invalidMessageFormatErr{messageType: "Bind"}
  28. }
  29. dst.DestinationPortal = string(src[:idx])
  30. rp := idx + 1
  31. idx = bytes.IndexByte(src[rp:], 0)
  32. if idx < 0 {
  33. return &invalidMessageFormatErr{messageType: "Bind"}
  34. }
  35. dst.PreparedStatement = string(src[rp : rp+idx])
  36. rp += idx + 1
  37. if len(src[rp:]) < 2 {
  38. return &invalidMessageFormatErr{messageType: "Bind"}
  39. }
  40. parameterFormatCodeCount := int(binary.BigEndian.Uint16(src[rp:]))
  41. rp += 2
  42. if parameterFormatCodeCount > 0 {
  43. dst.ParameterFormatCodes = make([]int16, parameterFormatCodeCount)
  44. if len(src[rp:]) < len(dst.ParameterFormatCodes)*2 {
  45. return &invalidMessageFormatErr{messageType: "Bind"}
  46. }
  47. for i := range parameterFormatCodeCount {
  48. dst.ParameterFormatCodes[i] = int16(binary.BigEndian.Uint16(src[rp:]))
  49. rp += 2
  50. }
  51. }
  52. if len(src[rp:]) < 2 {
  53. return &invalidMessageFormatErr{messageType: "Bind"}
  54. }
  55. parameterCount := int(binary.BigEndian.Uint16(src[rp:]))
  56. rp += 2
  57. if parameterCount > 0 {
  58. dst.Parameters = make([][]byte, parameterCount)
  59. for i := range parameterCount {
  60. if len(src[rp:]) < 4 {
  61. return &invalidMessageFormatErr{messageType: "Bind"}
  62. }
  63. msgSize := int(int32(binary.BigEndian.Uint32(src[rp:])))
  64. rp += 4
  65. // null
  66. if msgSize == -1 {
  67. continue
  68. }
  69. if len(src[rp:]) < msgSize {
  70. return &invalidMessageFormatErr{messageType: "Bind"}
  71. }
  72. dst.Parameters[i] = src[rp : rp+msgSize]
  73. rp += msgSize
  74. }
  75. }
  76. if len(src[rp:]) < 2 {
  77. return &invalidMessageFormatErr{messageType: "Bind"}
  78. }
  79. resultFormatCodeCount := int(binary.BigEndian.Uint16(src[rp:]))
  80. rp += 2
  81. dst.ResultFormatCodes = make([]int16, resultFormatCodeCount)
  82. if len(src[rp:]) < len(dst.ResultFormatCodes)*2 {
  83. return &invalidMessageFormatErr{messageType: "Bind"}
  84. }
  85. for i := range resultFormatCodeCount {
  86. dst.ResultFormatCodes[i] = int16(binary.BigEndian.Uint16(src[rp:]))
  87. rp += 2
  88. }
  89. return nil
  90. }
  91. // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
  92. func (src *Bind) Encode(dst []byte) ([]byte, error) {
  93. dst, sp := beginMessage(dst, 'B')
  94. dst = append(dst, src.DestinationPortal...)
  95. dst = append(dst, 0)
  96. dst = append(dst, src.PreparedStatement...)
  97. dst = append(dst, 0)
  98. if len(src.ParameterFormatCodes) > math.MaxUint16 {
  99. return nil, errors.New("too many parameter format codes")
  100. }
  101. dst = pgio.AppendUint16(dst, uint16(len(src.ParameterFormatCodes)))
  102. for _, fc := range src.ParameterFormatCodes {
  103. dst = pgio.AppendInt16(dst, fc)
  104. }
  105. if len(src.Parameters) > math.MaxUint16 {
  106. return nil, errors.New("too many parameters")
  107. }
  108. dst = pgio.AppendUint16(dst, uint16(len(src.Parameters)))
  109. for _, p := range src.Parameters {
  110. if p == nil {
  111. dst = pgio.AppendInt32(dst, -1)
  112. continue
  113. }
  114. dst = pgio.AppendInt32(dst, int32(len(p)))
  115. dst = append(dst, p...)
  116. }
  117. if len(src.ResultFormatCodes) > math.MaxUint16 {
  118. return nil, errors.New("too many result format codes")
  119. }
  120. dst = pgio.AppendUint16(dst, uint16(len(src.ResultFormatCodes)))
  121. for _, fc := range src.ResultFormatCodes {
  122. dst = pgio.AppendInt16(dst, fc)
  123. }
  124. return finishMessage(dst, sp)
  125. }
  126. // MarshalJSON implements encoding/json.Marshaler.
  127. func (src Bind) MarshalJSON() ([]byte, error) {
  128. formattedParameters := make([]map[string]string, len(src.Parameters))
  129. for i, p := range src.Parameters {
  130. if p == nil {
  131. continue
  132. }
  133. textFormat := true
  134. if len(src.ParameterFormatCodes) == 1 {
  135. textFormat = src.ParameterFormatCodes[0] == 0
  136. } else if len(src.ParameterFormatCodes) > 1 {
  137. textFormat = src.ParameterFormatCodes[i] == 0
  138. }
  139. if textFormat {
  140. formattedParameters[i] = map[string]string{"text": string(p)}
  141. } else {
  142. formattedParameters[i] = map[string]string{"binary": hex.EncodeToString(p)}
  143. }
  144. }
  145. return json.Marshal(struct {
  146. Type string
  147. DestinationPortal string
  148. PreparedStatement string
  149. ParameterFormatCodes []int16
  150. Parameters []map[string]string
  151. ResultFormatCodes []int16
  152. }{
  153. Type: "Bind",
  154. DestinationPortal: src.DestinationPortal,
  155. PreparedStatement: src.PreparedStatement,
  156. ParameterFormatCodes: src.ParameterFormatCodes,
  157. Parameters: formattedParameters,
  158. ResultFormatCodes: src.ResultFormatCodes,
  159. })
  160. }
  161. // UnmarshalJSON implements encoding/json.Unmarshaler.
  162. func (dst *Bind) UnmarshalJSON(data []byte) error {
  163. // Ignore null, like in the main JSON package.
  164. if string(data) == "null" {
  165. return nil
  166. }
  167. var msg struct {
  168. DestinationPortal string
  169. PreparedStatement string
  170. ParameterFormatCodes []int16
  171. Parameters []map[string]string
  172. ResultFormatCodes []int16
  173. }
  174. err := json.Unmarshal(data, &msg)
  175. if err != nil {
  176. return err
  177. }
  178. dst.DestinationPortal = msg.DestinationPortal
  179. dst.PreparedStatement = msg.PreparedStatement
  180. dst.ParameterFormatCodes = msg.ParameterFormatCodes
  181. dst.Parameters = make([][]byte, len(msg.Parameters))
  182. dst.ResultFormatCodes = msg.ResultFormatCodes
  183. for n, parameter := range msg.Parameters {
  184. dst.Parameters[n], err = getValueFromJSON(parameter)
  185. if err != nil {
  186. return fmt.Errorf("cannot get param %d: %w", n, err)
  187. }
  188. }
  189. return nil
  190. }