Search in sources :

Example 26 with TupleValue

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

the class TupleCodecTest method should_format_tuple.

@Test
public void should_format_tuple() {
    TupleValue tuple = tupleType.newValue();
    tuple = tuple.setInt(0, 1);
    tuple = tuple.setToNull(1);
    tuple = tuple.setString(2, "a");
    assertThat(format(tuple)).isEqualTo("(1,NULL,'a')");
    verify(intCodec).format(1);
    verify(doubleCodec).format(null);
    verify(textCodec).format("a");
}
Also used : DefaultTupleValue(com.datastax.oss.driver.internal.core.data.DefaultTupleValue) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) Test(org.junit.Test)

Example 27 with TupleValue

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

the class TupleCodecTest method should_parse_partial_tuple.

@Test
public void should_parse_partial_tuple() {
    TupleValue tuple = parse("(1,NULL)");
    assertThat(tuple.getInt(0)).isEqualTo(1);
    assertThat(tuple.isNull(1)).isTrue();
    assertThat(tuple.isNull(2)).isTrue();
    verify(intCodec).parse("1");
    verify(doubleCodec).parse("NULL");
    verifyNoMoreInteractions(textCodec);
}
Also used : DefaultTupleValue(com.datastax.oss.driver.internal.core.data.DefaultTupleValue) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) Test(org.junit.Test)

Example 28 with TupleValue

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

the class TupleCodecTest method should_parse_empty_tuple.

@Test
public void should_parse_empty_tuple() {
    TupleValue tuple = parse("()");
    assertThat(tuple.isNull(0)).isTrue();
    assertThat(tuple.isNull(1)).isTrue();
    assertThat(tuple.isNull(2)).isTrue();
    verifyNoMoreInteractions(intCodec);
    verifyNoMoreInteractions(doubleCodec);
    verifyNoMoreInteractions(textCodec);
}
Also used : DefaultTupleValue(com.datastax.oss.driver.internal.core.data.DefaultTupleValue) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) Test(org.junit.Test)

Example 29 with TupleValue

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

the class TupleCodecTest method should_encode_tuple.

