package com.datastax.oss.driver.api.core.cql;
import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.internal.core.cql.DefaultSimpleStatement;
import com.datastax.oss.protocol.internal.util.collection.NullAllowingImmutableList;
import com.datastax.oss.protocol.internal.util.collection.NullAllowingImmutableMap;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import net.jcip.annotations.NotThreadSafe;
@NotThreadSafe
public class SimpleStatementBuilder
extends StatementBuilder<SimpleStatementBuilder, SimpleStatement> {
@NonNull private String query;
@Nullable private CqlIdentifier keyspace;
@Nullable private NullAllowingImmutableList.Builder<Object> positionalValuesBuilder;
@Nullable private NullAllowingImmutableMap.Builder<CqlIdentifier, Object> namedValuesBuilder;
public SimpleStatementBuilder(@NonNull String query) {
this.query = query;
}
public SimpleStatementBuilder(@NonNull SimpleStatement template) {
super(template);
if (!template.getPositionalValues().isEmpty() && !template.getNamedValues().isEmpty()) {
throw new IllegalArgumentException(
"Illegal statement to copy, can't have both named and positional values");
}
this.query = template.getQuery();
if (!template.getPositionalValues().isEmpty()) {
this.positionalValuesBuilder =
NullAllowingImmutableList.builder(template.getPositionalValues().size())
.addAll(template.getPositionalValues());
}
if (!template.getNamedValues().isEmpty()) {
this.namedValuesBuilder =
NullAllowingImmutableMap.<CqlIdentifier, Object>builder(template.getNamedValues().size())
.putAll(template.getNamedValues());
}
}
@NonNull
public SimpleStatementBuilder setQuery(@NonNull String query) {
this.query = query;
return this;
}
@NonNull
public SimpleStatementBuilder setKeyspace(@Nullable CqlIdentifier keyspace) {
this.keyspace = keyspace;
return this;
}
@NonNull
public SimpleStatementBuilder setKeyspace(@Nullable String keyspaceName) {
return setKeyspace(keyspaceName == null ? null : CqlIdentifier.fromCql(keyspaceName));
}
@NonNull
public SimpleStatementBuilder addPositionalValue(@Nullable Object value) {
if (namedValuesBuilder != null) {
throw new IllegalArgumentException(
"Can't have both positional and named values in a statement.");
}
if (positionalValuesBuilder == null) {
positionalValuesBuilder = NullAllowingImmutableList.builder();
}
positionalValuesBuilder.add(value);
return this;
}
@NonNull
public SimpleStatementBuilder addPositionalValues(@NonNull Iterable<Object> values) {
if (namedValuesBuilder != null) {
throw new IllegalArgumentException(
"Can't have both positional and named values in a statement.");
}
if (positionalValuesBuilder == null) {
positionalValuesBuilder = NullAllowingImmutableList.builder();
}
positionalValuesBuilder.addAll(values);
return this;
}
@NonNull
public SimpleStatementBuilder addPositionalValues(@NonNull Object... values) {
return addPositionalValues(Arrays.asList(values));
}
@NonNull
public SimpleStatementBuilder clearPositionalValues() {
positionalValuesBuilder = NullAllowingImmutableList.builder();
return this;
}
@NonNull
public SimpleStatementBuilder addNamedValue(@NonNull CqlIdentifier name, @Nullable Object value) {
if (positionalValuesBuilder != null) {
throw new IllegalArgumentException(
"Can't have both positional and named values in a statement.");
}
if (namedValuesBuilder == null) {
namedValuesBuilder = NullAllowingImmutableMap.builder();
}
namedValuesBuilder.put(name, value);
return this;
}
@NonNull
public SimpleStatementBuilder addNamedValue(@NonNull String name, @Nullable Object value) {
return addNamedValue(CqlIdentifier.fromCql(name), value);
}
@NonNull
public SimpleStatementBuilder clearNamedValues() {
namedValuesBuilder = NullAllowingImmutableMap.builder();
return this;
}
@NonNull
@Override
public SimpleStatement build() {
return new DefaultSimpleStatement(
query,
(positionalValuesBuilder == null)
? NullAllowingImmutableList.of()
: positionalValuesBuilder.build(),
(namedValuesBuilder == null) ? NullAllowingImmutableMap.of() : namedValuesBuilder.build(),
executionProfileName,
executionProfile,
keyspace,
routingKeyspace,
routingKey,
routingToken,
buildCustomPayload(),
idempotent,
tracing,
timestamp,
pagingState,
pageSize,
consistencyLevel,
serialConsistencyLevel,
timeout,
node,
nowInSeconds);
}
}