use of com.datastax.oss.driver.api.core.type.reflect.GenericType in project java-driver by datastax.
the class GeometryIT method should_insert_as_field_in_udt.
/**
* Validates that a geometry value can be inserted as a field in a UDT and verifies that the UDT
* is stored correctly by retrieving it and ensuring it matches.
*/
@Test
@Ignore
public void should_insert_as_field_in_udt() {
UUID key = Uuids.random();
UserDefinedType udtType = sessionRule.session().getMetadata().getKeyspace(sessionRule.session().getKeyspace().orElseThrow(AssertionError::new)).flatMap(ks -> ks.getUserDefinedType(CqlIdentifier.fromInternal("udt1"))).orElseThrow(AssertionError::new);
assertThat(udtType).isNotNull();
UdtValue value = udtType.newValue();
value = value.set("g", sampleData.get(0), genericType);
PreparedStatement prepared = sessionRule.session().prepare("INSERT INTO tbl (k, u) values (?, ?)");
BoundStatement bs = prepared.boundStatementBuilder().setUuid(0, key).setUdtValue(1, value).build();
sessionRule.session().execute(bs);
ResultSet rs = sessionRule.session().execute(SimpleStatement.builder("SELECT k,u FROM tbl where k=?").addPositionalValues(key).build());
Row row = rs.iterator().next();
assertThat(row.getUuid("k")).isEqualTo(key);
assertThat(row.getUdtValue("u")).isEqualTo(value);
assertThat(row.getUdtValue(1)).isEqualTo(value);
}
use of com.datastax.oss.driver.api.core.type.reflect.GenericType in project java-driver by datastax.
the class EntityHelperBaseTest method should_throw_if_there_is_not_matching_cql_column.
@Test
public void should_throw_if_there_is_not_matching_cql_column() {
// given
ImmutableMap<CqlIdentifier, GenericType<?>> entityColumns = ImmutableMap.of(CqlIdentifier.fromCql("c1"), GenericType.of(Integer.class));
ColumnMetadata columnMetadataInt = mock(ColumnMetadata.class);
when(columnMetadataInt.getType()).thenReturn(DataTypes.INT);
ImmutableMap<CqlIdentifier, ColumnMetadata> cqlColumns = ImmutableMap.of(CqlIdentifier.fromCql("c2"), columnMetadataInt);
// when, then
assertThatThrownBy(() -> EntityHelperBase.findTypeMismatches(entityColumns, cqlColumns, CodecRegistry.DEFAULT)).isInstanceOf(AssertionError.class).hasMessageContaining("There is no cql column for entity column: c1");
}
use of com.datastax.oss.driver.api.core.type.reflect.GenericType in project java-driver by datastax.
the class CachingCodecRegistry method createCodec.
// Try to create a codec when we haven't found it in the cache
@NonNull
protected TypeCodec<?> createCodec(@Nullable DataType cqlType, @Nullable GenericType<?> javaType, boolean isJavaCovariant) {
LOG.trace("[{}] Cache miss, creating codec", logPrefix);
// Either type can be null, but not both.
if (javaType == null) {
assert cqlType != null;
return createCodec(cqlType);
} else if (cqlType == null) {
return createCodec(javaType, isJavaCovariant);
} else {
// Both non-null
TypeToken<?> token = javaType.__getToken();
if (cqlType instanceof ListType && List.class.isAssignableFrom(token.getRawType())) {
DataType elementCqlType = ((ListType) cqlType).getElementType();
TypeCodec<Object> elementCodec;
if (token.getType() instanceof ParameterizedType) {
Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
GenericType<?> elementJavaType = GenericType.of(typeArguments[0]);
elementCodec = uncheckedCast(codecFor(elementCqlType, elementJavaType, isJavaCovariant));
} else {
elementCodec = codecFor(elementCqlType);
}
return TypeCodecs.listOf(elementCodec);
} else if (cqlType instanceof SetType && Set.class.isAssignableFrom(token.getRawType())) {
DataType elementCqlType = ((SetType) cqlType).getElementType();
TypeCodec<Object> elementCodec;
if (token.getType() instanceof ParameterizedType) {
Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
GenericType<?> elementJavaType = GenericType.of(typeArguments[0]);
elementCodec = uncheckedCast(codecFor(elementCqlType, elementJavaType, isJavaCovariant));
} else {
elementCodec = codecFor(elementCqlType);
}
return TypeCodecs.setOf(elementCodec);
} else if (cqlType instanceof MapType && Map.class.isAssignableFrom(token.getRawType())) {
DataType keyCqlType = ((MapType) cqlType).getKeyType();
DataType valueCqlType = ((MapType) cqlType).getValueType();
TypeCodec<Object> keyCodec;
TypeCodec<Object> valueCodec;
if (token.getType() instanceof ParameterizedType) {
Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
GenericType<?> keyJavaType = GenericType.of(typeArguments[0]);
GenericType<?> valueJavaType = GenericType.of(typeArguments[1]);
keyCodec = uncheckedCast(codecFor(keyCqlType, keyJavaType, isJavaCovariant));
valueCodec = uncheckedCast(codecFor(valueCqlType, valueJavaType, isJavaCovariant));
} else {
keyCodec = codecFor(keyCqlType);
valueCodec = codecFor(valueCqlType);
}
return TypeCodecs.mapOf(keyCodec, valueCodec);
} else if (cqlType instanceof TupleType && TupleValue.class.isAssignableFrom(token.getRawType())) {
return TypeCodecs.tupleOf((TupleType) cqlType);
} else if (cqlType instanceof UserDefinedType && UdtValue.class.isAssignableFrom(token.getRawType())) {
return TypeCodecs.udtOf((UserDefinedType) cqlType);
} else if (cqlType instanceof CustomType && ByteBuffer.class.isAssignableFrom(token.getRawType())) {
return TypeCodecs.custom(cqlType);
}
throw new CodecNotFoundException(cqlType, javaType);
}
}
use of com.datastax.oss.driver.api.core.type.reflect.GenericType in project java-driver by datastax.
the class CodecRegistryIT method should_register_and_use_custom_codec_for_user_defined_type.
@Test
public void should_register_and_use_custom_codec_for_user_defined_type() {
Map<String, Coordinates> coordinatesMap = ImmutableMap.of("home", new Coordinates(12, 34));
GenericType<Map<String, Coordinates>> coordinatesMapType = GenericType.mapOf(String.class, Coordinates.class);
// Still create a separate session because we don't want to interfere with other tests
try (CqlSession session = SessionUtils.newSession(CCM_RULE, SESSION_RULE.keyspace())) {
// register the mapping codec for UDT coordinates
UserDefinedType coordinatesUdt = session.getMetadata().getKeyspace(SESSION_RULE.keyspace()).flatMap(ks -> ks.getUserDefinedType("coordinates")).orElseThrow(IllegalStateException::new);
MutableCodecRegistry codecRegistry = (MutableCodecRegistry) session.getContext().getCodecRegistry();
// Retrieve the inner codec
TypeCodec<UdtValue> innerCodec = codecRegistry.codecFor(coordinatesUdt);
assertThat(innerCodec).isInstanceOf(UdtCodec.class);
// Create the "outer" codec and register it
CoordinatesCodec coordinatesCodec = new CoordinatesCodec(innerCodec);
codecRegistry.register(coordinatesCodec);
// Test that the codec will be used to create on-the-fly codecs
assertThat(codecRegistry.codecFor(Coordinates.class)).isSameAs(coordinatesCodec);
assertThat(codecRegistry.codecFor(coordinatesMapType).accepts(coordinatesMap)).isTrue();
// test insertion
PreparedStatement prepared = session.prepare("INSERT INTO test3 (k0, k1, v) values (?, ?, ?)");
BoundStatement insert = prepared.boundStatementBuilder().setString(0, name.getMethodName()).setInt(1, 0).set(2, coordinatesMap, // use java type so has to be looked up in registry.
coordinatesMapType).build();
session.execute(insert);
// test retrieval
ResultSet result = session.execute(SimpleStatement.builder("SELECT v from test3 where k0 = ? AND k1 = ?").addPositionalValues(name.getMethodName(), 0).build());
List<Row> rows = result.all();
assertThat(rows).hasSize(1);
Row row = rows.get(0);
assertThat(row.get(0, coordinatesMapType)).isEqualTo(coordinatesMap);
assertThat(row.getMap(0, String.class, Coordinates.class)).isEqualTo(coordinatesMap);
}
}
Aggregations