execute.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package pgproto3
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "encoding/json"
  6. "github.com/jackc/pgx/v5/internal/pgio"
  7. )
  8. type Execute struct {
  9. Portal string
  10. MaxRows uint32
  11. }
  12. // Frontend identifies this message as sendable by a PostgreSQL frontend.
  13. func (*Execute) Frontend() {}
  14. // Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message
  15. // type identifier and 4 byte message length.
  16. func (dst *Execute) Decode(src []byte) error {
  17. buf := bytes.NewBuffer(src)
  18. b, err := buf.ReadBytes(0)
  19. if err != nil {
  20. return err
  21. }
  22. dst.Portal = string(b[:len(b)-1])
  23. if buf.Len() < 4 {
  24. return &invalidMessageFormatErr{messageType: "Execute"}
  25. }
  26. dst.MaxRows = binary.BigEndian.Uint32(buf.Next(4))
  27. return nil
  28. }
  29. // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
  30. func (src *Execute) Encode(dst []byte) ([]byte, error) {
  31. dst, sp := beginMessage(dst, 'E')
  32. dst = append(dst, src.Portal...)
  33. dst = append(dst, 0)
  34. dst = pgio.AppendUint32(dst, src.MaxRows)
  35. return finishMessage(dst, sp)
  36. }
  37. // MarshalJSON implements encoding/json.Marshaler.
  38. func (src Execute) MarshalJSON() ([]byte, error) {
  39. return json.Marshal(struct {
  40. Type string
  41. Portal string
  42. MaxRows uint32
  43. }{
  44. Type: "Execute",
  45. Portal: src.Portal,
  46. MaxRows: src.MaxRows,
  47. })
  48. }