Search in sources :

Example 1 with ScriptBuilder

use of com.datastax.oss.driver.internal.core.metadata.schema.ScriptBuilder in project java-driver by datastax.

the class DseAggregateMetadata method describe.

@NonNull
@Override
default String describe(boolean pretty) {
    // Easiest to just copy the OSS describe() method and add in DETERMINISTIC
    ScriptBuilder builder = new ScriptBuilder(pretty);
    builder.append("CREATE AGGREGATE ").append(getKeyspace()).append(".").append(getSignature().getName()).append("(");
    boolean first = true;
    for (int i = 0; i < getSignature().getParameterTypes().size(); i++) {
        if (first) {
            first = false;
        } else {
            builder.append(",");
        }
        DataType type = getSignature().getParameterTypes().get(i);
        builder.append(type.asCql(false, pretty));
    }
    builder.increaseIndent().append(")").newLine().append("SFUNC ").append(getStateFuncSignature().getName()).newLine().append("STYPE ").append(getStateType().asCql(false, pretty));
    if (getFinalFuncSignature().isPresent()) {
        builder.newLine().append("FINALFUNC ").append(getFinalFuncSignature().get().getName());
    }
    if (getInitCond().isPresent()) {
        Optional<String> formatInitCond = formatInitCond();
        assert formatInitCond.isPresent();
        builder.newLine().append("INITCOND ").append(formatInitCond.get());
    }
    // add DETERMINISTIC if present
    if (getDeterministic().orElse(false)) {
        builder.newLine().append("DETERMINISTIC");
    }
    return builder.append(";").build();
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType) ScriptBuilder(com.datastax.oss.driver.internal.core.metadata.schema.ScriptBuilder) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 2 with ScriptBuilder

use of com.datastax.oss.driver.internal.core.metadata.schema.ScriptBuilder in project java-driver by datastax.

the class DseGraphKeyspaceMetadata method describe.

@NonNull
@Override
default String describe(boolean pretty) {
    ScriptBuilder builder = new ScriptBuilder(pretty);
    if (isVirtual()) {
        builder.append("/* VIRTUAL ");
    } else {
        builder.append("CREATE ");
    }
    builder.append("KEYSPACE ").append(getName()).append(" WITH replication = { 'class' : '").append(getReplication().get("class")).append("'");
    for (Map.Entry<String, String> entry : getReplication().entrySet()) {
        if (!entry.getKey().equals("class")) {
            builder.append(", '").append(entry.getKey()).append("': '").append(entry.getValue()).append("'");
        }
    }
    builder.append(" } AND durable_writes = ").append(Boolean.toString(isDurableWrites()));
    getGraphEngine().ifPresent(graphEngine -> builder.append(" AND graph_engine ='").append(graphEngine).append("'"));
    builder.append(";");
    if (isVirtual()) {
        builder.append(" */");
    }
    return builder.build();
}
Also used : ScriptBuilder(com.datastax.oss.driver.internal.core.metadata.schema.ScriptBuilder) Map(java.util.Map) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 3 with ScriptBuilder

use of com.datastax.oss.driver.internal.core.metadata.schema.ScriptBuilder in project java-driver by datastax.

the class TableMetadata method describe.

@NonNull
@Override
default String describe(boolean pretty) {
    ScriptBuilder builder = new ScriptBuilder(pretty);
    if (isVirtual()) {
        builder.append("/* VIRTUAL ");
    } else {
        builder.append("CREATE ");
    }
    builder.append("TABLE ").append(getKeyspace()).append(".").append(getName()).append(" (").newLine().increaseIndent();
    for (ColumnMetadata column : getColumns().values()) {
        builder.append(column.getName()).append(" ").append(column.getType().asCql(true, pretty));
        if (column.isStatic()) {
            builder.append(" static");
        }
        builder.append(",").newLine();
    }
    // PK
    builder.append("PRIMARY KEY (");
    if (getPartitionKey().size() == 1) {
        // PRIMARY KEY (k
        builder.append(getPartitionKey().get(0).getName());
    } else {
        // PRIMARY KEY ((k1, k2)
        builder.append("(");
        boolean first = true;
        for (ColumnMetadata pkColumn : getPartitionKey()) {
            if (first) {
                first = false;
            } else {
                builder.append(", ");
            }
            builder.append(pkColumn.getName());
        }
        builder.append(")");
    }
    // PRIMARY KEY (<pk portion>, cc1, cc2, cc3)
    for (ColumnMetadata clusteringColumn : getClusteringColumns().keySet()) {
        builder.append(", ").append(clusteringColumn.getName());
    }
    builder.append(")");
    builder.newLine().decreaseIndent().append(")");
    builder.increaseIndent();
    if (isCompactStorage()) {
        builder.andWith().append("COMPACT STORAGE");
    }
    if (getClusteringColumns().containsValue(ClusteringOrder.DESC)) {
        builder.andWith().append("CLUSTERING ORDER BY (");
        boolean first = true;
        for (Map.Entry<ColumnMetadata, ClusteringOrder> entry : getClusteringColumns().entrySet()) {
            if (first) {
                first = false;
            } else {
                builder.append(", ");
            }
            builder.append(entry.getKey().getName()).append(" ").append(entry.getValue().name());
        }
        builder.append(")");
    }
    Map<CqlIdentifier, Object> options = getOptions();
    RelationParser.appendOptions(options, builder);
    builder.append(";");
    if (isVirtual()) {
        builder.append(" */");
    }
    return builder.build();
}
Also used : ScriptBuilder(com.datastax.oss.driver.internal.core.metadata.schema.ScriptBuilder) Map(java.util.Map) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 4 with ScriptBuilder

use of com.datastax.oss.driver.internal.core.metadata.schema.ScriptBuilder in project java-driver by datastax.

the class ViewMetadata method describe.

@NonNull
@Override
default String describe(boolean pretty) {
    ScriptBuilder builder = new ScriptBuilder(pretty).append("CREATE MATERIALIZED VIEW ").append(getKeyspace()).append(".").append(getName()).append(" AS").newLine();
    builder.append("SELECT");
    if (includesAllColumns()) {
        builder.append(" * ");
    } else {
        builder.newLine().increaseIndent();
        boolean first = true;
        for (ColumnMetadata column : getColumns().values()) {
            if (first) {
                first = false;
            } else {
                builder.append(",").newLine();
            }
            builder.append(column.getName());
        }
        builder.newLine().decreaseIndent();
    }
    builder.append("FROM ").append(getKeyspace()).append(".").append(getBaseTable());
    Optional<String> whereClause = getWhereClause();
    if (whereClause.isPresent() && !whereClause.get().isEmpty()) {
        builder.newLine().append("WHERE ").append(whereClause.get());
    }
    builder.newLine().append("PRIMARY KEY (");
    if (getPartitionKey().size() == 1) {
        // PRIMARY KEY (k
        builder.append(getPartitionKey().get(0).getName());
    } else {
        // PRIMARY KEY ((k1, k2)
        builder.append("(");
        boolean first = true;
        for (ColumnMetadata pkColumn : getPartitionKey()) {
            if (first) {
                first = false;
            } else {
                builder.append(", ");
            }
            builder.append(pkColumn.getName());
        }
        builder.append(")");
    }
    // PRIMARY KEY (<pk portion>, cc1, cc2, cc3)
    for (ColumnMetadata clusteringColumn : getClusteringColumns().keySet()) {
        builder.append(", ").append(clusteringColumn.getName());
    }
    builder.append(")").increaseIndent();
    RelationParser.appendOptions(getOptions(), builder);
    return builder.append(";").build();
}
Also used : ScriptBuilder(com.datastax.oss.driver.internal.core.metadata.schema.ScriptBuilder) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 5 with ScriptBuilder

use of com.datastax.oss.driver.internal.core.metadata.schema.ScriptBuilder in project java-driver by datastax.

the class DseFunctionMetadata method describe.

@NonNull
@Override
default String describe(boolean pretty) {
    ScriptBuilder builder = new ScriptBuilder(pretty);
    builder.append("CREATE FUNCTION ").append(getKeyspace()).append(".").append(getSignature().getName()).append("(");
    boolean first = true;
    for (int i = 0; i < getSignature().getParameterTypes().size(); i++) {
        if (first) {
            first = false;
        } else {
            builder.append(",");
        }
        DataType type = getSignature().getParameterTypes().get(i);
        CqlIdentifier name = getParameterNames().get(i);
        builder.append(name).append(" ").append(type.asCql(false, pretty));
    }
    builder.append(")").increaseIndent().newLine().append(isCalledOnNullInput() ? "CALLED ON NULL INPUT" : "RETURNS NULL ON NULL INPUT").newLine().append("RETURNS ").append(getReturnType().asCql(false, true)).newLine();
    // handle deterministic and monotonic
    if (getDeterministic().orElse(false)) {
        builder.append("DETERMINISTIC").newLine();
    }
    if (getMonotonicity().isPresent()) {
        switch(getMonotonicity().get()) {
            case FULLY_MONOTONIC:
                builder.append("MONOTONIC").newLine();
                break;
            case PARTIALLY_MONOTONIC:
                builder.append("MONOTONIC ON ").append(getMonotonicArgumentNames().get(0)).newLine();
                break;
            default:
                break;
        }
    }
    builder.append("LANGUAGE ").append(getLanguage()).newLine().append("AS '").append(getBody()).append("';");
    return builder.build();
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType) ScriptBuilder(com.datastax.oss.driver.internal.core.metadata.schema.ScriptBuilder) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Aggregations

ScriptBuilder (com.datastax.oss.driver.internal.core.metadata.schema.ScriptBuilder)13 NonNull (edu.umd.cs.findbugs.annotations.NonNull)13 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)5 Map (java.util.Map)5 DataType (com.datastax.oss.driver.api.core.type.DataType)4 AttachmentPoint (com.datastax.oss.driver.api.core.detach.AttachmentPoint)1 ClusteringOrder (com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)1 ColumnMetadata (com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata)1 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)1