📄 column.java
字号:
int type) throws HsqlException { if (a == null || b == null) { return null; }// fredt@users - type conversion - may need to apply to other arithmetic operations too if (!(a instanceof Number && b instanceof Number)) { a = Column.convertObject(a, type); b = Column.convertObject(b, type); } 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 ValuePool.getDouble(Double.doubleToLongBits(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 ValuePool.getInt(ai * bi); } case Types.BIGINT : { long longa = ((Number) a).longValue(); long longb = ((Number) b).longValue(); return ValuePool.getLong(longa * longb); } default : throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, Types.getTypeString(type)); } } /** * Divide numeric object a by object b. * * @param a * @param b * @param type * @return result * @throws HsqlException */ static Object divide(Object a, Object b, int type) throws HsqlException { 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 ValuePool.getDouble(Double.doubleToLongBits(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(); if (bi == 0) { throw Trace.error(Trace.DIVISION_BY_ZERO); } return ValuePool.getInt(ai / bi); } case Types.BIGINT : { long longa = ((Number) a).longValue(); long longb = ((Number) b).longValue(); return (longb == 0) ? null : ValuePool.getLong(longa / longb); } default : throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, Types.getTypeString(type)); } } /** * Subtract numeric object b from object a. * * @param a * @param b * @param type * @return result * @throws HsqlException */ static Object subtract(Object a, Object b, int type) throws HsqlException { 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 ValuePool.getDouble(Double.doubleToLongBits(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 ValuePool.getInt(ai - bi); } case Types.BIGINT : { long longa = ((Number) a).longValue(); long longb = ((Number) b).longValue(); return ValuePool.getLong(longa - longb); } default : throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, Types.getTypeString(type)); } }// boucherb@users 2003-09-25// TODO: Maybe use //#ifdef tag or reflective static method attribute// instantiation to take advantage of String.compareToIgnoreCase when// available (JDK 1.2 and greater) during ANT build. That or perhaps// consider using either local character-wise comparison or first converting// to lower case and then to upper case. Sun states that the JDK 1.2 introduced// String.compareToIngnorCase() comparison involves calling// Character.toLowerCase(Character.toUpperCase()) on compared characters,// to correctly handle some caveats concering using only the one operation or// the other outside the ascii character range.// fredt@users 20020130 - patch 418022 by deforest@users// use of rtrim() to mimic SQL92 behaviour /** * Compare a with b and return int value as result. * * @param a instance of Java wrapper, depending on type, but always same for a & b (can be null) * @param b instance of Java wrapper, depending on type, but always same for a & b (can be null) * @param type one of the java.sql.Types * @return result 1 if a>b, 0 if a=b, -1 if b>a * @throws HsqlException */ static int compare(Collation collation, Object a, Object b, int type) { int i = 0; if (a == b) { return 0; } // Current null handling here: null==null and smaller any value // Note, standard SQL null handling is handled by Expression.test() calling testNull() instead of this! // Attention, this is also used for grouping ('null' is one group) if (a == null) { return -1; } if (b == null) { return 1; } switch (type) { case Types.NULL : return 0; case Types.VARCHAR : case Types.LONGVARCHAR : return collation.compare((String) a, (String) b); case Types.CHAR : return collation.compare(Library.rtrim((String) a), Library.rtrim((String) b)); case Types.VARCHAR_IGNORECASE : return collation.compareIgnoreCase(((String) a), ((String) b)); case Types.TINYINT : case Types.SMALLINT : case Types.INTEGER : { int ai = ((Number) a).intValue(); int bi = ((Number) b).intValue(); return (ai > bi) ? 1 : (bi > ai ? -1 : 0); } case Types.BIGINT : { long longa = ((Number) a).longValue(); long longb = ((Number) b).longValue(); return (longa > longb) ? 1 : (longb > longa ? -1 : 0); } case Types.REAL : case Types.FLOAT : case Types.DOUBLE : { double ad = ((Number) a).doubleValue(); double bd = ((Number) b).doubleValue(); return (ad > bd) ? 1 : (bd > ad ? -1 : 0); } case Types.NUMERIC : case Types.DECIMAL : i = ((BigDecimal) a).compareTo((BigDecimal) b); break; case Types.DATE : return HsqlDateTime.compare((Date) a, (Date) b); case Types.TIME : return HsqlDateTime.compare((Time) a, (Time) b); case Types.TIMESTAMP : return HsqlDateTime.compare((Timestamp) a, (Timestamp) b); case Types.BOOLEAN : { boolean boola = ((Boolean) a).booleanValue(); boolean boolb = ((Boolean) b).booleanValue(); return (boola == boolb) ? 0 : (boolb ? -1 : 1); } case Types.BINARY : case Types.VARBINARY : case Types.LONGVARBINARY : if (a instanceof Binary && b instanceof Binary) { i = compareTo(((Binary) a).getBytes(), ((Binary) b).getBytes()); } break; case Types.OTHER : return 0; } return (i == 0) ? 0 : (i < 0 ? -1 : 1); } /** * Convert an object into a Java object that represents its SQL type.<p> * All internal type conversion operations start with * this method. If a direct conversion doesn't take place, the object * is converted into a string first and an attempt is made to convert * the string into the target type.<br> * * One objective of this mehod is to ensure the Object can be converted * to the given SQL type. For example, a very large BIGINT * value cannot be narrowed down to an INTEGER or SMALLINT.<br> * * Type conversion performed by this method has gradually evolved in 1.7.2 * to allow narrowing of numeric types in all cases according to the SQL * standard.<br> * * Another new objective is to normalize DATETIME values.<br> * * Objects set via JDBC PreparedStatement use this method to convert * the data to the Java type that is required for custom serialization * by the engine. <br> * * @param o * @param type * @return result * @throws HsqlException */ 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -