package com.datastax.oss.protocol.internal.request;
import com.datastax.oss.protocol.internal.Message;
import com.datastax.oss.protocol.internal.PrimitiveCodec;
import com.datastax.oss.protocol.internal.PrimitiveSizes;
import com.datastax.oss.protocol.internal.ProtocolConstants;
import com.datastax.oss.protocol.internal.request.query.QueryOptions;
public class Query extends Message {
public final String query;
public final QueryOptions options;
public Query(String query, QueryOptions options) {
super(false, ProtocolConstants.Opcode.QUERY);
this.query = query;
this.options = options;
}
public Query(String query) {
this(query, QueryOptions.DEFAULT);
}
@Override
public String toString() {
return "QUERY (" + query + ')';
}
public static class Codec extends Message.Codec {
protected final QueryOptions.Codec optionsCodec;
public Codec(int protocolVersion, QueryOptions.Codec optionsCodec) {
super(ProtocolConstants.Opcode.QUERY, protocolVersion);
this.optionsCodec = optionsCodec;
}
public Codec(int protocolVersion) {
this(protocolVersion, new QueryOptions.Codec(protocolVersion));
}
@Override
public <B> void encode(B dest, Message message, PrimitiveCodec<B> encoder) {
Query query = (Query) message;
encoder.writeLongString(query.query, dest);
optionsCodec.encode(dest, query.options, encoder);
}
@Override
public int encodedSize(Message message) {
Query query = (Query) message;
return PrimitiveSizes.sizeOfLongString(query.query) + optionsCodec.encodedSize(query.options);
}
@Override
public <B> Message decode(B source, PrimitiveCodec<B> decoder) {
String query = decoder.readLongString(source);
QueryOptions options = optionsCodec.decode(source, decoder);
return new Query(query, options);
}
}
}