@Test
public void should_encode_tuple() {
    TupleValue tuple = tupleType.newValue();
    tuple = tuple.setInt(0, 1);
    tuple = tuple.setToNull(1);
    tuple = tuple.setString(2, "a");
    assertThat(encode(tuple)).isEqualTo("0x" + // size and contents of field 0
    ("00000004" + "00000001") + // null field 1
    "ffffffff" + // size and contents of field 2
    ("00000001" + "61"));
    verify(intCodec).encodePrimitive(1, ProtocolVersion.DEFAULT);
    // null values are handled directly in the tuple codec, without calling the child codec:
    verifyZeroInteractions(doubleCodec);
    verify(textCodec).encode("a", 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)

Example 30 with TupleValue

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

the class DataTypeIT method readValue.

private <K> void readValue(Statement<?> select, DataType dataType, K value, K expectedPrimitiveValue) {
    TypeCodec<Object> codec = SESSION_RULE.session().getContext().getCodecRegistry().codecFor(dataType);
    ResultSet result = SESSION_RULE.session().execute(select);
    String columnName = columnNameFor(dataType);
    List<Row> rows = result.all();
    assertThat(rows).hasSize(1);
    Row row = rows.iterator().next();
    K expectedValue = expectedPrimitiveValue != null ? expectedPrimitiveValue : value;
    switch(dataType.getProtocolCode()) {
        case ProtocolConstants.DataType.ASCII:
        case ProtocolConstants.DataType.VARCHAR:
            assertThat(row.getString(columnName)).isEqualTo(expectedValue);
            assertThat(row.getString(0)).isEqualTo(expectedValue);
            break;
        case ProtocolConstants.DataType.BIGINT:
            assertThat(row.getLong(columnName)).isEqualTo(expectedValue);
            assertThat(row.getLong(0)).isEqualTo(expectedValue);
            break;
        case ProtocolConstants.DataType.BLOB:
            assertThat(row.getByteBuffer(columnName)).isEqualTo(expectedValue);
            assertThat(row.getByteBuffer(0)).isEqualTo(expectedValue);
            break;
        case ProtocolConstants.DataType.BOOLEAN:
            assertThat(row.getBoolean(columnName)).isEqualTo(expectedValue);
            assertThat(row.getBoolean(0)).isEqualTo(expectedValue);
            break;
        case ProtocolConstants.DataType.DECIMAL:
            assertThat(row.getBigDecimal(columnName)).isEqualTo(expectedValue);
            assertThat(row.getBigDecimal(0)).isEqualTo(expectedValue);
            break;
        case ProtocolConstants.DataType.DOUBLE:
            assertThat(row.getDouble(columnName)).isEqualTo(expectedValue);
            assertThat(row.getDouble(0)).isEqualTo(expectedValue);
            break;
        case ProtocolConstants.DataType.FLOAT:
            assertThat(row.getFloat(columnName)).isEqualTo(expectedValue);
            assertThat(row.getFloat(0)).isEqualTo(expectedValue);
            break;
        case ProtocolConstants.DataType.INET:
            assertThat(row.getInetAddress(columnName)).isEqualTo(expectedValue);
            assertThat(row.getInetAddress(0)).isEqualTo(expectedValue);
            break;
        case ProtocolConstants.DataType.TINYINT:
            assertThat(row.getByte(columnName)).isEqualTo(expectedValue);
            assertThat(row.getByte(0)).isEqualTo(expectedValue);
            break;
        case ProtocolConstants.DataType.SMALLINT:
            assertThat(row.getShort(columnName)).isEqualTo(expectedValue);
            assertThat(row.getShort(0)).isEqualTo(expectedValue);
            break;
        case ProtocolConstants.DataType.INT:
            assertThat(row.getInt(columnName)).isEqualTo(expectedValue);
            assertThat(row.getInt(0)).isEqualTo(expectedValue);
            break;
        case ProtocolConstants.DataType.DURATION:
            assertThat(row.getCqlDuration(columnName)).isEqualTo(expectedValue);
            assertThat(row.getCqlDuration(0)).isEqualTo(expectedValue);
            break;
        case ProtocolConstants.DataType.TIMESTAMP:
            assertThat(row.getInstant(columnName)).isEqualTo(expectedValue);
            assertThat(row.getInstant(0)).isEqualTo(expectedValue);
            break;
        case ProtocolConstants.DataType.DATE:
            assertThat(row.getLocalDate(columnName)).isEqualTo(expectedValue);
            assertThat(row.getLocalDate(0)).isEqualTo(expectedValue);
            break;
        case ProtocolConstants.DataType.TIME:
            assertThat(row.getLocalTime(columnName)).isEqualTo(expectedValue);
            assertThat(row.getLocalTime(0)).isEqualTo(expectedValue);
            break;
        case ProtocolConstants.DataType.TIMEUUID:
        case ProtocolConstants.DataType.UUID:
            assertThat(row.getUuid(columnName)).isEqualTo(expectedValue);
            assertThat(row.getUuid(0)).isEqualTo(expectedValue);
            break;
        case ProtocolConstants.DataType.VARINT:
            assertThat(row.getBigInteger(columnName)).isEqualTo(expectedValue);
            assertThat(row.getBigInteger(0)).isEqualTo(expectedValue);
            break;
        case ProtocolConstants.DataType.CUSTOM:
            if (((CustomType) dataType).getClassName().equals("org.apache.cassandra.db.marshal.DurationType")) {
                assertThat(row.getCqlDuration(columnName)).isEqualTo(expectedValue);
                assertThat(row.getCqlDuration(0)).isEqualTo(expectedValue);
                break;
            }
        // fall through
        case ProtocolConstants.DataType.LIST:
        case ProtocolConstants.DataType.MAP:
        case ProtocolConstants.DataType.SET:
            assertThat(row.get(columnName, codec)).isEqualTo(expectedValue);
            assertThat(row.get(0, codec)).isEqualTo(expectedValue);
            break;
        case ProtocolConstants.DataType.TUPLE:
            TupleValue returnedValue = row.getTupleValue(columnName);
            TupleValue exValue = (TupleValue) expectedValue;
            assertThat(returnedValue.getType()).isEqualTo(exValue.getType());
            assertThat(row.getTupleValue(columnName)).isEqualTo(expectedValue);
            assertThat(row.getTupleValue(0)).isEqualTo(expectedValue);
            break;
        case ProtocolConstants.DataType.UDT:
            UdtValue returnedUdtValue = row.getUdtValue(columnName);
            UdtValue exUdtValue = (UdtValue) expectedValue;
            assertThat(returnedUdtValue.getType()).isEqualTo(exUdtValue.getType());
            assertThat(row.getUdtValue(columnName)).isEqualTo(expectedValue);
            assertThat(row.getUdtValue(0)).isEqualTo(expectedValue);
            break;
        default:
            fail("Unhandled DataType " + dataType);
    }
    if (value == null) {
        assertThat(row.isNull(columnName)).isTrue();
        assertThat(row.isNull(0)).isTrue();
    }
    // Decode directly using the codec
    ProtocolVersion protocolVersion = SESSION_RULE.session().getContext().getProtocolVersion();
    assertThat(codec.decode(row.getBytesUnsafe(columnName), protocolVersion)).isEqualTo(value);
    assertThat(codec.decode(row.getBytesUnsafe(0), protocolVersion)).isEqualTo(value);
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) Row(com.datastax.oss.driver.api.core.cql.Row) ProtocolVersion(com.datastax.oss.driver.api.core.ProtocolVersion) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue)

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