Search in sources :

Example 11 with TupleValue

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

the class DateRangeIT method should_allow_date_range_in_udt_and_tuple.

/**
 * Validates that {@link DateRange} can be used in UDT and Tuple types.
 */
@Test
public void should_allow_date_range_in_udt_and_tuple() throws Exception {
    CqlSession session = sessionRule.session();
    String tableName = testName.getMethodName();
    session.execute("CREATE TYPE IF NOT EXISTS test_udt (i int, range 'DateRangeType')");
    session.execute(String.format("CREATE TABLE %s (k int PRIMARY KEY, u test_udt, uf frozen<test_udt>, " + "t tuple<'DateRangeType', int>, tf frozen<tuple<'DateRangeType', int>>)", tableName));
    session.execute(String.format("INSERT INTO %s (k, u, uf, t, tf) VALUES (" + "1, " + "{i: 10, range: '[2000-01-01T10:15:30.003Z TO 2020-01-01T10:15:30.001Z]'}, " + "{i: 20, range: '[2000-01-01T10:15:30.003Z TO 2020-01-01T10:15:30.001Z]'}, " + "('[2000-01-01T10:15:30.003Z TO 2020-01-01T10:15:30.001Z]', 30), " + "('[2000-01-01T10:15:30.003Z TO 2020-01-01T10:15:30.001Z]', 40))", tableName));
    DateRange expected = DateRange.parse("[2000-01-01T10:15:30.003Z TO 2020-01-01T10:15:30.001Z]");
    List<Row> rows = session.execute("SELECT * FROM " + tableName).all();
    assertThat(rows).hasSize(1);
    UdtValue u = rows.get(0).get("u", UdtValue.class);
    DateRange dateRange = u.get("range", DateRange.class);
    assertThat(dateRange).isEqualTo(expected);
    assertThat(u.getInt("i")).isEqualTo(10);
    u = rows.get(0).get("uf", UdtValue.class);
    dateRange = u.get("range", DateRange.class);
    assertThat(dateRange).isEqualTo(expected);
    assertThat(u.getInt("i")).isEqualTo(20);
    TupleValue t = rows.get(0).get("t", TupleValue.class);
    dateRange = t.get(0, DateRange.class);
    assertThat(dateRange).isEqualTo(expected);
    assertThat(t.getInt(1)).isEqualTo(30);
    t = rows.get(0).get("tf", TupleValue.class);
    dateRange = t.get(0, DateRange.class);
    assertThat(dateRange).isEqualTo(expected);
    assertThat(t.getInt(1)).isEqualTo(40);
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) Row(com.datastax.oss.driver.api.core.cql.Row) CqlSession(com.datastax.oss.driver.api.core.CqlSession) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) Test(org.junit.Test)

Example 12 with TupleValue

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

the class GeometryIT method should_insert_as_tuple.

/**
 * Validates that geometry values can be inserted as a tuple and verifies that the tuple is stored
 * correctly by retrieving it and ensuring it matches.
 */
@Test
@Ignore
public void should_insert_as_tuple() {
    UUID key = Uuids.random();
    PreparedStatement prepared = sessionRule.session().prepare("INSERT INTO tbl (k, t) values (?, ?)");
    TupleType tupleType = (TupleType) prepared.getVariableDefinitions().get(1).getType();
    TupleValue tuple = tupleType.newValue();
    tuple = tuple.set(0, sampleData.get(0), genericType);
    tuple = tuple.set(1, sampleData.get(1), genericType);
    tuple = tuple.set(2, sampleData.get(2), genericType);
    BoundStatement bs = prepared.boundStatementBuilder().setUuid(0, key).setTupleValue(1, tuple).build();
    sessionRule.session().execute(bs);
    ResultSet rs = sessionRule.session().execute(SimpleStatement.builder("SELECT k,t FROM tbl where k=?").addPositionalValues(key).build());
    Row row = rs.iterator().next();
    assertThat(row.getUuid("k")).isEqualTo(key);
    assertThat(row.getTupleValue("t")).isEqualTo(tuple);
    assertThat(row.getTupleValue(1)).isEqualTo(tuple);
}
Also used : TupleType(com.datastax.oss.driver.api.core.type.TupleType) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) Row(com.datastax.oss.driver.api.core.cql.Row) UUID(java.util.UUID) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 13 with TupleValue

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

the class DefaultTupleValueTest method should_equate_instances_with_same_values_but_different_binary_representations.

@Test
public void should_equate_instances_with_same_values_but_different_binary_representations() {
    TupleType tupleType = DataTypes.tupleOf(DataTypes.VARINT);
    TupleValue tuple1 = tupleType.newValue().setBytesUnsafe(0, Bytes.fromHexString("0x01"));
    TupleValue tuple2 = tupleType.newValue().setBytesUnsafe(0, Bytes.fromHexString("0x0001"));
    assertThat(tuple1).isEqualTo(tuple2);
    assertThat(tuple1.hashCode()).isEqualTo(tuple2.hashCode());
}
Also used : TupleType(com.datastax.oss.driver.api.core.type.TupleType) DefaultTupleType(com.datastax.oss.driver.internal.core.type.DefaultTupleType) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) Test(org.junit.Test)

Example 14 with TupleValue

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

the class DefaultTupleValueTest method should_equate_instances_with_different_protocol_versions.

@Test
public void should_equate_instances_with_different_protocol_versions() {
    TupleType tupleType1 = DataTypes.tupleOf(DataTypes.TEXT);
    tupleType1.attach(attachmentPoint);
    // use the V3 attachmentPoint for type2
    TupleType tupleType2 = DataTypes.tupleOf(DataTypes.TEXT);
    tupleType2.attach(v3AttachmentPoint);
    TupleValue tuple1 = tupleType1.newValue().setBytesUnsafe(0, Bytes.fromHexString("0x01"));
    TupleValue tuple2 = tupleType2.newValue().setBytesUnsafe(0, Bytes.fromHexString("0x01"));
    assertThat(tuple1).isEqualTo(tuple2);
}
Also used : TupleType(com.datastax.oss.driver.api.core.type.TupleType) DefaultTupleType(com.datastax.oss.driver.internal.core.type.DefaultTupleType) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) Test(org.junit.Test)

Example 15 with TupleValue

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

the class DefaultTupleValueTest method should_support_null_items_when_setting_in_bulk.

@Test
public void should_support_null_items_when_setting_in_bulk() {
    DefaultTupleType type = new DefaultTupleType(ImmutableList.of(DataTypes.INT, DataTypes.TEXT), attachmentPoint);
    when(codecRegistry.<Integer>codecFor(DataTypes.INT)).thenReturn(TypeCodecs.INT);
    when(codecRegistry.codecFor(DataTypes.TEXT, "foo")).thenReturn(TypeCodecs.TEXT);
    TupleValue value = type.newValue(null, "foo");
    assertThat(value.isNull(0)).isTrue();
    assertThat(value.getString(1)).isEqualTo("foo");
}
Also used : DefaultTupleType(com.datastax.oss.driver.internal.core.type.DefaultTupleType) 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