gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

query.go (1135B)


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