Search in sources :

Example 26 with DataType

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

the class SettableByIndex method setLong.

/**
 * Sets the {@code i}th value to the provided Java primitive long.
 *
 * <p>By default, this works with CQL types {@code bigint} and {@code counter}.
 *
 * <p>To set the value to CQL {@code NULL}, use {@link #setToNull(int)}, or {@code set(i, v,
 * Long.class)}.
 *
 * @throws IndexOutOfBoundsException if the index is invalid.
 */
@NonNull
@CheckReturnValue
default SelfT setLong(int i, long v) {
    DataType cqlType = getType(i);
    TypeCodec<Long> codec = codecRegistry().codecFor(cqlType, Long.class);
    return (codec instanceof PrimitiveLongCodec) ? setBytesUnsafe(i, ((PrimitiveLongCodec) codec).encodePrimitive(v, protocolVersion())) : set(i, v, codec);
}
Also used : PrimitiveLongCodec(com.datastax.oss.driver.api.core.type.codec.PrimitiveLongCodec) DataType(com.datastax.oss.driver.api.core.type.DataType) CheckReturnValue(edu.umd.cs.findbugs.annotations.CheckReturnValue) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 27 with DataType

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

the class SettableByIndex method setBoolean.

/**
 * Sets the {@code i}th value to the provided Java primitive boolean.
 *
 * <p>By default, this works with CQL type {@code boolean}.
 *
 * <p>To set the value to CQL {@code NULL}, use {@link #setToNull(int)}, or {@code set(i, v,
 * Boolean.class)}.
 *
 * @throws IndexOutOfBoundsException if the index is invalid.
 */
@NonNull
@CheckReturnValue
default SelfT setBoolean(int i, boolean v) {
    DataType cqlType = getType(i);
    TypeCodec<Boolean> codec = codecRegistry().codecFor(cqlType, Boolean.class);
    return (codec instanceof PrimitiveBooleanCodec) ? setBytesUnsafe(i, ((PrimitiveBooleanCodec) codec).encodePrimitive(v, protocolVersion())) : set(i, v, codec);
}
Also used : PrimitiveBooleanCodec(com.datastax.oss.driver.api.core.type.codec.PrimitiveBooleanCodec) DataType(com.datastax.oss.driver.api.core.type.DataType) CheckReturnValue(edu.umd.cs.findbugs.annotations.CheckReturnValue) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 28 with DataType

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

the class GettableByIndex method getLong.

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

Example 29 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>This variant is for generic Java types. If the target type is not generic, use {@link
 * #get(int, Class)} instead, which may perform slightly better.
 *
 * @throws IndexOutOfBoundsException if the index is invalid.
 * @throws CodecNotFoundException if no codec can perform the conversion.
 */
@Nullable
default <ValueT> ValueT get(int i, GenericType<ValueT> targetType) {
    DataType cqlType = getType(i);
    TypeCodec<ValueT> codec = codecRegistry().codecFor(cqlType, targetType);
    return get(i, codec);
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType) Nullable(edu.umd.cs.findbugs.annotations.Nullable)

Example 30 with DataType

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

the class GettableByIndex method getShort.

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

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