Search in sources :

Example 11 with UserDefinedTypeBuilder

use of com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder in project java-driver by datastax.

the class DefaultUdtValueTest method newInstance.

@Override
protected UdtValue newInstance(List<DataType> dataTypes, List<Object> values, 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(values.toArray());
}
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 12 with UserDefinedTypeBuilder

use of com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder in project java-driver by datastax.

the class GraphDataTypesTest method complexUdtTests.

@Test
public void complexUdtTests() throws IOException {
    UserDefinedType type1 = new UserDefinedTypeBuilder("ks", "udt1").withField("a", INT).withField("b", TEXT).build();
    verifySerDeBinary(type1.newValue(1, "2"));
    TupleType secondNested = tupleOf(BIGINT, listOf(BIGINT));
    TupleType firstNested = tupleOf(TEXT, secondNested);
    UserDefinedType type2 = new UserDefinedTypeBuilder("ks", "udt2").withField("a", INT).withField("b", TEXT).withField("c", type1).withField("mylist", listOf(BIGINT)).withField("mytuple_withlist", firstNested).build();
    verifySerDeBinary(type2.newValue(1, "2", type1.newValue(3, "4"), ImmutableList.of(5L), firstNested.newValue("6", secondNested.newValue(7L, ImmutableList.of(8L)))));
    UserDefinedType type3 = new UserDefinedTypeBuilder("ks", "udt3").withField("a", listOf(INT)).withField("b", setOf(FLOAT)).withField("c", mapOf(TEXT, BIGINT)).withField("d", listOf(listOf(DOUBLE))).withField("e", setOf(setOf(FLOAT))).withField("f", listOf(tupleOf(INT, TEXT))).build();
    verifySerDeBinary(type3.newValue(ImmutableList.of(1), ImmutableSet.of(2.1f), ImmutableMap.of("3", 4L), ImmutableList.of(ImmutableList.of(5.1d, 6.1d), ImmutableList.of(7.1d)), ImmutableSet.of(ImmutableSet.of(8.1f), ImmutableSet.of(9.1f)), ImmutableList.of(tupleOf(INT, TEXT).newValue(10, "11"))));
}
Also used : TupleType(com.datastax.oss.driver.api.core.type.TupleType) 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 UserDefinedTypeBuilder

use of com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder 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)

Example 14 with UserDefinedTypeBuilder

use of com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder in project java-driver by datastax.

the class CachingCodecRegistryTestDataProviders method collectionsWithCqlAndJavaTypes.

