package com.datastax.oss.protocol.internal.request;
import static com.datastax.oss.protocol.internal.ProtocolConstants.Version.V5;
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;
public class Prepare extends Message {
public final String cqlQuery;
public final String keyspace;
public Prepare(String cqlQuery, String keyspace) {
super(false, ProtocolConstants.Opcode.PREPARE);
this.cqlQuery = cqlQuery;
this.keyspace = keyspace;
}
public Prepare(String cqlQuery) {
this(cqlQuery, null);
}
@Override
public String toString() {
return "PREPARE(" + cqlQuery + ", " + keyspace + ')';
}
public static class Codec extends Message.Codec {
public Codec(int protocolVersion) {
super(ProtocolConstants.Opcode.PREPARE, protocolVersion);
}
@Override
public <B> void encode(B dest, Message message, PrimitiveCodec<B> encoder) {
Prepare prepare = (Prepare) message;
encoder.writeLongString(prepare.cqlQuery, dest);
if (protocolVersion >= V5) {
encoder.writeInt((prepare.keyspace == null) ? 0x00 : 0x01, dest);
if (prepare.keyspace != null) {
encoder.writeString(prepare.keyspace, dest);
}
}
}
@Override
public int encodedSize(Message message) {
Prepare prepare = (Prepare) message;
int size = PrimitiveSizes.sizeOfLongString(prepare.cqlQuery);
if (protocolVersion >= V5) {
size += PrimitiveSizes.INT;
if (prepare.keyspace != null) {
size += PrimitiveSizes.sizeOfString(prepare.keyspace);
}
}
return size;
}
@Override
public <B> Message decode(B source, PrimitiveCodec<B> decoder) {
String cqlQuery = decoder.readLongString(source);
String keyspace = null;
if (protocolVersion >= V5) {
int flags = decoder.readInt(source);
if ((flags & 0x01) == 0x01) {
keyspace = decoder.readString(source);
}
}
return new Prepare(cqlQuery, keyspace);
}
}
}