Search in sources :

Example 11 with ProtocolVersion

use of com.datastax.oss.driver.api.core.ProtocolVersion 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

ProtocolVersion (com.datastax.oss.driver.api.core.ProtocolVersion)11 ByteBuffer (java.nio.ByteBuffer)4 ConsistencyLevel (com.datastax.oss.driver.api.core.ConsistencyLevel)3 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)3 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)3 Row (com.datastax.oss.driver.api.core.cql.Row)3 ProtocolVersionRegistry (com.datastax.oss.driver.internal.core.ProtocolVersionRegistry)3 UnsupportedProtocolVersionException (com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException)2 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)2 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)2 Node (com.datastax.oss.driver.api.core.metadata.Node)2 TokenMap (com.datastax.oss.driver.api.core.metadata.TokenMap)2 CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)2 ConsistencyLevelRegistry (com.datastax.oss.driver.internal.core.ConsistencyLevelRegistry)2 Execute (com.datastax.oss.protocol.internal.request.Execute)2 Query (com.datastax.oss.protocol.internal.request.Query)2 List (java.util.List)2 Test (org.junit.Test)2 DseProtocolVersion (com.datastax.dse.driver.api.core.DseProtocolVersion)1 ScriptGraphStatement (com.datastax.dse.driver.api.core.graph.ScriptGraphStatement)1