Search in sources :

Example 36 with UserDefinedType

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);
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) MutableCodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.MutableCodecRegistry) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType)

Example 37 with UserDefinedType

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);
}
Also used : CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) DaoFactory(com.datastax.oss.driver.api.mapper.annotations.DaoFactory) BeforeClass(org.junit.BeforeClass) DaoKeyspace(com.datastax.oss.driver.api.mapper.annotations.DaoKeyspace) TestRule(org.junit.rules.TestRule) Mapper(com.datastax.oss.driver.api.mapper.annotations.Mapper) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CcmRule(com.datastax.oss.driver.api.testinfra.ccm.CcmRule) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) MappedAsyncPagingIterable(com.datastax.oss.driver.api.core.MappedAsyncPagingIterable) ParallelizableTests(com.datastax.oss.driver.categories.ParallelizableTests) PagingIterable(com.datastax.oss.driver.api.core.PagingIterable) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) ClassRule(org.junit.ClassRule) Row(com.datastax.oss.driver.api.core.cql.Row) CompletableFutures(com.datastax.oss.driver.internal.core.util.concurrent.CompletableFutures) Sets(com.datastax.oss.driver.shaded.guava.common.collect.Sets) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) DefaultNullSavingStrategy(com.datastax.oss.driver.api.mapper.annotations.DefaultNullSavingStrategy) Test(org.junit.Test) Insert(com.datastax.oss.driver.api.mapper.annotations.Insert) UUID(java.util.UUID) Category(org.junit.experimental.categories.Category) UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) SessionRule(com.datastax.oss.driver.api.testinfra.session.SessionRule) RuleChain(org.junit.rules.RuleChain) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) GetEntity(com.datastax.oss.driver.api.mapper.annotations.GetEntity) Stream(java.util.stream.Stream) NullSavingStrategy(com.datastax.oss.driver.api.mapper.entity.saving.NullSavingStrategy) Dao(com.datastax.oss.driver.api.mapper.annotations.Dao) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlSession(com.datastax.oss.driver.api.core.CqlSession) BeforeClass(org.junit.BeforeClass)

Example 38 with UserDefinedType

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());
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.Test)

Example 39 with UserDefinedType

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);
}
Also used : ListType(com.datastax.oss.driver.api.core.type.ListType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) ShallowUserDefinedType(com.datastax.oss.driver.internal.core.metadata.schema.ShallowUserDefinedType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Test(org.junit.Test)

Example 40 with UserDefinedType

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));
}
Also used : DefaultKeyspaceMetadata(com.datastax.oss.driver.internal.core.metadata.schema.DefaultKeyspaceMetadata) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) MetadataRefresh(com.datastax.oss.driver.internal.core.metadata.MetadataRefresh) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) Test(org.junit.Test)

Aggregations

UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)53 Test (org.junit.Test)33 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)21 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)20 UserDefinedTypeBuilder (com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder)15 TupleType (com.datastax.oss.driver.api.core.type.TupleType)13 CqlSession (com.datastax.oss.driver.api.core.CqlSession)9 DataType (com.datastax.oss.driver.api.core.type.DataType)9 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 ListType (com.datastax.oss.driver.api.core.type.ListType)6 SetType (com.datastax.oss.driver.api.core.type.SetType)6 SessionRule (com.datastax.oss.driver.api.testinfra.session.SessionRule)6 ParallelizableTests (com.datastax.oss.driver.categories.ParallelizableTests)6 Category (org.junit.experimental.categories.Category)6 Row (com.datastax.oss.driver.api.core.cql.Row)5 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)5 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)5 TypeCodec (com.datastax.oss.driver.api.core.type.codec.TypeCodec)5 CcmRule (com.datastax.oss.driver.api.testinfra.ccm.CcmRule)5 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)4