Search in sources :

Example 1 with CodecNotFoundException

use of com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException in project java-driver by datastax.

the class CachingCodecRegistry method createCodec.

// Try to create a codec when we haven't found it in the cache.
// Variant where the Java type is unknown.
@NonNull
protected TypeCodec<?> createCodec(@NonNull DataType cqlType) {
    if (cqlType instanceof ListType) {
        DataType elementType = ((ListType) cqlType).getElementType();
        TypeCodec<Object> elementCodec = codecFor(elementType);
        return TypeCodecs.listOf(elementCodec);
    } else if (cqlType instanceof SetType) {
        DataType elementType = ((SetType) cqlType).getElementType();
        TypeCodec<Object> elementCodec = codecFor(elementType);
        return TypeCodecs.setOf(elementCodec);
    } else if (cqlType instanceof MapType) {
        DataType keyType = ((MapType) cqlType).getKeyType();
        DataType valueType = ((MapType) cqlType).getValueType();
        TypeCodec<Object> keyCodec = codecFor(keyType);
        TypeCodec<Object> valueCodec = codecFor(valueType);
        return TypeCodecs.mapOf(keyCodec, valueCodec);
    } else if (cqlType instanceof TupleType) {
        return TypeCodecs.tupleOf((TupleType) cqlType);
    } else if (cqlType instanceof UserDefinedType) {
        return TypeCodecs.udtOf((UserDefinedType) cqlType);
    } else if (cqlType instanceof CustomType) {
        return TypeCodecs.custom(cqlType);
    }
    throw new CodecNotFoundException(cqlType, null);
}
Also used : CustomType(com.datastax.oss.driver.api.core.type.CustomType) SetType(com.datastax.oss.driver.api.core.type.SetType) TypeCodec(com.datastax.oss.driver.api.core.type.codec.TypeCodec) ListType(com.datastax.oss.driver.api.core.type.ListType) TupleType(com.datastax.oss.driver.api.core.type.TupleType) DataType(com.datastax.oss.driver.api.core.type.DataType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CodecNotFoundException(com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException) MapType(com.datastax.oss.driver.api.core.type.MapType) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 2 with CodecNotFoundException

use of com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException in project java-driver by datastax.

the class CachingCodecRegistry method inferJavaTypeFromCqlType.

@NonNull
protected GenericType<?> inferJavaTypeFromCqlType(@NonNull DataType cqlType) {
    if (cqlType instanceof ListType) {
        DataType elementType = ((ListType) cqlType).getElementType();
        return GenericType.listOf(inferJavaTypeFromCqlType(elementType));
    } else if (cqlType instanceof SetType) {
        DataType elementType = ((SetType) cqlType).getElementType();
        return GenericType.setOf(inferJavaTypeFromCqlType(elementType));
    } else if (cqlType instanceof MapType) {
        DataType keyType = ((MapType) cqlType).getKeyType();
        DataType valueType = ((MapType) cqlType).getValueType();
        return GenericType.mapOf(inferJavaTypeFromCqlType(keyType), inferJavaTypeFromCqlType(valueType));
    }
    switch(cqlType.getProtocolCode()) {
        case ProtocolConstants.DataType.CUSTOM:
        case ProtocolConstants.DataType.BLOB:
            return GenericType.BYTE_BUFFER;
        case ProtocolConstants.DataType.ASCII:
        case ProtocolConstants.DataType.VARCHAR:
            return GenericType.STRING;
        case ProtocolConstants.DataType.BIGINT:
        case ProtocolConstants.DataType.COUNTER:
            return GenericType.LONG;
        case ProtocolConstants.DataType.BOOLEAN:
            return GenericType.BOOLEAN;
        case ProtocolConstants.DataType.DECIMAL:
            return GenericType.BIG_DECIMAL;
        case ProtocolConstants.DataType.DOUBLE:
            return GenericType.DOUBLE;
        case ProtocolConstants.DataType.FLOAT:
            return GenericType.FLOAT;
        case ProtocolConstants.DataType.INT:
            return GenericType.INTEGER;
        case ProtocolConstants.DataType.TIMESTAMP:
            return GenericType.INSTANT;
        case ProtocolConstants.DataType.UUID:
        case ProtocolConstants.DataType.TIMEUUID:
            return GenericType.UUID;
        case ProtocolConstants.DataType.VARINT:
            return GenericType.BIG_INTEGER;
        case ProtocolConstants.DataType.INET:
            return GenericType.INET_ADDRESS;
        case ProtocolConstants.DataType.DATE:
            return GenericType.LOCAL_DATE;
        case ProtocolConstants.DataType.TIME:
            return GenericType.LOCAL_TIME;
        case ProtocolConstants.DataType.SMALLINT:
            return GenericType.SHORT;
        case ProtocolConstants.DataType.TINYINT:
            return GenericType.BYTE;
        case ProtocolConstants.DataType.DURATION:
            return GenericType.CQL_DURATION;
        case ProtocolConstants.DataType.UDT:
            return GenericType.UDT_VALUE;
        case ProtocolConstants.DataType.TUPLE:
            return GenericType.TUPLE_VALUE;
        default:
            throw new CodecNotFoundException(cqlType, null);
    }
}
Also used : SetType(com.datastax.oss.driver.api.core.type.SetType) ListType(com.datastax.oss.driver.api.core.type.ListType) DataType(com.datastax.oss.driver.api.core.type.DataType) CodecNotFoundException(com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException) MapType(com.datastax.oss.driver.api.core.type.MapType) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 3 with CodecNotFoundException

use of com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException in project java-driver by datastax.

the class PagingStateIT method should_inject_in_simple_statement_with_custom_codecs.

@Test
public void should_inject_in_simple_statement_with_custom_codecs() {
    try (CqlSession session = (CqlSession) SessionUtils.baseBuilder().addTypeCodecs(new IntWrapperCodec()).addContactEndPoints(CCM_RULE.getContactPoints()).withKeyspace(SESSION_RULE.keyspace()).build()) {
        SimpleStatement statement = SimpleStatement.newInstance("SELECT * FROM foo WHERE k = ?", new IntWrapper(1)).setPageSize(15);
        ResultSet resultSet = session.execute(statement);
        assertThat(resultSet.getAvailableWithoutFetching()).isEqualTo(15);
        assertThat(resultSet.isFullyFetched()).isFalse();
        PagingState pagingState = resultSet.getExecutionInfo().getSafePagingState();
        // setPagingState() cannot find the custom codec.
        try {
            @SuppressWarnings("unused") SimpleStatement ignored = statement.setPagingState(pagingState);
            fail("Expected a CodecNotFoundException");
        } catch (CodecNotFoundException e) {
        // expected
        }
        resultSet = session.execute(statement.setPagingState(pagingState, session));
        assertThat(resultSet.getAvailableWithoutFetching()).isEqualTo(5);
        assertThat(resultSet.isFullyFetched()).isTrue();
    }
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) CodecNotFoundException(com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException) CqlSession(com.datastax.oss.driver.api.core.CqlSession) PagingState(com.datastax.oss.driver.api.core.cql.PagingState) Test(org.junit.Test)

Example 4 with CodecNotFoundException

use of com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException in project java-driver by datastax.

the class CachingCodecRegistry method createCodec.

// Try to create a codec when we haven't found it in the cache
@NonNull
protected TypeCodec<?> createCodec(@Nullable DataType cqlType, @Nullable GenericType<?> javaType, boolean isJavaCovariant) {
    LOG.trace("[{}] Cache miss, creating codec", logPrefix);
    // Either type can be null, but not both.
    if (javaType == null) {
        assert cqlType != null;
        return createCodec(cqlType);
    } else if (cqlType == null) {
        return createCodec(javaType, isJavaCovariant);
    } else {
        // Both non-null
        TypeToken<?> token = javaType.__getToken();
        if (cqlType instanceof ListType && List.class.isAssignableFrom(token.getRawType())) {
            DataType elementCqlType = ((ListType) cqlType).getElementType();
            TypeCodec<Object> elementCodec;
            if (token.getType() instanceof ParameterizedType) {
                Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
                GenericType<?> elementJavaType = GenericType.of(typeArguments[0]);
                elementCodec = uncheckedCast(codecFor(elementCqlType, elementJavaType, isJavaCovariant));
            } else {
                elementCodec = codecFor(elementCqlType);
            }
            return TypeCodecs.listOf(elementCodec);
        } else if (cqlType instanceof SetType && Set.class.isAssignableFrom(token.getRawType())) {
            DataType elementCqlType = ((SetType) cqlType).getElementType();
            TypeCodec<Object> elementCodec;
            if (token.getType() instanceof ParameterizedType) {
                Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
                GenericType<?> elementJavaType = GenericType.of(typeArguments[0]);
                elementCodec = uncheckedCast(codecFor(elementCqlType, elementJavaType, isJavaCovariant));
            } else {
                elementCodec = codecFor(elementCqlType);
            }
            return TypeCodecs.setOf(elementCodec);
        } else if (cqlType instanceof MapType && Map.class.isAssignableFrom(token.getRawType())) {
            DataType keyCqlType = ((MapType) cqlType).getKeyType();
            DataType valueCqlType = ((MapType) cqlType).getValueType();
            TypeCodec<Object> keyCodec;
            TypeCodec<Object> valueCodec;
            if (token.getType() instanceof ParameterizedType) {
                Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
                GenericType<?> keyJavaType = GenericType.of(typeArguments[0]);
                GenericType<?> valueJavaType = GenericType.of(typeArguments[1]);
                keyCodec = uncheckedCast(codecFor(keyCqlType, keyJavaType, isJavaCovariant));
                valueCodec = uncheckedCast(codecFor(valueCqlType, valueJavaType, isJavaCovariant));
            } else {
                keyCodec = codecFor(keyCqlType);
                valueCodec = codecFor(valueCqlType);
            }
            return TypeCodecs.mapOf(keyCodec, valueCodec);
        } else if (cqlType instanceof TupleType && TupleValue.class.isAssignableFrom(token.getRawType())) {
            return TypeCodecs.tupleOf((TupleType) cqlType);
        } else if (cqlType instanceof UserDefinedType && UdtValue.class.isAssignableFrom(token.getRawType())) {
            return TypeCodecs.udtOf((UserDefinedType) cqlType);
        } else if (cqlType instanceof CustomType && ByteBuffer.class.isAssignableFrom(token.getRawType())) {
            return TypeCodecs.custom(cqlType);
        }
        throw new CodecNotFoundException(cqlType, javaType);
    }
}
Also used : CustomType(com.datastax.oss.driver.api.core.type.CustomType) GenericType(com.datastax.oss.driver.api.core.type.reflect.GenericType) Set(java.util.Set) TypeCodec(com.datastax.oss.driver.api.core.type.codec.TypeCodec) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) ByteBuffer(java.nio.ByteBuffer) MapType(com.datastax.oss.driver.api.core.type.MapType) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) ParameterizedType(java.lang.reflect.ParameterizedType) ListType(com.datastax.oss.driver.api.core.type.ListType) GenericType(com.datastax.oss.driver.api.core.type.reflect.GenericType) MapType(com.datastax.oss.driver.api.core.type.MapType) TupleType(com.datastax.oss.driver.api.core.type.TupleType) DataType(com.datastax.oss.driver.api.core.type.DataType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) SetType(com.datastax.oss.driver.api.core.type.SetType) CustomType(com.datastax.oss.driver.api.core.type.CustomType) SetType(com.datastax.oss.driver.api.core.type.SetType) TypeToken(com.datastax.oss.driver.shaded.guava.common.reflect.TypeToken) ListType(com.datastax.oss.driver.api.core.type.ListType) TupleType(com.datastax.oss.driver.api.core.type.TupleType) DataType(com.datastax.oss.driver.api.core.type.DataType) CodecNotFoundException(com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Aggregations

CodecNotFoundException (com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException)4 DataType (com.datastax.oss.driver.api.core.type.DataType)3 ListType (com.datastax.oss.driver.api.core.type.ListType)3 MapType (com.datastax.oss.driver.api.core.type.MapType)3 SetType (com.datastax.oss.driver.api.core.type.SetType)3 NonNull (edu.umd.cs.findbugs.annotations.NonNull)3 CustomType (com.datastax.oss.driver.api.core.type.CustomType)2 TupleType (com.datastax.oss.driver.api.core.type.TupleType)2 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)2 TypeCodec (com.datastax.oss.driver.api.core.type.codec.TypeCodec)2 CqlSession (com.datastax.oss.driver.api.core.CqlSession)1 PagingState (com.datastax.oss.driver.api.core.cql.PagingState)1 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)1 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)1 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)1 GenericType (com.datastax.oss.driver.api.core.type.reflect.GenericType)1 TypeToken (com.datastax.oss.driver.shaded.guava.common.reflect.TypeToken)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ByteBuffer (java.nio.ByteBuffer)1