📄 column.java
字号:
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()); } } /** * Return a java object based on a SQL string. This is called from * convertObject(Object o, int type). * * @param s * @param type * @return * @throws HsqlException */ private static Object convertString(String s, int type) throws HsqlException { switch (type) { case Types.TINYINT : case Types.SMALLINT : // fredt - do maximumm / minimum checks on each type return convertObject(s, type); case Types.INTEGER : int val = Integer.parseInt(s); return ValuePool.getInt(val); case Types.BIGINT : return ValuePool.getLong(Long.parseLong(s)); case Types.REAL : case Types.FLOAT : case Types.DOUBLE : double d = JavaSystem.parseDouble(s); long l = Double.doubleToLongBits(d); return ValuePool.getDouble(l); case Types.VARCHAR_IGNORECASE : case Types.VARCHAR : case Types.CHAR : case Types.LONGVARCHAR : return s; case Types.DATE : return HsqlDateTime.dateValue(s); case Types.TIME : return HsqlDateTime.timeValue(s); case Types.TIMESTAMP : return HsqlDateTime.timestampValue(s); case Types.NUMERIC : case Types.DECIMAL : s = Library.trim(s, " ", true, true); return new BigDecimal(s); case Types.BOOLEAN : return s.equalsIgnoreCase("TRUE") ? Boolean.TRUE : Boolean.FALSE; case Types.BINARY : case Types.VARBINARY : case Types.LONGVARBINARY : case Types.OTHER : default : throw Trace.error(Trace.INVALID_CONVERSION, Types.getTypeString(type)); } } /** * Return an SQL representation of an object. Strings will be quoted * with single quotes, other objects will represented as in a SQL * statement. * * @param o * @param type * @return result * @throws HsqlException */ static String createSQLString(Object o, int type) throws HsqlException { if (o == null) { return "NULL"; } switch (type) { case Types.NULL : return "NULL"; case Types.REAL : case Types.FLOAT : case Types.DOUBLE : return createSQLString(((Number) o).doubleValue()); case Types.DATE : case Types.TIME : case Types.TIMESTAMP : return StringConverter.toQuotedString(o.toString(), '\'', false); case Types.BINARY : case Types.VARBINARY : case Types.LONGVARBINARY : if (!(o instanceof Binary)) { throw Trace.error(Trace.INVALID_CONVERSION); } return StringConverter.toQuotedString( StringConverter.byteToHex(((Binary) o).getBytes()), '\'', false); case Types.OTHER : if (!(o instanceof JavaObject)) { throw Trace.error(Trace.SERIALIZATION_FAILURE); } return StringConverter.toQuotedString( StringConverter.byteToHex(((JavaObject) o).getBytes()), '\'', false); case Types.VARCHAR_IGNORECASE : case Types.VARCHAR : case Types.CHAR : case Types.LONGVARCHAR : return createSQLString((String) o); default : return o.toString(); } } public static String createSQLString(double x) { if (x == Double.NEGATIVE_INFINITY) { return "-1E0/0"; } if (x == Double.POSITIVE_INFINITY) { return "1E0/0"; } if (Double.isNaN(x)) { return "0E0/0E0"; } String s = Double.toString(x); // ensure the engine treats the value as a DOUBLE, not DECIMAL if (s.indexOf('E') < 0) { s = s.concat("E0"); } return s; } /** * Turns a java string into a quoted SQL string * * @param s java string * @return quoted SQL string */ public static String createSQLString(String s) { if (s == null) { return "NULL"; } return StringConverter.toQuotedString(s, '\'', true); } /** * Explicit casts are handled here. This is separate from the implicit * casts carried out when inserting/updating rows. * SQL standard 6.12 rules for enforcement of size, precision and scale * are implemented here are as follows: * * For no size, precision or scale, default to convertObject(Object) * * Right truncation is allowed only for CHAR to CHAR casts * (CHAR is generic for all string types). * * For other casts to CHAR, right truncation is not allowed. * * For numeric conversions, scale is always converted to target first, * then precision is imposed. No truncation is allowed. (fredt) */ public static Object convertObject(Session session, Object o, int type, int precision, int scale) throws HsqlException { if (o == null) { return o; } if (precision == 0) { return convertObject(o, type); } boolean check = true; switch (type) { case Types.VARCHAR_IGNORECASE : case Types.LONGVARCHAR : type = Types.VARCHAR; case Types.VARCHAR : case Types.CHAR : if (o instanceof String) { check = false; } else { o = convertObject(o, Types.VARCHAR); } return enforceSize(o, type, precision, scale, check); case Types.NUMERIC : case Types.DECIMAL : if (!(o instanceof BigDecimal)) { o = convertObject(o, type);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -