| 254 | type partialBatchCodec struct{} |
| 255 | |
| 256 | func (p partialBatchCodec) Encode(msg message.Message, dest io.Writer, version primitive.ProtocolVersion) error { |
| 257 | switch batch := msg.(type) { |
| 258 | case *PartialBatch: |
| 259 | if err := primitive.WriteByte(byte(batch.Type), dest); err != nil { |
| 260 | return fmt.Errorf("cannot write BATCH type: %w", err) |
| 261 | } |
| 262 | if err := primitive.WriteShort(uint16(len(batch.Queries)), dest); err != nil { |
| 263 | return fmt.Errorf("cannot write BATCH query count: %w", err) |
| 264 | } |
| 265 | for i, query := range batch.Queries { |
| 266 | switch q := query.QueryOrId.(type) { |
| 267 | case string: |
| 268 | if err := primitive.WriteByte(byte(primitive.BatchChildTypeQueryString), dest); err != nil { |
| 269 | return fmt.Errorf("cannot write BATCH query kind 0 for child #%d: %w", i, err) |
| 270 | } |
| 271 | if err := primitive.WriteLongString(q, dest); err != nil { |
| 272 | return fmt.Errorf("cannot write BATCH query string for child #%d: %w", i, err) |
| 273 | } |
| 274 | case []byte: |
| 275 | if err := primitive.WriteByte(byte(primitive.BatchChildTypePreparedId), dest); err != nil { |
| 276 | return fmt.Errorf("cannot write BATCH query kind 1 for child #%d: %w", i, err) |
| 277 | } |
| 278 | if err := primitive.WriteShortBytes(q, dest); err != nil { |
| 279 | return fmt.Errorf("cannot write BATCH query id for child #%d: %w", i, err) |
| 280 | } |
| 281 | } |
| 282 | if _, err := dest.Write(query.Values); err != nil { |
| 283 | return fmt.Errorf("cannot write BATCH positional values for child #%d: %w", i, err) |
| 284 | } |
| 285 | } |
| 286 | if err := primitive.WriteShort(uint16(batch.Consistency), dest); err != nil { |
| 287 | return fmt.Errorf("cannot write BATCH consistency: %w", err) |
| 288 | } |
| 289 | if _, err := dest.Write(batch.Parameters); err != nil { |
| 290 | return fmt.Errorf("cannot write BATCH parameters: %w", err) |
| 291 | } |
| 292 | return nil |
| 293 | default: |
| 294 | return builtinBatchCodec.Encode(msg, dest, version) |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | func (p partialBatchCodec) EncodedLength(msg message.Message, version primitive.ProtocolVersion) (int, error) { |
| 299 | switch batch := msg.(type) { |