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;
}
}
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;
}
}
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);
}
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();
}
}
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());
}
Aggregations