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