@DataProvider
public static Object[][] collectionsWithCqlAndJavaTypes() throws UnknownHostException, ClassNotFoundException {
    TupleType tupleType = DataTypes.tupleOf(DataTypes.INT, DataTypes.listOf(DataTypes.TEXT));
    TupleValue tupleValue = tupleType.newValue();
    UserDefinedType userType = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("field1"), DataTypes.INT).withField(CqlIdentifier.fromInternal("field2"), DataTypes.listOf(DataTypes.TEXT)).build();
    UdtValue udtValue = userType.newValue();
    return new Object[][] { // lists
    { DataTypes.listOf(DataTypes.INT), GenericType.listOf(Integer.class), GenericType.listOf(Integer.class), ImmutableList.of(1) }, { DataTypes.listOf(DataTypes.TEXT), GenericType.listOf(String.class), GenericType.listOf(String.class), ImmutableList.of("foo") }, { DataTypes.listOf(DataTypes.BLOB), GenericType.listOf(ByteBuffer.class), GenericType.listOf(Class.forName("java.nio.HeapByteBuffer")), ImmutableList.of(ByteBuffer.wrap(new byte[] { 127, 0, 0, 1 })) }, { DataTypes.listOf(DataTypes.INET), GenericType.listOf(InetAddress.class), GenericType.listOf(Inet4Address.class), ImmutableList.of(InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 })) }, { DataTypes.listOf(tupleType), GenericType.listOf(TupleValue.class), GenericType.listOf(DefaultTupleValue.class), ImmutableList.of(tupleValue) }, { DataTypes.listOf(userType), GenericType.listOf(UdtValue.class), GenericType.listOf(DefaultUdtValue.class), ImmutableList.of(udtValue) }, { DataTypes.listOf(DataTypes.listOf(DataTypes.INT)), GenericType.listOf(GenericType.listOf(Integer.class)), GenericType.listOf(GenericType.listOf(Integer.class)), ImmutableList.of(ImmutableList.of(1)) }, { DataTypes.listOf(DataTypes.listOf(tupleType)), GenericType.listOf(GenericType.listOf(TupleValue.class)), GenericType.listOf(GenericType.listOf(DefaultTupleValue.class)), ImmutableList.of(ImmutableList.of(tupleValue)) }, { DataTypes.listOf(DataTypes.listOf(userType)), GenericType.listOf(GenericType.listOf(UdtValue.class)), GenericType.listOf(GenericType.listOf(DefaultUdtValue.class)), ImmutableList.of(ImmutableList.of(udtValue)) }, // sets
    { DataTypes.setOf(DataTypes.INT), GenericType.setOf(Integer.class), GenericType.setOf(Integer.class), ImmutableSet.of(1) }, { DataTypes.setOf(DataTypes.TEXT), GenericType.setOf(String.class), GenericType.setOf(String.class), ImmutableSet.of("foo") }, { DataTypes.setOf(DataTypes.BLOB), GenericType.setOf(ByteBuffer.class), GenericType.setOf(Class.forName("java.nio.HeapByteBuffer")), ImmutableSet.of(ByteBuffer.wrap(new byte[] { 127, 0, 0, 1 })) }, { DataTypes.setOf(DataTypes.INET), GenericType.setOf(InetAddress.class), GenericType.setOf(Inet4Address.class), ImmutableSet.of(InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 })) }, { DataTypes.setOf(tupleType), GenericType.setOf(TupleValue.class), GenericType.setOf(DefaultTupleValue.class), ImmutableSet.of(tupleValue) }, { DataTypes.setOf(userType), GenericType.setOf(UdtValue.class), GenericType.setOf(DefaultUdtValue.class), ImmutableSet.of(udtValue) }, { DataTypes.setOf(DataTypes.setOf(DataTypes.INT)), GenericType.setOf(GenericType.setOf(Integer.class)), GenericType.setOf(GenericType.setOf(Integer.class)), ImmutableSet.of(ImmutableSet.of(1)) }, { DataTypes.setOf(DataTypes.setOf(tupleType)), GenericType.setOf(GenericType.setOf(TupleValue.class)), GenericType.setOf(GenericType.setOf(DefaultTupleValue.class)), ImmutableSet.of(ImmutableSet.of(tupleValue)) }, { DataTypes.setOf(DataTypes.setOf(userType)), GenericType.setOf(GenericType.setOf(UdtValue.class)), GenericType.setOf(GenericType.setOf(DefaultUdtValue.class)), ImmutableSet.of(ImmutableSet.of(udtValue)) }, // maps
    { DataTypes.mapOf(DataTypes.INT, DataTypes.TEXT), GenericType.mapOf(Integer.class, String.class), GenericType.mapOf(Integer.class, String.class), ImmutableMap.of(1, "foo") }, { DataTypes.mapOf(DataTypes.BLOB, DataTypes.INET), GenericType.mapOf(ByteBuffer.class, InetAddress.class), GenericType.mapOf(Class.forName("java.nio.HeapByteBuffer"), Inet4Address.class), ImmutableMap.of(ByteBuffer.wrap(new byte[] { 127, 0, 0, 1 }), InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 })) }, { DataTypes.mapOf(tupleType, tupleType), GenericType.mapOf(TupleValue.class, TupleValue.class), GenericType.mapOf(DefaultTupleValue.class, DefaultTupleValue.class), ImmutableMap.of(tupleValue, tupleValue) }, { DataTypes.mapOf(userType, userType), GenericType.mapOf(UdtValue.class, UdtValue.class), GenericType.mapOf(DefaultUdtValue.class, DefaultUdtValue.class), ImmutableMap.of(udtValue, udtValue) }, { DataTypes.mapOf(DataTypes.UUID, DataTypes.mapOf(DataTypes.INT, DataTypes.TEXT)), GenericType.mapOf(GenericType.UUID, GenericType.mapOf(Integer.class, String.class)), GenericType.mapOf(GenericType.UUID, GenericType.mapOf(Integer.class, String.class)), ImmutableMap.of(UUID.randomUUID(), ImmutableMap.of(1, "foo")) }, { DataTypes.mapOf(DataTypes.mapOf(userType, userType), DataTypes.mapOf(tupleType, tupleType)), GenericType.mapOf(GenericType.mapOf(UdtValue.class, UdtValue.class), GenericType.mapOf(TupleValue.class, TupleValue.class)), GenericType.mapOf(GenericType.mapOf(DefaultUdtValue.class, DefaultUdtValue.class), GenericType.mapOf(DefaultTupleValue.class, DefaultTupleValue.class)), ImmutableMap.of(ImmutableMap.of(udtValue, udtValue), ImmutableMap.of(tupleValue, tupleValue)) } };
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) DefaultUdtValue(com.datastax.oss.driver.internal.core.data.DefaultUdtValue) Inet4Address(java.net.Inet4Address) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) ByteBuffer(java.nio.ByteBuffer) DefaultTupleValue(com.datastax.oss.driver.internal.core.data.DefaultTupleValue) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) BigInteger(java.math.BigInteger) DefaultUdtValue(com.datastax.oss.driver.internal.core.data.DefaultUdtValue) DefaultTupleValue(com.datastax.oss.driver.internal.core.data.DefaultTupleValue) TupleType(com.datastax.oss.driver.api.core.type.TupleType) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) InetAddress(java.net.InetAddress) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider)

