Search in sources :

Example 31 with DataType

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

the class GettableByIndex method get.

/**
 * Returns the {@code i}th value, converting it to the given Java type.
 *
 * <p>The {@link #codecRegistry()} will be used to look up a codec to handle the conversion.
 *
 * <p>If the target type is generic, use {@link #get(int, GenericType)} instead.
 *
 * @throws IndexOutOfBoundsException if the index is invalid.
 * @throws CodecNotFoundException if no codec can perform the conversion.
 */
@Nullable
default <ValueT> ValueT get(int i, Class<ValueT> targetClass) {
    // This is duplicated from the GenericType variant, because we want to give the codec registry
    // a chance to process the unwrapped class directly, if it can do so in a more efficient way.
    DataType cqlType = getType(i);
    TypeCodec<ValueT> codec = codecRegistry().codecFor(cqlType, targetClass);
    return get(i, codec);
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType) Nullable(edu.umd.cs.findbugs.annotations.Nullable)

Example 32 with DataType

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

the class GettableByIndex method getFloat.

/**
 * Returns the {@code i}th value as a Java primitive float.
 *
 * <p>By default, this works with CQL type {@code float}.
 *
 * <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.0}. If this doesn't work for you, either call {@link
 * #isNull(int)} before calling this method, or use {@code get(i, Float.class)} instead.
 *
 * @throws IndexOutOfBoundsException if the index is invalid.
 */
default float getFloat(int i) {
    DataType cqlType = getType(i);
    TypeCodec<Float> codec = codecRegistry().codecFor(cqlType, Float.class);
    if (codec instanceof PrimitiveFloatCodec) {
        return ((PrimitiveFloatCodec) codec).decodePrimitive(getBytesUnsafe(i), protocolVersion());
    } else {
        Float value = get(i, codec);
        return value == null ? 0 : value;
    }
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType) PrimitiveFloatCodec(com.datastax.oss.driver.api.core.type.codec.PrimitiveFloatCodec)

Example 33 with DataType

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

the class GettableByIndex method getByte.

/**
 * Returns the {@code i}th value as a Java primitive byte.
 *
 * <p>By default, this works with CQL type {@code tinyint}.
 *
 * <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, Byte.class)} instead.
 *
 * @throws IndexOutOfBoundsException if the index is invalid.
 */
default byte getByte(int i) {
    DataType cqlType = getType(i);
    TypeCodec<Byte> codec = codecRegistry().codecFor(cqlType, Byte.class);
    if (codec instanceof PrimitiveByteCodec) {
        return ((PrimitiveByteCodec) codec).decodePrimitive(getBytesUnsafe(i), protocolVersion());
    } else {
        Byte value = get(i, codec);
        return value == null ? 0 : value;
    }
}
Also used : PrimitiveByteCodec(com.datastax.oss.driver.api.core.type.codec.PrimitiveByteCodec) DataType(com.datastax.oss.driver.api.core.type.DataType)

Example 34 with DataType

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

the class FunctionSignature method toString.

@Override
public String toString() {
    StringBuilder builder = new StringBuilder(name.asInternal()).append('(');
    boolean first = true;
    for (DataType type : parameterTypes) {
        if (first) {
            first = false;
        } else {
            builder.append(", ");
        }
        builder.append(type.asCql(true, true));
    }
    return builder.append(')').toString();
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType)

Example 35 with DataType

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

the class TableParser method parseVirtualTable.

TableMetadata parseVirtualTable(AdminRow tableRow, CqlIdentifier keyspaceId, Map<CqlIdentifier, UserDefinedType> userTypes) {
    CqlIdentifier tableId = CqlIdentifier.fromInternal(tableRow.getString("table_name"));
    List<RawColumn> rawColumns = RawColumn.toRawColumns(rows.virtualColumns().getOrDefault(keyspaceId, ImmutableMultimap.of()).get(tableId));
    if (rawColumns.isEmpty()) {
        LOG.warn("[{}] Processing TABLE refresh for {}.{} but found no matching rows, skipping", logPrefix, keyspaceId, tableId);
        return null;
    }
    Collections.sort(rawColumns);
    ImmutableMap.Builder<CqlIdentifier, ColumnMetadata> allColumnsBuilder = ImmutableMap.builder();
    ImmutableList.Builder<ColumnMetadata> partitionKeyBuilder = ImmutableList.builder();
    ImmutableMap.Builder<ColumnMetadata, ClusteringOrder> clusteringColumnsBuilder = ImmutableMap.builder();
    for (RawColumn raw : rawColumns) {
        DataType dataType = rows.dataTypeParser().parse(keyspaceId, raw.dataType, userTypes, context);
        ColumnMetadata column = new DefaultColumnMetadata(keyspaceId, tableId, raw.name, dataType, raw.kind.equals(RawColumn.KIND_STATIC));
        switch(raw.kind) {
            case RawColumn.KIND_PARTITION_KEY:
                partitionKeyBuilder.add(column);
                break;
            case RawColumn.KIND_CLUSTERING_COLUMN:
                clusteringColumnsBuilder.put(column, raw.reversed ? ClusteringOrder.DESC : ClusteringOrder.ASC);
                break;
            default:
        }
        allColumnsBuilder.put(column.getName(), column);
    }
    return new DefaultTableMetadata(keyspaceId, tableId, null, false, true, partitionKeyBuilder.build(), clusteringColumnsBuilder.build(), allColumnsBuilder.build(), Collections.emptyMap(), Collections.emptyMap());
}
Also used : DefaultColumnMetadata(com.datastax.oss.driver.internal.core.metadata.schema.DefaultColumnMetadata) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) DefaultColumnMetadata(com.datastax.oss.driver.internal.core.metadata.schema.DefaultColumnMetadata) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) DefaultTableMetadata(com.datastax.oss.driver.internal.core.metadata.schema.DefaultTableMetadata) DataType(com.datastax.oss.driver.api.core.type.DataType) ClusteringOrder(com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)

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