📄 column.java
字号:
case Types.BIGINT : if (o instanceof java.lang.Long) { return o; } if (o instanceof java.lang.String) { return new Long((String) o); } if (o instanceof java.lang.Integer) { return new Long(((Integer) o).longValue()); } break; case Types.REAL : case Types.FLOAT : case Types.DOUBLE : if (o instanceof java.lang.Double) { return o; } if (o instanceof java.lang.String) { return new Double((String) o); } if (o instanceof java.lang.Number) { return new Double(((Number) o).doubleValue()); } break; case Types.NUMERIC : case Types.DECIMAL : if (o instanceof java.math.BigDecimal) { return o; } break; case Types.BIT : if (o instanceof java.lang.Boolean) { return o; } if (o instanceof java.lang.String) { return new Boolean((String) o); } if (o instanceof Integer || o instanceof Long) { boolean bit = ((Number) o).longValue() == 0L ? false : true; return new Boolean(bit); } if (o instanceof java.lang.Double) { boolean bit = ((Double) o).doubleValue() == 0.0 ? false : true; return new Boolean(bit); } if (o instanceof java.math.BigDecimal) { boolean bit = ((BigDecimal) o).compareTo(BIGDECIMAL_0) == 0 ? false : true; return new Boolean(bit); } break; case VARCHAR_IGNORECASE : case Types.VARCHAR : case Types.CHAR : case Types.LONGVARCHAR : if (o instanceof java.lang.String) { return o; } if (o instanceof byte[]) { return ByteArray.toString((byte[]) o); } break; case Types.TIME : if (o instanceof java.sql.Timestamp) { return new Time(((Timestamp) o).getTime()); } if (o instanceof java.sql.Date) { return new Time(0); } break; case Types.DATE : if (o instanceof java.sql.Timestamp) { return new java.sql.Date(((Timestamp) o).getTime()); } break; case Types.BINARY : case Types.VARBINARY : case Types.LONGVARBINARY : if (o instanceof byte[]) { return o; } break;// fredt@users 20020328 - patch 482109 by fredt - OBJECT handling// currently String objects cannot be stored directly in OTHER columns// all strings are treated as a hex representation of a serialized Object// a new escape pattern needs to be established to differentiate between// SQL strings that are normal strings and those that represent a hex version// of the BINARY or OTHER data case Types.OTHER : if (o instanceof String == false) { return o; } default : } return convertString(o.toString(), type); } catch (SQLException e) { throw e; } catch (Exception e) { throw Trace.error(Trace.WRONG_DATA_TYPE, e.getMessage()); } } /** * Return a java object based on a SQL string. This is called from * convertObject(Object o, int type). * * @param s * @param type * @return * @throws SQLException */ private static Object convertString(String s, int type) throws SQLException { switch (type) { case Types.TINYINT : case Types.SMALLINT : // fredt - do maximumm / minimum checks on each type return convertObject(s, type); case Types.INTEGER : return new Integer(s); case Types.BIGINT : return new Long(s); case Types.REAL : case Types.FLOAT : case Types.DOUBLE : return new Double(s); case 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 : return new BigDecimal(s.trim()); case Types.BIT : return new Boolean(s); case Types.BINARY : case Types.VARBINARY : case Types.LONGVARBINARY : return ByteArray.hexToByteArray(s); case Types.OTHER : return ByteArray.deserialize(ByteArray.hexToByteArray(s)); default : throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, 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 SQLException */ static String createSQLString(Object o, int type) throws SQLException { 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 : return StringConverter.toQuotedString( ByteArray.toString((byte[]) o), '\'', false); case Types.OTHER : return StringConverter.toQuotedString( ByteArray.serializeToString(o), '\'', false); case VARCHAR_IGNORECASE : case Types.VARCHAR : case Types.CHAR : case Types.LONGVARCHAR : return createSQLString((String) o); default : return o.toString(); } } static String createSQLString(double x) throws SQLException { 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 java string * @return quoted SQL string */ static String createSQLString(String s) { return StringConverter.toQuotedString(s, '\'', true); }// fredt@users 20020408 - patch 442993 by fredt - arithmetic expressions /** * Arithmetic expressions terms are promoted to a type that can * represent the resulting values and avoid incorrect results.<p> * When the result or the expression is converted to the * type of the target column for storage, an exception is thrown if the * resulting value cannot be stored in the column<p> * Returns a SQL type "wide" enough to represent the result of the * expression.<br> * A type is "wider" than the other if it can represent all its * numeric values.<BR> * Types narrower than INTEGER (int) are promoted to * INTEGER. The order is as follows<p> * * INTEGER, BIGINT, DOUBLE, DECIMAL<p> * * TINYINT and SMALLINT in any combination return INTEGER<br> * INTEGER and INTEGER return BIGINT<br> * BIGINT and INTEGER return NUMERIC/DECIMAL<br> * BIGINT and BIGINT return NUMERIC/DECIMAL<br> * DOUBLE and INTEGER return DOUBLE<br> * DOUBLE and BIGINT return DOUBLE<br> * NUMERIC/DECIMAL and any type returns NUMERIC/DECIMAL<br> * * @author fredt@users * @param type1 java.sql.Types value for the first numeric type * @param type2 java.sql.Types value for the second numeric type * @return either type1 or type2 on the basis of the above order */ static int getCombinedNumberType(int type1, int type2, int expType) { int typeWidth1 = getNumTypeWidth(type1); int typeWidth2 = getNumTypeWidth(type2); if (typeWidth1 == 16 || typeWidth2 == 16) { return Types.DOUBLE; } if (expType != Expression.DIVIDE) { if (typeWidth1 + typeWidth2 <= 4) { return Types.INTEGER; } if (typeWidth1 + typeWidth2 <= 8) { return Types.BIGINT; } if (typeWidth1 + typeWidth2 <= 16) { return Types.NUMERIC; } } return (typeWidth1 > typeWidth2) ? type1 : type2; } /** * @param java.sql.Types int for a numeric type * @return relative width */ private static int getNumTypeWidth(int type) { switch (type) { case Types.TINYINT : return 1; case Types.SMALLINT : return 2; case Types.INTEGER : return 4; case Types.BIGINT : return 8; case Types.REAL : case Types.FLOAT : case Types.DOUBLE : return 16; case Types.NUMERIC : case Types.DECIMAL : return 32; default : return 32; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -