Search in sources :

Example 61 with DataType

use of com.datastax.oss.driver.api.core.type.DataType 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)

Example 62 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.

the class AggregateMetadata method describe.

@NonNull
@Override
default String describe(boolean pretty) {
    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());
    }
    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 63 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.

the class FunctionMetadata 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));
    }
    return builder.append(")").increaseIndent().newLine().append(isCalledOnNullInput() ? "CALLED ON NULL INPUT" : "RETURNS NULL ON NULL INPUT").newLine().append("RETURNS ").append(getReturnType().asCql(false, true)).newLine().append("LANGUAGE ").append(getLanguage()).newLine().append("AS '").append(getBody()).append("';").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)

Example 64 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.

the class GettableByIndex method getInt.

/**
 * Returns the {@code i}th value as a Java primitive integer.
 *
 * <p>By default, this works with CQL type {@code int}.
 *
 * <p>Note that, due to its signature, this method cannot return {@code null}. If the CQL value is
 * {@code NULL}, it will return {@code 0}. If this doesn't work for you, either call {@link
 * #isNull(int)} before calling this method, or use {@code get(i, Integer.class)} instead.
 *
 * @throws IndexOutOfBoundsException if the index is invalid.
 */
default int getInt(int i) {
    DataType cqlType = getType(i);
    TypeCodec<Integer> codec = codecRegistry().codecFor(cqlType, Integer.class);
    if (codec instanceof PrimitiveIntCodec) {
        return ((PrimitiveIntCodec) codec).decodePrimitive(getBytesUnsafe(i), protocolVersion());
    } else {
        Integer value = get(i, codec);
        return value == null ? 0 : value;
    }
}
Also used : BigInteger(java.math.BigInteger) DataType(com.datastax.oss.driver.api.core.type.DataType) PrimitiveIntCodec(com.datastax.oss.driver.api.core.type.codec.PrimitiveIntCodec)

Example 65 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.

the class GettableByIndex method getObject.

/**
 * Returns the {@code i}th value, converting it to the most appropriate Java type.
 *
 * <p>The {@link #codecRegistry()} will be used to look up a codec to handle the conversion.
 *
 * <p>Use this method to dynamically inspect elements when types aren't known in advance, for
 * instance if you're writing a generic row logger. If you know the target Java type, it is
 * generally preferable to use typed variants, such as the ones for built-in types ({@link
 * #getBoolean(int)}, {@link #getInt(int)}, etc.), or {@link #get(int, Class)} and {@link
 * #get(int, GenericType)} for custom types.
 *
 * <p>The definition of "most appropriate" is unspecified, and left to the appreciation of the
 * {@link #codecRegistry()} implementation. By default, the driver uses the mapping described in
 * the other {@code getXxx()} methods (for example {@link #getString(int) String for text, varchar
 * and ascii}, etc).
 *
 * @throws IndexOutOfBoundsException if the index is invalid.
 * @throws CodecNotFoundException if no codec can perform the conversion.
 */
@Nullable
default Object getObject(int i) {
    DataType cqlType = getType(i);
    TypeCodec<?> codec = codecRegistry().codecFor(cqlType);
    return codec.decode(getBytesUnsafe(i), protocolVersion());
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType) Nullable(edu.umd.cs.findbugs.annotations.Nullable)

Aggregations

DataType (com.datastax.oss.driver.api.core.type.DataType)73 NonNull (edu.umd.cs.findbugs.annotations.NonNull)21 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)19 SetType (com.datastax.oss.driver.api.core.type.SetType)10 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)10 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)10 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)9 CheckReturnValue (edu.umd.cs.findbugs.annotations.CheckReturnValue)9 ColumnMetadata (com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata)8 Map (java.util.Map)8 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)7 ClusteringOrder (com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)7 ListType (com.datastax.oss.driver.api.core.type.ListType)7 MapType (com.datastax.oss.driver.api.core.type.MapType)7 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)6 Nullable (edu.umd.cs.findbugs.annotations.Nullable)6 UUID (java.util.UUID)6 TupleType (com.datastax.oss.driver.api.core.type.TupleType)5 CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)5 List (java.util.List)5