📄 column.java
字号:
case Types.BIGINT : return "BIGINT"; case Types.REAL : return "REAL"; case Types.FLOAT : return "FLOAT"; case Types.NUMERIC : return "NUMERIC"; case Types.TIMESTAMP : return "TIMESTAMP"; case Types.BINARY : return "BINARY"; case Types.VARBINARY : return "VARBINARY"; case Types.LONGVARBINARY : return "LONGVARBINARY"; case Types.OTHER : return "OBJECT"; default : throw Trace.error(Trace.WRONG_DATA_TYPE, type); } } /** * Add two object of a given type * * @param a * @param b * @param type * @return result * @throws SQLException */ static Object add(Object a, Object b, int type) throws SQLException { if (a == null || b == null) { return null; } switch (type) { case Types.NULL : return null; case Types.REAL : case Types.FLOAT : case Types.DOUBLE : double ad = ((Number) a).doubleValue(); double bd = ((Number) b).doubleValue(); return new Double(ad + bd); case Types.VARCHAR : case Types.CHAR : case Types.LONGVARCHAR : case VARCHAR_IGNORECASE : return (String) a + (String) b; case Types.NUMERIC : case Types.DECIMAL : BigDecimal abd = (BigDecimal) a; BigDecimal bbd = (BigDecimal) b; return abd.add(bbd); case Types.TINYINT : case Types.SMALLINT : case Types.INTEGER : int ai = ((Number) a).intValue(); int bi = ((Number) b).intValue(); return new Integer(ai + bi); case Types.BIGINT : long longa = ((Number) a).longValue(); long longb = ((Number) b).longValue(); return new Long(longa + longb); default : throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, type); } } /** * Concat two objects by turning them into strings first. * * @param a * @param b * @return result * @throws SQLException */ static Object concat(Object a, Object b) throws SQLException { if (a == null) { return b; } else if (b == null) { return a; } return convertObject(a) + convertObject(b); } /** * Negate a numeric object. * * @param a * @param type * @return result * @throws SQLException */ static Object negate(Object a, int type) throws SQLException { if (a == null) { return null; } switch (type) { case Types.NULL : return null; case Types.REAL : case Types.FLOAT : case Types.DOUBLE : return new Double(-((Number) a).doubleValue()); case Types.NUMERIC : case Types.DECIMAL : return ((BigDecimal) a).negate(); case Types.TINYINT : case Types.SMALLINT : case Types.INTEGER : return new Integer(-((Number) a).intValue()); case Types.BIGINT : return new Long(-((Number) a).longValue()); default : throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, type); } } /** * Multiply two numeric objects. * * @param a * @param b * @param type * @return result * @throws SQLException */ static Object multiply(Object a, Object b, int type) throws SQLException { if (a == null || b == null) { return null; } switch (type) { case Types.NULL : return null; case Types.REAL : case Types.FLOAT : case Types.DOUBLE : double ad = ((Number) a).doubleValue(); double bd = ((Number) b).doubleValue(); return new Double(ad * bd); case Types.NUMERIC : case Types.DECIMAL : BigDecimal abd = (BigDecimal) a; BigDecimal bbd = (BigDecimal) b; return abd.multiply(bbd); case Types.TINYINT : case Types.SMALLINT : case Types.INTEGER : int ai = ((Number) a).intValue(); int bi = ((Number) b).intValue(); return new Integer(ai * bi); case Types.BIGINT : long longa = ((Number) a).longValue(); long longb = ((Number) b).longValue(); return new Long(longa * longb); default : throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, type); } } /** * Divide numeric object a by object b. * * @param a * @param b * @param type * @return result * @throws SQLException */ static Object divide(Object a, Object b, int type) throws SQLException { if (a == null || b == null) { return null; } switch (type) { case Types.NULL : return null; case Types.REAL : case Types.FLOAT : case Types.DOUBLE : double ad = ((Number) a).doubleValue(); double bd = ((Number) b).doubleValue(); return new Double(ad / bd); case Types.NUMERIC : case Types.DECIMAL : BigDecimal abd = (BigDecimal) a; BigDecimal bbd = (BigDecimal) b; int scale = abd.scale() > bbd.scale() ? abd.scale() : bbd.scale(); return (bbd.signum() == 0) ? null : abd.divide(bbd, scale, BigDecimal.ROUND_HALF_DOWN); case Types.TINYINT : case Types.SMALLINT : case Types.INTEGER : int ai = ((Number) a).intValue(); int bi = ((Number) b).intValue(); Trace.check(bi != 0, Trace.DIVISION_BY_ZERO); return new Integer(ai / bi); case Types.BIGINT : long longa = ((Number) a).longValue(); long longb = ((Number) b).longValue(); return (longb == 0) ? null : new Long(longa / longb); default : throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, type); } } /** * Subtract numeric object b from object a. * * @param a * @param b * @param type * @return result * @throws SQLException */ static Object subtract(Object a, Object b, int type) throws SQLException { if (a == null || b == null) { return null; } switch (type) { case Types.NULL : return null; case Types.REAL : case Types.FLOAT : case Types.DOUBLE : double ad = ((Number) a).doubleValue(); double bd = ((Number) b).doubleValue(); return new Double(ad - bd); case Types.NUMERIC : case Types.DECIMAL : BigDecimal abd = (BigDecimal) a; BigDecimal bbd = (BigDecimal) b; return abd.subtract(bbd); case Types.TINYINT : case Types.SMALLINT : case Types.INTEGER : int ai = ((Number) a).intValue(); int bi = ((Number) b).intValue(); return new Integer(ai - bi); case Types.BIGINT : long longa = ((Number) a).longValue(); long longb = ((Number) b).longValue(); return new Long(longa - longb); default : throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, type); } } /** * Add two numeric objects. * * @param a * @param b * @param type * @return result * @throws SQLException */ static Object sum(Object a, Object b, int type) throws SQLException { if (a == null) { return b; } if (b == null) { return a; } switch (type) { case Types.NULL : return null; case Types.REAL : case Types.FLOAT : case Types.DOUBLE : return new Double(((Number) a).doubleValue() + ((Number) b).doubleValue()); case Types.NUMERIC : case Types.DECIMAL : return ((BigDecimal) a).add((BigDecimal) b); case Types.TINYINT : case Types.SMALLINT : case Types.INTEGER : return new Integer(((Number) a).intValue() + ((Number) b).intValue()); case Types.BIGINT : return new Long(((Number) a).longValue() + ((Number) b).longValue()); default : throw Trace.error(Trace.SUM_OF_NON_NUMERIC); } } /** * Divide numeric object a by int count. Adding all of these values in
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -