use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class SettableByIndex method setShort.
/**
* Sets the {@code i}th value to the provided Java primitive short.
*
* <p>By default, this works with CQL type {@code smallint}.
*
* <p>To set the value to CQL {@code NULL}, use {@link #setToNull(int)}, or {@code set(i, v,
* Short.class)}.
*
* @throws IndexOutOfBoundsException if the index is invalid.
*/
@NonNull
@CheckReturnValue
default SelfT setShort(int i, short v) {
DataType cqlType = getType(i);
TypeCodec<Short> codec = codecRegistry().codecFor(cqlType, Short.class);
return (codec instanceof PrimitiveShortCodec) ? setBytesUnsafe(i, ((PrimitiveShortCodec) codec).encodePrimitive(v, protocolVersion())) : set(i, v, codec);
}
use of com.datastax.oss.driver.api.core.type.DataType 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);
}
}
use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class CachingCodecRegistry method inferCqlTypeFromValue.
@Nullable
protected DataType inferCqlTypeFromValue(@NonNull Object value) {
if (value instanceof List) {
List<?> list = (List<?>) value;
if (list.isEmpty()) {
return CQL_TYPE_FOR_EMPTY_LISTS;
}
Object firstElement = list.get(0);
if (firstElement == null) {
throw new IllegalArgumentException("Can't infer list codec because the first element is null " + "(note that CQL does not allow null values in collections)");
}
DataType elementType = inferCqlTypeFromValue(firstElement);
if (elementType == null) {
return null;
}
return DataTypes.listOf(elementType);
} else if (value instanceof Set) {
Set<?> set = (Set<?>) value;
if (set.isEmpty()) {
return CQL_TYPE_FOR_EMPTY_SETS;
}
Object firstElement = set.iterator().next();
if (firstElement == null) {
throw new IllegalArgumentException("Can't infer set codec because the first element is null " + "(note that CQL does not allow null values in collections)");
}
DataType elementType = inferCqlTypeFromValue(firstElement);
if (elementType == null) {
return null;
}
return DataTypes.setOf(elementType);
} else if (value instanceof Map) {
Map<?, ?> map = (Map<?, ?>) value;
if (map.isEmpty()) {
return CQL_TYPE_FOR_EMPTY_MAPS;
}
Entry<?, ?> firstEntry = map.entrySet().iterator().next();
Object firstKey = firstEntry.getKey();
Object firstValue = firstEntry.getValue();
if (firstKey == null || firstValue == null) {
throw new IllegalArgumentException("Can't infer map codec because the first key and/or value is null " + "(note that CQL does not allow null values in collections)");
}
DataType keyType = inferCqlTypeFromValue(firstKey);
DataType valueType = inferCqlTypeFromValue(firstValue);
if (keyType == null || valueType == null) {
return null;
}
return DataTypes.mapOf(keyType, valueType);
}
Class<?> javaClass = value.getClass();
if (ByteBuffer.class.isAssignableFrom(javaClass)) {
return DataTypes.BLOB;
} else if (String.class.equals(javaClass)) {
return DataTypes.TEXT;
} else if (Long.class.equals(javaClass)) {
return DataTypes.BIGINT;
} else if (Boolean.class.equals(javaClass)) {
return DataTypes.BOOLEAN;
} else if (BigDecimal.class.equals(javaClass)) {
return DataTypes.DECIMAL;
} else if (Double.class.equals(javaClass)) {
return DataTypes.DOUBLE;
} else if (Float.class.equals(javaClass)) {
return DataTypes.FLOAT;
} else if (Integer.class.equals(javaClass)) {
return DataTypes.INT;
} else if (Instant.class.equals(javaClass)) {
return DataTypes.TIMESTAMP;
} else if (UUID.class.equals(javaClass)) {
return DataTypes.UUID;
} else if (BigInteger.class.equals(javaClass)) {
return DataTypes.VARINT;
} else if (InetAddress.class.isAssignableFrom(javaClass)) {
return DataTypes.INET;
} else if (LocalDate.class.equals(javaClass)) {
return DataTypes.DATE;
} else if (LocalTime.class.equals(javaClass)) {
return DataTypes.TIME;
} else if (Short.class.equals(javaClass)) {
return DataTypes.SMALLINT;
} else if (Byte.class.equals(javaClass)) {
return DataTypes.TINYINT;
} else if (CqlDuration.class.equals(javaClass)) {
return DataTypes.DURATION;
} else if (UdtValue.class.isAssignableFrom(javaClass)) {
return ((UdtValue) value).getType();
} else if (TupleValue.class.isAssignableFrom(javaClass)) {
return ((TupleValue) value).getType();
}
// so don't throw CodecNotFoundException just yet.
return null;
}
use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class UdtCodec method parse.
@Nullable
@Override
public UdtValue parse(@Nullable String value) {
if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) {
return null;
}
UdtValue udt = cqlType.newValue();
int length = value.length();
int position = ParseUtils.skipSpaces(value, 0);
if (value.charAt(position) != '{') {
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\" at character %d: expecting '{' but got '%c'", value, position, value.charAt(position)));
}
position++;
position = ParseUtils.skipSpaces(value, position);
if (position == length) {
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\" at character %d: expecting CQL identifier or '}', got EOF", value, position));
}
CodecRegistry registry = cqlType.getAttachmentPoint().getCodecRegistry();
CqlIdentifier id = null;
while (position < length) {
if (value.charAt(position) == '}') {
position = ParseUtils.skipSpaces(value, position + 1);
if (position == length) {
return udt;
}
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", at character %d expecting EOF or blank, but got \"%s\"", value, position, value.substring(position)));
}
int n;
try {
n = ParseUtils.skipCQLId(value, position);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", cannot parse a CQL identifier at character %d", value, position), e);
}
id = CqlIdentifier.fromInternal(value.substring(position, n));
position = n;
if (!cqlType.contains(id)) {
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", unknown CQL identifier at character %d: \"%s\"", value, position, id));
}
position = ParseUtils.skipSpaces(value, position);
if (position == length) {
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", at field %s (character %d) expecting ':', but got EOF", value, id, position));
}
if (value.charAt(position) != ':') {
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", at field %s (character %d) expecting ':', but got '%c'", value, id, position, value.charAt(position)));
}
position++;
position = ParseUtils.skipSpaces(value, position);
try {
n = ParseUtils.skipCQLValue(value, position);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", invalid CQL value at field %s (character %d)", value, id, position), e);
}
String fieldValue = value.substring(position, n);
// This works because ids occur at most once in UDTs
DataType fieldType = cqlType.getFieldTypes().get(cqlType.firstIndexOf(id));
TypeCodec<Object> codec = registry.codecFor(fieldType);
Object parsed;
try {
parsed = codec.parse(fieldValue);
} catch (Exception e) {
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", invalid CQL value at field %s (character %d): %s", value, id, position, e.getMessage()), e);
}
udt = udt.set(id, parsed, codec);
position = n;
position = ParseUtils.skipSpaces(value, position);
if (position == length) {
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", at field %s (character %d) expecting ',' or '}', but got EOF", value, id, position));
}
if (value.charAt(position) == '}') {
continue;
}
if (value.charAt(position) != ',') {
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", at field %s (character %d) expecting ',' but got '%c'", value, id, position, value.charAt(position)));
}
// skip ','
++position;
position = ParseUtils.skipSpaces(value, position);
}
throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\" at field %s (character %d): expecting CQL identifier or '}', got EOF", value, id, position));
}
use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class DataTypeHelper method fromProtocolSpec.
public static DataType fromProtocolSpec(RawType rawType, AttachmentPoint attachmentPoint) {
DataType type = PRIMITIVE_TYPES_BY_CODE.get(rawType.id);
if (type != null) {
return type;
} else {
switch(rawType.id) {
case ProtocolConstants.DataType.CUSTOM:
RawType.RawCustom rawCustom = (RawType.RawCustom) rawType;
return DataTypes.custom(rawCustom.className);
case ProtocolConstants.DataType.LIST:
RawType.RawList rawList = (RawType.RawList) rawType;
return DataTypes.listOf(fromProtocolSpec(rawList.elementType, attachmentPoint));
case ProtocolConstants.DataType.SET:
RawType.RawSet rawSet = (RawType.RawSet) rawType;
return DataTypes.setOf(fromProtocolSpec(rawSet.elementType, attachmentPoint));
case ProtocolConstants.DataType.MAP:
RawType.RawMap rawMap = (RawType.RawMap) rawType;
return DataTypes.mapOf(fromProtocolSpec(rawMap.keyType, attachmentPoint), fromProtocolSpec(rawMap.valueType, attachmentPoint));
case ProtocolConstants.DataType.TUPLE:
RawType.RawTuple rawTuple = (RawType.RawTuple) rawType;
List<RawType> rawFieldsList = rawTuple.fieldTypes;
ImmutableList.Builder<DataType> fields = ImmutableList.builder();
for (RawType rawField : rawFieldsList) {
fields.add(fromProtocolSpec(rawField, attachmentPoint));
}
return new DefaultTupleType(fields.build(), attachmentPoint);
case ProtocolConstants.DataType.UDT:
RawType.RawUdt rawUdt = (RawType.RawUdt) rawType;
ImmutableList.Builder<CqlIdentifier> fieldNames = ImmutableList.builder();
ImmutableList.Builder<DataType> fieldTypes = ImmutableList.builder();
for (Map.Entry<String, RawType> entry : rawUdt.fields.entrySet()) {
fieldNames.add(CqlIdentifier.fromInternal(entry.getKey()));
fieldTypes.add(fromProtocolSpec(entry.getValue(), attachmentPoint));
}
return new DefaultUserDefinedType(CqlIdentifier.fromInternal(rawUdt.keyspace), CqlIdentifier.fromInternal(rawUdt.typeName), false, fieldNames.build(), fieldTypes.build(), attachmentPoint);
default:
throw new IllegalArgumentException("Unsupported type: " + rawType.id);
}
}
}
Aggregations