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);
}
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);
}
}
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();
}
}
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);
}
}
Aggregations