Example 15 with UserDefinedTypeBuilder

use of com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder in project java-driver by datastax.

the class CachingCodecRegistryTestDataProviders method udtsWithCqlTypes.

@DataProvider
public static Object[][] udtsWithCqlTypes() {
    UserDefinedType userType1 = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("field1"), DataTypes.INT).withField(CqlIdentifier.fromInternal("field2"), DataTypes.TEXT).build();
    UserDefinedType userType2 = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("field1"), DataTypes.setOf(DataTypes.BIGINT)).withField(CqlIdentifier.fromInternal("field2"), DataTypes.listOf(DataTypes.TEXT)).build();
    UserDefinedType userType3 = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("field1"), DataTypes.mapOf(userType1, userType2)).build();
    UdtValue userValue1 = userType1.newValue(42, "foo");
    UdtValue userValue2 = userType2.newValue(ImmutableSet.of(24L, 43L), ImmutableList.of("foo", "bar"));
    return new Object[][] { { userType1, userType1.newValue() }, { userType1, userValue1 }, { userType2, userType2.newValue() }, { userType2, userValue2 }, { userType3, userType3.newValue() }, { userType3, userType3.newValue(ImmutableMap.of(userValue1, userValue2)) } };
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) DefaultUdtValue(com.datastax.oss.driver.internal.core.data.DefaultUdtValue) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider)

Aggregations

UserDefinedTypeBuilder (com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder)16 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)15 Test (org.junit.Test)10 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)9 TupleType (com.datastax.oss.driver.api.core.type.TupleType)5 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)3 DefaultUdtValue (com.datastax.oss.driver.internal.core.data.DefaultUdtValue)3 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)3 AttachmentPoint (com.datastax.oss.driver.api.core.detach.AttachmentPoint)2 DefaultTupleValue (com.datastax.oss.driver.internal.core.data.DefaultTupleValue)2 MetadataRefresh (com.datastax.oss.driver.internal.core.metadata.MetadataRefresh)2 DefaultKeyspaceMetadata (com.datastax.oss.driver.internal.core.metadata.schema.DefaultKeyspaceMetadata)2 BigInteger (java.math.BigInteger)2 InetAddress (java.net.InetAddress)2 ByteBuffer (java.nio.ByteBuffer)2 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)1 DataType (com.datastax.oss.driver.api.core.type.DataType)1 CharsetCodec (com.datastax.oss.driver.api.querybuilder.CharsetCodec)1 DefaultTupleType (com.datastax.oss.driver.internal.core.type.DefaultTupleType)1 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)1