in HSQL/src/org/hsqldb1/Column.java [800:1198]
public static Object convertObject(Object o,
int type) throws HsqlException {
try {
if (o == null) {
return null;
}
switch (type) {
case Types.NULL :
return null;
case Types.TINYINT :
if (o instanceof java.lang.String) {
o = Library.trim((String) o, " ", true, true);
int val = Integer.parseInt((String) o);
o = ValuePool.getInt(val);
}
if (o instanceof java.lang.Integer) {
int temp = ((Number) o).intValue();
if (Byte.MAX_VALUE < temp || temp < Byte.MIN_VALUE) {
throw Trace.error(
Trace.NUMERIC_VALUE_OUT_OF_RANGE);
}
return o;
}
if (o instanceof java.lang.Long) {
long temp = ((Number) o).longValue();
if (Byte.MAX_VALUE < temp || temp < Byte.MIN_VALUE) {
throw Trace.error(
Trace.NUMERIC_VALUE_OUT_OF_RANGE);
}
return ValuePool.getInt(((Number) o).intValue());
}
// fredt@users - direct conversion to optimise JDBC setObject(Byte)
if (o instanceof java.lang.Byte) {
return ValuePool.getInt(((Number) o).intValue());
}
// fredt@users - returns to this method for range checking
if (o instanceof java.lang.Number) {
return convertObject(convertToInt(o), type);
}
if (o instanceof java.lang.Boolean) {
return ((Boolean) o).booleanValue()
? ValuePool.getInt(1)
: ValuePool.getInt(0);
}
break;
case Types.SMALLINT :
if (o instanceof java.lang.String) {
o = Library.trim((String) o, " ", true, true);
int val = Integer.parseInt((String) o);
o = ValuePool.getInt(val);
}
if (o instanceof java.lang.Integer) {
int temp = ((Number) o).intValue();
if (Short.MAX_VALUE < temp || temp < Short.MIN_VALUE) {
throw Trace.error(
Trace.NUMERIC_VALUE_OUT_OF_RANGE);
}
return o;
}
if (o instanceof java.lang.Long) {
long temp = ((Number) o).longValue();
if (Short.MAX_VALUE < temp || temp < Short.MIN_VALUE) {
throw Trace.error(
Trace.NUMERIC_VALUE_OUT_OF_RANGE);
}
return ValuePool.getInt(((Number) o).intValue());
}
// fredt@users - direct conversion for JDBC setObject(Short), etc.
if (o instanceof Byte || o instanceof Short) {
return ValuePool.getInt(((Number) o).intValue());
}
// fredt@users - returns to this method for range checking
if (o instanceof Number) {
return convertObject(convertToInt(o), type);
}
if (o instanceof java.lang.Boolean) {
return ((Boolean) o).booleanValue()
? ValuePool.getInt(1)
: ValuePool.getInt(0);
}
break;
case Types.INTEGER :
if (o instanceof java.lang.Integer) {
return o;
}
if (o instanceof java.lang.String) {
o = Library.trim((String) o, " ", true, true);
int val = Integer.parseInt((String) o);
return ValuePool.getInt(val);
}
if (o instanceof java.lang.Long) {
long temp = ((Number) o).longValue();
if (Integer.MAX_VALUE < temp
|| temp < Integer.MIN_VALUE) {
throw Trace.error(
Trace.NUMERIC_VALUE_OUT_OF_RANGE);
}
return ValuePool.getInt(((Number) o).intValue());
}
if (o instanceof Byte || o instanceof Short) {
return ValuePool.getInt(((Number) o).intValue());
}
if (o instanceof java.lang.Number) {
return convertToInt(o);
}
if (o instanceof java.lang.Boolean) {
return ((Boolean) o).booleanValue()
? ValuePool.getInt(1)
: ValuePool.getInt(0);
}
break;
case Types.BIGINT :
if (o instanceof java.lang.Long) {
return o;
}
if (o instanceof java.lang.String) {
o = Library.trim((String) o, " ", true, true);
long val = Long.parseLong((String) o);
return ValuePool.getLong(val);
}
if (o instanceof java.lang.Integer) {
return ValuePool.getLong(((Integer) o).longValue());
}
if (o instanceof Byte || o instanceof Short) {
return ValuePool.getLong(((Number) o).intValue());
}
if (o instanceof java.lang.Number) {
return convertToLong(o);
}
if (o instanceof java.lang.Boolean) {
return ((Boolean) o).booleanValue()
? ValuePool.getLong(1)
: ValuePool.getLong(0);
}
break;
case Types.REAL :
case Types.FLOAT :
case Types.DOUBLE :
if (o instanceof java.lang.Double) {
return o;
}
if (o instanceof java.lang.String) {
o = Library.trim((String) o, " ", true, true);
double d = JavaSystem.parseDouble((String) o);
long l = Double.doubleToLongBits(d);
return ValuePool.getDouble(l);
}
if (o instanceof java.lang.Number) {
return convertToDouble(o);
}
if (o instanceof java.lang.Boolean) {
return ((Boolean) o).booleanValue()
? ValuePool.getDouble(1)
: ValuePool.getDouble(0);
}
break;
case Types.NUMERIC :
case Types.DECIMAL :
if (o instanceof BigDecimal) {
return o;
}
if (o instanceof Long) {
return BigDecimal.valueOf(((Long) o).longValue());
}
if (o instanceof java.lang.Boolean) {
return ((Boolean) o).booleanValue() ? BIG_DECIMAL_1
: BIG_DECIMAL_0;
}
break;
case Types.BOOLEAN :
if (o instanceof java.lang.Boolean) {
return (Boolean) o;
}
if (o instanceof java.lang.String) {
o = Library.trim((String) o, " ", true, true);
return ((String) o).equalsIgnoreCase("TRUE")
? Boolean.TRUE
: Boolean.FALSE;
}
if (o instanceof Integer) {
return ((Integer) o).intValue() == 0 ? Boolean.FALSE
: Boolean.TRUE;
}
if (o instanceof Long) {
return ((Long) o).longValue() == 0L ? Boolean.FALSE
: Boolean.TRUE;
}
if (o instanceof java.lang.Double) {
return ((Double) o).doubleValue() == 0.0
? Boolean.FALSE
: Boolean.TRUE;
}
if (o instanceof BigDecimal) {
return ((BigDecimal) o).equals(BIG_DECIMAL_0)
? Boolean.FALSE
: Boolean.TRUE;
}
throw Trace.error(Trace.WRONG_DATA_TYPE);
case Types.VARCHAR_IGNORECASE :
case Types.VARCHAR :
case Types.CHAR :
case Types.LONGVARCHAR :
if (o instanceof java.lang.String) {
return o;
}
if (o instanceof Time) {
return HsqlDateTime.getTimeString((Time) o, null);
}
if (o instanceof Timestamp) {
return HsqlDateTime.getTimestampString((Timestamp) o,
null);
}
if (o instanceof Date) {
return HsqlDateTime.getDateString((Date) o, null);
}
if (o instanceof byte[]) {
return StringConverter.byteToHex((byte[]) o);
}
break;
case Types.TIME :
if (o instanceof Time) {
return HsqlDateTime.getNormalisedTime((Time) o);
}
if (o instanceof Timestamp) {
return HsqlDateTime.getNormalisedTime((Timestamp) o);
}
if (o instanceof String) {
return HsqlDateTime.timeValue((String) o);
}
if (o instanceof Date) {
throw Trace.error(Trace.INVALID_CONVERSION,
Types.getTypeString(type));
}
break;
case Types.TIMESTAMP :
if (o instanceof Timestamp) {
return o;
}
if (o instanceof Time) {
return HsqlDateTime.getNormalisedTimestamp((Time) o);
}
if (o instanceof Date) {
return HsqlDateTime.getNormalisedTimestamp((Date) o);
}
if (o instanceof String) {
return HsqlDateTime.timestampValue((String) o);
}
break;
case Types.DATE :
if (o instanceof Date) {
return HsqlDateTime.getNormalisedDate((Date) o);
}
if (o instanceof Timestamp) {
return HsqlDateTime.getNormalisedDate((Timestamp) o);
}
if (o instanceof String) {
return HsqlDateTime.dateValue((String) o);
}
if (o instanceof Time) {
throw Trace.error(Trace.INVALID_CONVERSION,
Types.getTypeString(type));
}
break;
case Types.BINARY :
case Types.VARBINARY :
case Types.LONGVARBINARY :
if (o instanceof Binary) {
return o;
} else if (o instanceof byte[]) {
return new Binary((byte[]) o, false);
} else if (o instanceof String) {
/**
* @todo fredt - we need this for script processing only
* handle the script separately and process normal
* conversion according to rules in SQL
* standard
*/
return new Binary(
StringConverter.hexToByte((String) o), false);
}
throw Trace.error(Trace.INVALID_CONVERSION,
Types.getTypeString(type));
// fredt@users 20030708 - patch 1.7.2 - OBJECT handling - superseded
case Types.OTHER :
if (o instanceof JavaObject) {
return o;
} else if (o instanceof String) {
/**
* @todo fredt - we need this for script processing only
* handle the script separately and allow normal Sting
* objects to be stored as JavaObject
*/
return new JavaObject(
StringConverter.hexToByte((String) o));
} else if (o instanceof Binary) {
return new JavaObject(((Binary) o).getBytes());
}
return new JavaObject((Serializable) o);
default :
}
if (o instanceof JavaObject) {
o = ((JavaObject) o).getObject();
return convertObject(o, type);
}
return convertString(o.toString(), type);
} catch (HsqlException e) {
throw e;
} catch (Exception e) {
throw Trace.error(Trace.WRONG_DATA_TYPE, e.toString());
}
}