github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/message/execute.go (about) 1 // Copyright 2020 DataStax 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package message 16 17 import ( 18 "encoding/hex" 19 "errors" 20 "fmt" 21 "io" 22 23 "github.com/datastax/go-cassandra-native-protocol/primitive" 24 ) 25 26 // Execute is a request message that executes a prepared statement, identified by its prepared query id. 27 // +k8s:deepcopy-gen=true 28 // +k8s:deepcopy-gen:interfaces=github.com/datastax/go-cassandra-native-protocol/message.Message 29 type Execute struct { 30 // QueryId is the prepared query id to execute. 31 QueryId []byte 32 // the ID of the result set metadata that was sent along with response to PREPARE message. 33 // Valid in protocol version 5 and DSE protocol version 2. See PreparedResult. 34 ResultMetadataId []byte 35 Options *QueryOptions 36 } 37 38 func (m *Execute) IsResponse() bool { 39 return false 40 } 41 42 func (m *Execute) GetOpCode() primitive.OpCode { 43 return primitive.OpCodeExecute 44 } 45 46 func (m *Execute) String() string { 47 return "EXECUTE " + hex.EncodeToString(m.QueryId) 48 } 49 50 type executeCodec struct{} 51 52 func (c *executeCodec) Encode(msg Message, dest io.Writer, version primitive.ProtocolVersion) error { 53 execute, ok := msg.(*Execute) 54 if !ok { 55 return errors.New(fmt.Sprintf("expected *message.Execute, got %T", msg)) 56 } 57 if len(execute.QueryId) == 0 { 58 return errors.New("EXECUTE missing query id") 59 } else if err := primitive.WriteShortBytes(execute.QueryId, dest); err != nil { 60 return fmt.Errorf("cannot write EXECUTE query id: %w", err) 61 } 62 if version.SupportsResultMetadataId() { 63 if len(execute.ResultMetadataId) == 0 { 64 return errors.New("EXECUTE missing result metadata id") 65 } else if err := primitive.WriteShortBytes(execute.ResultMetadataId, dest); err != nil { 66 return fmt.Errorf("cannot write EXECUTE result metadata id: %w", err) 67 } 68 } 69 if err := EncodeQueryOptions(execute.Options, dest, version); err != nil { 70 return fmt.Errorf("cannot write EXECUTE options: %w", err) 71 } 72 return nil 73 } 74 75 func (c *executeCodec) EncodedLength(msg Message, version primitive.ProtocolVersion) (size int, err error) { 76 execute, ok := msg.(*Execute) 77 if !ok { 78 return -1, errors.New(fmt.Sprintf("expected *message.Execute, got %T", msg)) 79 } 80 size += primitive.LengthOfShortBytes(execute.QueryId) 81 if version.SupportsResultMetadataId() { 82 size += primitive.LengthOfShortBytes(execute.ResultMetadataId) 83 } 84 if lengthOfQueryOptions, err := LengthOfQueryOptions(execute.Options, version); err == nil { 85 return size + lengthOfQueryOptions, nil 86 } else { 87 return -1, fmt.Errorf("cannot compute size EXECUTE query options: %w", err) 88 } 89 } 90 91 func (c *executeCodec) Decode(source io.Reader, version primitive.ProtocolVersion) (msg Message, err error) { 92 var execute = &Execute{ 93 Options: nil, 94 } 95 if execute.QueryId, err = primitive.ReadShortBytes(source); err != nil { 96 return nil, fmt.Errorf("cannot read EXECUTE query id: %w", err) 97 } else if len(execute.QueryId) == 0 { 98 return nil, errors.New("EXECUTE missing query id") 99 } 100 if version.SupportsResultMetadataId() { 101 if execute.ResultMetadataId, err = primitive.ReadShortBytes(source); err != nil { 102 return nil, fmt.Errorf("cannot read EXECUTE result metadata id: %w", err) 103 } else if len(execute.ResultMetadataId) == 0 { 104 return nil, errors.New("EXECUTE missing result metadata id") 105 } 106 } 107 if execute.Options, err = DecodeQueryOptions(source, version); err != nil { 108 return nil, fmt.Errorf("cannot read EXECUTE query options: %w", err) 109 } 110 return execute, nil 111 } 112 113 func (c *executeCodec) GetOpCode() primitive.OpCode { 114 return primitive.OpCodeExecute 115 }