Search in sources :

Example 66 with DataType

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

the class GettableByIndex method getBoolean.

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

Example 67 with DataType

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

the class GettableByIndex method getDouble.

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

Example 68 with DataType

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

the class UserDefinedTypeParser method parseType.

private UserDefinedType parseType(AdminRow row, CqlIdentifier keyspaceId, Map<CqlIdentifier, UserDefinedType> userDefinedTypes) {
    // Cassandra < 3.0:
    // CREATE TABLE system.schema_usertypes (
    // keyspace_name text,
    // type_name text,
    // field_names list<text>,
    // field_types list<text>,
    // PRIMARY KEY (keyspace_name, type_name)
    // ) WITH CLUSTERING ORDER BY (type_name ASC)
    // 
    // Cassandra >= 3.0:
    // CREATE TABLE system_schema.types (
    // keyspace_name text,
    // type_name text,
    // field_names frozen<list<text>>,
    // field_types frozen<list<text>>,
    // PRIMARY KEY (keyspace_name, type_name)
    // ) WITH CLUSTERING ORDER BY (type_name ASC)
    CqlIdentifier name = CqlIdentifier.fromInternal(row.getString("type_name"));
    List<CqlIdentifier> fieldNames = ImmutableList.copyOf(Lists.transform(row.getListOfString("field_names"), CqlIdentifier::fromInternal));
    List<DataType> fieldTypes = dataTypeParser.parse(keyspaceId, row.getListOfString("field_types"), userDefinedTypes, context);
    return new DefaultUserDefinedType(keyspaceId, name, false, fieldNames, fieldTypes, context);
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) DefaultUserDefinedType(com.datastax.oss.driver.internal.core.type.DefaultUserDefinedType)

Example 69 with DataType

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

the class DataTypeDetachableTest method simple_types_should_never_be_detached.

@Test
public void simple_types_should_never_be_detached() {
    // default
    for (DataType simpleType : ImmutableList.of(DataTypes.INT, DataTypes.custom("some.class"))) {
        assertThat(simpleType.isDetached()).isFalse();
        assertThat(SerializationHelper.serializeAndDeserialize(simpleType).isDetached()).isFalse();
    }
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType) Test(org.junit.Test)

Example 70 with DataType

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

the class RequestLogFormatterTest method mockPreparedStatement.

private PreparedStatement mockPreparedStatement(String query, Map<String, DataType> variables) {
    ImmutableList.Builder<ColumnDefinition> definitions = ImmutableList.builder();
    int i = 0;
    for (Map.Entry<String, DataType> entry : variables.entrySet()) {
        definitions.add(new DefaultColumnDefinition(new ColumnSpec("test", "foo", entry.getKey(), i, RawType.PRIMITIVES.get(entry.getValue().getProtocolCode())), context));
    }
    return new DefaultPreparedStatement(Bytes.fromHexString("0x"), query, DefaultColumnDefinitions.valueOf(definitions.build()), Collections.emptyList(), null, null, null, Collections.emptyMap(), null, null, null, null, null, Collections.emptyMap(), null, null, null, Integer.MIN_VALUE, null, null, false, context.getCodecRegistry(), context.getProtocolVersion());
}
Also used : DefaultPreparedStatement(com.datastax.oss.driver.internal.core.cql.DefaultPreparedStatement) ColumnSpec(com.datastax.oss.protocol.internal.response.result.ColumnSpec) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) DefaultColumnDefinition(com.datastax.oss.driver.internal.core.cql.DefaultColumnDefinition) DataType(com.datastax.oss.driver.api.core.type.DataType) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) Map(java.util.Map) DefaultColumnDefinition(com.datastax.oss.driver.internal.core.cql.DefaultColumnDefinition) ColumnDefinition(com.datastax.oss.driver.api.core.cql.ColumnDefinition)

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