function_call.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package pgproto3
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. "math"
  6. "github.com/jackc/pgx/v5/internal/pgio"
  7. )
  8. type FunctionCall struct {
  9. Function uint32
  10. ArgFormatCodes []uint16
  11. Arguments [][]byte
  12. ResultFormatCode uint16
  13. }
  14. // Frontend identifies this message as sendable by a PostgreSQL frontend.
  15. func (*FunctionCall) Frontend() {}
  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 *FunctionCall) Decode(src []byte) error {
  19. *dst = FunctionCall{}
  20. rp := 0
  21. // Specifies the object ID of the function to call.
  22. dst.Function = binary.BigEndian.Uint32(src[rp:])
  23. rp += 4
  24. // The number of argument format codes that follow (denoted C below).
  25. // This can be zero to indicate that there are no arguments or that the arguments all use the default format (text);
  26. // or one, in which case the specified format code is applied to all arguments;
  27. // or it can equal the actual number of arguments.
  28. nArgumentCodes := int(binary.BigEndian.Uint16(src[rp:]))
  29. rp += 2
  30. argumentCodes := make([]uint16, nArgumentCodes)
  31. for i := range nArgumentCodes {
  32. // The argument format codes. Each must presently be zero (text) or one (binary).
  33. ac := binary.BigEndian.Uint16(src[rp:])
  34. if ac != 0 && ac != 1 {
  35. return &invalidMessageFormatErr{messageType: "FunctionCall"}
  36. }
  37. argumentCodes[i] = ac
  38. rp += 2
  39. }
  40. dst.ArgFormatCodes = argumentCodes
  41. // Specifies the number of arguments being supplied to the function.
  42. nArguments := int(binary.BigEndian.Uint16(src[rp:]))
  43. rp += 2
  44. arguments := make([][]byte, nArguments)
  45. for i := range nArguments {
  46. // The length of the argument value, in bytes (this count does not include itself). Can be zero.
  47. // As a special case, -1 indicates a NULL argument value. No value bytes follow in the NULL case.
  48. argumentLength := int(binary.BigEndian.Uint32(src[rp:]))
  49. rp += 4
  50. if argumentLength == -1 {
  51. arguments[i] = nil
  52. } else {
  53. // The value of the argument, in the format indicated by the associated format code. n is the above length.
  54. argumentValue := src[rp : rp+argumentLength]
  55. rp += argumentLength
  56. arguments[i] = argumentValue
  57. }
  58. }
  59. dst.Arguments = arguments
  60. // The format code for the function result. Must presently be zero (text) or one (binary).
  61. resultFormatCode := binary.BigEndian.Uint16(src[rp:])
  62. if resultFormatCode != 0 && resultFormatCode != 1 {
  63. return &invalidMessageFormatErr{messageType: "FunctionCall"}
  64. }
  65. dst.ResultFormatCode = resultFormatCode
  66. return nil
  67. }
  68. // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
  69. func (src *FunctionCall) Encode(dst []byte) ([]byte, error) {
  70. dst, sp := beginMessage(dst, 'F')
  71. dst = pgio.AppendUint32(dst, src.Function)
  72. if len(src.ArgFormatCodes) > math.MaxUint16 {
  73. return nil, errors.New("too many arg format codes")
  74. }
  75. dst = pgio.AppendUint16(dst, uint16(len(src.ArgFormatCodes)))
  76. for _, argFormatCode := range src.ArgFormatCodes {
  77. dst = pgio.AppendUint16(dst, argFormatCode)
  78. }
  79. if len(src.Arguments) > math.MaxUint16 {
  80. return nil, errors.New("too many arguments")
  81. }
  82. dst = pgio.AppendUint16(dst, uint16(len(src.Arguments)))
  83. for _, argument := range src.Arguments {
  84. if argument == nil {
  85. dst = pgio.AppendInt32(dst, -1)
  86. } else {
  87. dst = pgio.AppendInt32(dst, int32(len(argument)))
  88. dst = append(dst, argument...)
  89. }
  90. }
  91. dst = pgio.AppendUint16(dst, src.ResultFormatCode)
  92. return finishMessage(dst, sp)
  93. }