in empire-db/src/main/java/org/apache/empire/commons/ValueUtils.java [783:839]
public <T> T convertColumnValue(ColumnExpr column, Object value, Class<T> vt)
throws ClassCastException
{
// check
if (value==ObjectUtils.NO_VALUE)
throw new InvalidValueException(value);
// use a is
if (vt==Object.class)
return ((T)value);
// convert
if (vt==String.class)
return (T)toString(value);
if (vt==Integer.class)
return (T)(value!=null ? toInteger(value) : INTEGER_ZERO);
if (vt==Long.class)
return (T)(value!=null ? toLong(value) : LONG_ZERO);
if (vt==Double.class)
return (T)(value!=null ? toDouble(value) : DOUBLE_ZERO);
if (vt==BigDecimal.class)
return (T)(value!=null ? toDecimal(value) : BigDecimal.ZERO); // required for compatibility
if (vt==Boolean.class)
return (T)(value!=null ? toBoolean(value, false) : Boolean.FALSE);
// enum
if (vt.isEnum())
{ // Null
if (value==null)
return null;
// convert
Class<? extends Enum<?>> enumType = (Class<? extends Enum<?>>)vt;
try {
// Convert to enum, depending on DataType
boolean numeric = column.getDataType().isNumeric();
return (T)toEnum(enumType, (numeric ? toInteger(value) : value));
} catch (Exception e) {
// Illegal value
log.error("Unable to resolve enum value of '{}' for type {}", value, enumType.getName());
throw new FieldIllegalValueException(column.getUpdateColumn(), String.valueOf(value), e);
}
}
// Date conversions
try {
// DateTimeFormatter.ISO_LOCAL_DATE_TIME
if (vt==Date.class)
return (T)toDate(value);
if (vt==Timestamp.class)
return (T)toTimestamp(value);
if (vt==LocalDate.class)
return (T)toLocalDate(value);
if (vt==LocalDateTime.class)
return (T)toLocalDateTime(value);
} catch (ParseException | DateTimeParseException e) {
throw new ValueConversionException(Timestamp.class, value, e);
}
// something else?
return convertToJava(vt, value);
}