io.stargate.sgv2.graphql.schema.cqlfirst.dml.fetchers.DmlFetcher Maven / Gradle / Ivy
package io.stargate.sgv2.graphql.schema.cqlfirst.dml.fetchers;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Streams;
import com.google.protobuf.Int32Value;
import graphql.language.OperationDefinition;
import graphql.schema.DataFetchingEnvironment;
import io.stargate.bridge.grpc.BytesValues;
import io.stargate.bridge.proto.QueryOuterClass;
import io.stargate.bridge.proto.QueryOuterClass.ColumnSpec;
import io.stargate.bridge.proto.QueryOuterClass.Consistency;
import io.stargate.bridge.proto.QueryOuterClass.ConsistencyValue;
import io.stargate.bridge.proto.QueryOuterClass.Query;
import io.stargate.bridge.proto.QueryOuterClass.QueryParameters;
import io.stargate.bridge.proto.QueryOuterClass.Response;
import io.stargate.bridge.proto.QueryOuterClass.ResultSet;
import io.stargate.bridge.proto.QueryOuterClass.Row;
import io.stargate.bridge.proto.QueryOuterClass.Value;
import io.stargate.bridge.proto.Schema.CqlTable;
import io.stargate.sgv2.common.cql.builder.BuiltCondition;
import io.stargate.sgv2.common.cql.builder.Predicate;
import io.stargate.sgv2.common.grpc.proto.Rows;
import io.stargate.sgv2.graphql.schema.CassandraFetcher;
import io.stargate.sgv2.graphql.schema.SchemaConstants;
import io.stargate.sgv2.graphql.schema.cqlfirst.dml.NameMapping;
import io.stargate.sgv2.graphql.web.resources.StargateGraphqlContext;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class DmlFetcher extends CassandraFetcher {
private static final Logger LOG = LoggerFactory.getLogger(DmlFetcher.class);
protected final String keyspaceName;
protected final CqlTable table;
protected final NameMapping nameMapping;
protected final DbColumnGetter dbColumnGetter;
protected DmlFetcher(String keyspaceName, CqlTable table, NameMapping nameMapping) {
this.keyspaceName = keyspaceName;
this.table = table;
this.nameMapping = nameMapping;
this.dbColumnGetter = new DbColumnGetter(nameMapping);
}
protected QueryParameters buildParameters(DataFetchingEnvironment environment) {
Map options = environment.getArgument("options");
if (options == null) {
return DEFAULT_PARAMETERS;
}
QueryParameters.Builder builder = DEFAULT_PARAMETERS.toBuilder();
Object consistency = options.get("consistency");
if (consistency != null) {
builder.setConsistency(
ConsistencyValue.newBuilder().setValue(Consistency.valueOf((String) consistency)));
}
Object serialConsistency = options.get("serialConsistency");
if (serialConsistency != null) {
builder.setSerialConsistency(
ConsistencyValue.newBuilder().setValue(Consistency.valueOf((String) serialConsistency)));
}
Object pageSize = options.get("pageSize");
if (pageSize != null) {
builder.setPageSize(Int32Value.of((Integer) pageSize));
}
Object pageState = options.get("pageState");
if (pageState != null) {
builder.setPagingState(BytesValues.ofBase64((String) pageState));
}
return builder.build();
}
protected List buildConditions(Map> columnList) {
if (columnList == null) {
return ImmutableList.of();
}
List where = new ArrayList<>();
for (Map.Entry> clauseEntry : columnList.entrySet()) {
ColumnSpec column = dbColumnGetter.getColumn(table, clauseEntry.getKey());
for (Map.Entry condition : clauseEntry.getValue().entrySet()) {
FilterOperator operator = FilterOperator.fromFieldName(condition.getKey());
where.add(operator.buildCondition(column, condition.getValue(), nameMapping));
}
}
return where;
}
protected List buildClause(CqlTable table, DataFetchingEnvironment environment) {
if (environment.containsArgument("filter")) {
Map> columnList = environment.getArgument("filter");
return buildConditions(columnList);
} else {
Map value = environment.getArgument("value");
if (value == null) {
return ImmutableList.of();
}
List relations = new ArrayList<>();
for (Map.Entry entry : value.entrySet()) {
ColumnSpec column = dbColumnGetter.getColumn(table, entry.getKey());
Value whereValue = toGrpcValue(column.getType(), entry.getValue());
relations.add(BuiltCondition.of(column.getName(), Predicate.EQ, whereValue));
}
return relations;
}
}
protected boolean containsDirective(OperationDefinition operation, String directive) {
return operation.getDirectives().stream().anyMatch(d -> d.getName().equals(directive));
}
protected CompletableFuture
© 2015 - 2024 Weber Informatics LLC | Privacy Policy