use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class UserDefinedTypesMapped method registerCoordinatesCodec.
private static void registerCoordinatesCodec(CqlSession session) {
// retrieve the codec registry
MutableCodecRegistry codecRegistry = (MutableCodecRegistry) session.getContext().getCodecRegistry();
// retrieve the user-defined type metadata
UserDefinedType coordinatesType = retrieveCoordinatesType(session);
// retrieve the driver built-in codec for the user-defined type "coordinates"
TypeCodec<UdtValue> innerCodec = codecRegistry.codecFor(coordinatesType);
// create a custom codec to map the "coordinates" user-defined type to the Coordinates class
CoordinatesCodec coordinatesCodec = new CoordinatesCodec(innerCodec);
// register the new codec
codecRegistry.register(coordinatesCodec);
}
use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class GetEntityIT method setup.
@BeforeClass
public static void setup() {
CqlSession session = SESSION_RULE.session();
for (String query : createStatements(CCM_RULE)) {
session.execute(SimpleStatement.builder(query).setExecutionProfile(SESSION_RULE.slowProfile()).build());
}
UserDefinedType dimensions2d = session.getKeyspace().flatMap(ks -> session.getMetadata().getKeyspace(ks)).flatMap(ks -> ks.getUserDefinedType("dimensions2d")).orElseThrow(AssertionError::new);
session.execute("INSERT INTO product2d (id, description, dimensions) VALUES (?, ?, ?)", PRODUCT_2D_ID, "2D product", dimensions2d.newValue(12, 34));
InventoryMapper inventoryMapper = new GetEntityIT_InventoryMapperBuilder(session).build();
dao = inventoryMapper.productDao(SESSION_RULE.keyspace());
dao.save(FLAMETHROWER);
dao.save(MP3_DOWNLOAD);
}
use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class SetEntityIT method should_set_entity_on_udt_value.
@Test
public void should_set_entity_on_udt_value() {
CqlSession session = SESSION_RULE.session();
UserDefinedType udtType = session.getMetadata().getKeyspace(SESSION_RULE.keyspace()).orElseThrow(AssertionError::new).getUserDefinedType("dimensions").orElseThrow(AssertionError::new);
UdtValue udtValue = udtType.newValue();
Dimensions dimensions = new Dimensions(30, 10, 8);
dao.set(dimensions, udtValue);
assertThat(udtValue.getInt("length")).isEqualTo(dimensions.getLength());
assertThat(udtValue.getInt("width")).isEqualTo(dimensions.getWidth());
assertThat(udtValue.getInt("height")).isEqualTo(dimensions.getHeight());
}
use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class DataTypeCqlNameParserTest method should_reuse_existing_user_type_when_not_top_level.
@Test
public void should_reuse_existing_user_type_when_not_top_level() {
UserDefinedType addressType = mock(UserDefinedType.class);
UserDefinedType frozenAddressType = mock(UserDefinedType.class);
when(addressType.copy(false)).thenReturn(addressType);
when(addressType.copy(true)).thenReturn(frozenAddressType);
ImmutableMap<CqlIdentifier, UserDefinedType> existingTypes = ImmutableMap.of(CqlIdentifier.fromInternal("address"), addressType);
ListType listOfAddress = (ListType) parse("list<address>", existingTypes);
assertThat(listOfAddress.getElementType()).isEqualTo(addressType);
ListType listOfFrozenAddress = (ListType) parse("list<frozen<address>>", existingTypes);
assertThat(listOfFrozenAddress.getElementType()).isEqualTo(frozenAddressType);
}
use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class SchemaRefreshTest method should_detect_updated_children_in_keyspace.
@Test
public void should_detect_updated_children_in_keyspace() {
// Drop one type, modify the other and add a third one
UserDefinedType newT2 = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks1"), CqlIdentifier.fromInternal("t2")).withField(CqlIdentifier.fromInternal("i"), DataTypes.TEXT).build();
UserDefinedType t3 = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks1"), CqlIdentifier.fromInternal("t3")).withField(CqlIdentifier.fromInternal("i"), DataTypes.INT).build();
DefaultKeyspaceMetadata newKs1 = newKeyspace("ks1", true, newT2, t3);
SchemaRefresh refresh = new SchemaRefresh(ImmutableMap.of(OLD_KS1.getName(), newKs1));
MetadataRefresh.Result result = refresh.compute(oldMetadata, false, context);
assertThat(result.newMetadata.getKeyspaces().get(OLD_KS1.getName())).isEqualTo(newKs1);
assertThat(result.events).containsExactly(TypeChangeEvent.dropped(OLD_T1), TypeChangeEvent.updated(OLD_T2, newT2), TypeChangeEvent.created(t3));
}
Aggregations