Search in sources :

Example 21 with TupleValue

use of com.datastax.oss.driver.api.core.data.TupleValue in project java-driver by datastax.

the class TuplesSimple method retrieveData.

private static void retrieveData(CqlSession session) {
    for (int k = 1; k <= 2; k++) {
        // Execute the SELECT query and retrieve the single row in the result set
        SimpleStatement statement = SimpleStatement.newInstance("SELECT c FROM examples.tuples WHERE k = ?", k);
        Row row = session.execute(statement).one();
        assert row != null;
        // Retrieve the value for column c
        TupleValue coordinatesValue = row.getTupleValue("c");
        assert coordinatesValue != null;
        // Display the contents of the tuple
        System.out.printf("found coordinate: (%d,%d)%n", coordinatesValue.getInt(0), coordinatesValue.getInt(1));
    }
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Row(com.datastax.oss.driver.api.core.cql.Row) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue)

Example 22 with TupleValue

use of com.datastax.oss.driver.api.core.data.TupleValue 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 23 with TupleValue

use of com.datastax.oss.driver.api.core.data.TupleValue in project java-driver by datastax.

the class CachingCodecRegistryTestDataProviders method tuplesWithCqlTypes.

@DataProvider
public static Object[][] tuplesWithCqlTypes() {
    TupleType tupleType1 = DataTypes.tupleOf(DataTypes.INT, DataTypes.TEXT);
    TupleType tupleType2 = DataTypes.tupleOf(DataTypes.INT, DataTypes.listOf(DataTypes.TEXT));
    TupleType tupleType3 = DataTypes.tupleOf(DataTypes.mapOf(tupleType1, tupleType2));
    TupleValue tupleValue1 = tupleType1.newValue(42, "foo");
    TupleValue tupleValue2 = tupleType2.newValue(42, ImmutableList.of("foo", "bar"));
    return new Object[][] { { tupleType1, tupleType1.newValue() }, { tupleType1, tupleValue1 }, { tupleType2, tupleType2.newValue() }, { tupleType2, tupleValue2 }, { tupleType3, tupleType3.newValue() }, { tupleType3, tupleType3.newValue(ImmutableMap.of(tupleValue1, tupleValue2)) } };
}
Also used : TupleType(com.datastax.oss.driver.api.core.type.TupleType) DefaultTupleValue(com.datastax.oss.driver.internal.core.data.DefaultTupleValue) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider)

Example 24 with TupleValue

use of com.datastax.oss.driver.api.core.data.TupleValue in project java-driver by datastax.

the class CachingCodecRegistryTest method should_create_tuple_codec_for_cql_type.

@Test
@UseDataProvider(value = "tuplesWithCqlTypes", location = CachingCodecRegistryTestDataProviders.class)
public void should_create_tuple_codec_for_cql_type(DataType cqlType, Object value) {
    TestCachingCodecRegistry registry = new TestCachingCodecRegistry(mockCache);
    InOrder inOrder = inOrder(mockCache);
    TypeCodec<TupleValue> codec = registry.codecFor(cqlType);
    assertThat(codec).isNotNull();
    assertThat(codec.accepts(cqlType)).isTrue();
    assertThat(codec.accepts(GenericType.TUPLE_VALUE)).isTrue();
    assertThat(codec.accepts(TupleValue.class)).isTrue();
    assertThat(codec.accepts(value)).isTrue();
    inOrder.verify(mockCache).lookup(cqlType, null, false);
}
Also used : InOrder(org.mockito.InOrder) DefaultTupleValue(com.datastax.oss.driver.internal.core.data.DefaultTupleValue) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 25 with TupleValue

use of com.datastax.oss.driver.api.core.data.TupleValue in project java-driver by datastax.

the class TupleCodecTest method should_decode_tuple.

@Test
public void should_decode_tuple() {
    TupleValue tuple = decode("0x" + ("00000004" + "00000001") + "ffffffff" + ("00000001" + "61"));
    assertThat(tuple.getInt(0)).isEqualTo(1);
    assertThat(tuple.isNull(1)).isTrue();
    assertThat(tuple.getString(2)).isEqualTo("a");
    verify(intCodec).decodePrimitive(Bytes.fromHexString("0x00000001"), ProtocolVersion.DEFAULT);
    verifyZeroInteractions(doubleCodec);
    verify(textCodec).decode(Bytes.fromHexString("0x61"), ProtocolVersion.DEFAULT);
}
Also used : DefaultTupleValue(com.datastax.oss.driver.internal.core.data.DefaultTupleValue) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) Test(org.junit.Test)

Aggregations

TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)31 Test (org.junit.Test)18 DefaultTupleValue (com.datastax.oss.driver.internal.core.data.DefaultTupleValue)12 TupleType (com.datastax.oss.driver.api.core.type.TupleType)10 Row (com.datastax.oss.driver.api.core.cql.Row)6 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)5 DefaultTupleType (com.datastax.oss.driver.internal.core.type.DefaultTupleType)5 DataType (com.datastax.oss.driver.api.core.type.DataType)4 ByteBuffer (java.nio.ByteBuffer)4 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)3 Nullable (edu.umd.cs.findbugs.annotations.Nullable)3 InetAddress (java.net.InetAddress)3 List (java.util.List)3 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)2 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)2 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)2 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)2 CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)2 UserDefinedTypeBuilder (com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder)2 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)2