Search in sources :

Example 11 with UserDefinedType

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

the class UserDefinedTypeListParserTest method should_resolve_tuple_dependency.

@Test
public void should_resolve_tuple_dependency() {
    UserDefinedTypeParser parser = new UserDefinedTypeParser(new DataTypeCqlNameParser(), context);
    Map<CqlIdentifier, UserDefinedType> types = parser.parse(KEYSPACE_ID, mockTypeRow("ks", "a", ImmutableList.of("b"), ImmutableList.of("frozen<tuple<int, frozen<b>>>")), mockTypeRow("ks", "b", ImmutableList.of("i"), ImmutableList.of("int")));
    assertThat(types).hasSize(2);
    UserDefinedType aType = types.get(CqlIdentifier.fromInternal("a"));
    UserDefinedType bType = types.get(CqlIdentifier.fromInternal("b"));
    assertThat(((TupleType) aType.getFieldTypes().get(0)).getComponentTypes().get(1)).isEqualTo(bType);
}
Also used : UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Test(org.junit.Test)

Example 12 with UserDefinedType

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

the class DefaultUdtValueTest method should_format_to_string.

@Test
public void should_format_to_string() {
    UserDefinedType type = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("t"), DataTypes.TEXT).withField(CqlIdentifier.fromInternal("i"), DataTypes.INT).withField(CqlIdentifier.fromInternal("d"), DataTypes.DOUBLE).build();
    UdtValue udt = type.newValue().setString("t", "foobar").setDouble("d", 3.14);
    assertThat(udt.getFormattedContents()).isEqualTo("{t:'foobar',i:NULL,d:3.14}");
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) Test(org.junit.Test)

Example 13 with UserDefinedType

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

the class DefaultUdtValueTest method should_equate_instances_with_different_protocol_versions.

@Test
public void should_equate_instances_with_different_protocol_versions() {
    UserDefinedType type1 = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("t"), DataTypes.TEXT).withField(CqlIdentifier.fromInternal("i"), DataTypes.INT).withField(CqlIdentifier.fromInternal("d"), DataTypes.DOUBLE).build();
    type1.attach(attachmentPoint);
    // create an idential type, but with a different attachment point
    UserDefinedType type2 = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("t"), DataTypes.TEXT).withField(CqlIdentifier.fromInternal("i"), DataTypes.INT).withField(CqlIdentifier.fromInternal("d"), DataTypes.DOUBLE).build();
    type2.attach(v3AttachmentPoint);
    UdtValue udt1 = type1.newValue().setString("t", "some text string").setInt("i", 42).setDouble("d", 3.14);
    UdtValue udt2 = type2.newValue().setString("t", "some text string").setInt("i", 42).setDouble("d", 3.14);
    assertThat(udt1).isEqualTo(udt2);
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) Test(org.junit.Test)

Example 14 with UserDefinedType

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

the class DefaultUdtValueTest method newInstance.

@Override
protected UdtValue newInstance(List<DataType> dataTypes, AttachmentPoint attachmentPoint) {
    UserDefinedTypeBuilder builder = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type"));
    for (int i = 0; i < dataTypes.size(); i++) {
        builder.withField(CqlIdentifier.fromInternal("field" + i), dataTypes.get(i));
    }
    UserDefinedType userDefinedType = builder.build();
    userDefinedType.attach(attachmentPoint);
    return userDefinedType.newValue();
}
Also used : UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) AttachmentPoint(com.datastax.oss.driver.api.core.detach.AttachmentPoint)

Example 15 with UserDefinedType

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

the class ImmutableEntityIT 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 mapper = InventoryMapper.builder(session).build();
    dao = mapper.immutableProductDao(SESSION_RULE.keyspace());
}
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) ParallelizableTests(com.datastax.oss.driver.categories.ParallelizableTests) FLUENT(com.datastax.oss.driver.api.mapper.entity.naming.GetterStyle.FLUENT) Computed(com.datastax.oss.driver.api.mapper.annotations.Computed) Entity(com.datastax.oss.driver.api.mapper.annotations.Entity) CqlSession(com.datastax.oss.driver.api.core.CqlSession) MapperBuilder(com.datastax.oss.driver.api.mapper.MapperBuilder) Select(com.datastax.oss.driver.api.mapper.annotations.Select) PropertyStrategy(com.datastax.oss.driver.api.mapper.annotations.PropertyStrategy) ClassRule(org.junit.ClassRule) Row(com.datastax.oss.driver.api.core.cql.Row) PartitionKey(com.datastax.oss.driver.api.mapper.annotations.PartitionKey) DefaultNullSavingStrategy(com.datastax.oss.driver.api.mapper.annotations.DefaultNullSavingStrategy) Test(org.junit.Test) CqlName(com.datastax.oss.driver.api.mapper.annotations.CqlName) 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) Objects(java.util.Objects) RuleChain(org.junit.rules.RuleChain) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) GetEntity(com.datastax.oss.driver.api.mapper.annotations.GetEntity) 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)

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