📄 column.java
字号:
* a column of the result of a SELECT statement gives the average for * that column. * * @param a * @param type * @param count * @return result * @throws SQLException */ static Object avg(Object a, int type, int count) throws SQLException { if (a == null || count == 0) { return null; } switch (type) { case Types.NULL : return null; case Types.REAL : case Types.FLOAT : case Types.DOUBLE : return new Double(((Double) a).doubleValue() / count); case Types.NUMERIC : case Types.DECIMAL : return ((BigDecimal) a).divide(new BigDecimal(count), BigDecimal.ROUND_HALF_DOWN); case Types.TINYINT : case Types.SMALLINT : case Types.INTEGER : return new Integer(((Number) a).intValue() / count); case Types.BIGINT : return new Long(((Long) a).longValue() / count); default : throw Trace.error(Trace.SUM_OF_NON_NUMERIC); } } /** * Return the smaller of two objects. * * @param a * @param b * @param type * @return result * @throws SQLException */ static Object min(Object a, Object b, int type) throws SQLException { if (a == null) { return b; } if (b == null) { return a; } if (compare(a, b, type) < 0) { return a; } return b; } /** * Return the larger of two objects. * * @param a * @param b * @param type * @return result * @throws SQLException */ static Object max(Object a, Object b, int type) throws SQLException { if (a == null) { return b; } if (b == null) { return a; } if (compare(a, b, type) > 0) { return a; } return b; }// fredt@users 20020130 - patch 505356 by daniel_fiser@users// modified for performance and made optional private static Collator i18nCollator = Collator.getInstance(); private static boolean sql_compare_in_locale = false; static void setCompareInLocal(boolean value) { sql_compare_in_locale = value; } /** * Compare a with b and return int value as result. * * @param a * @param b * @param type * @return result * @throws SQLException */ static int compare(Object a, Object b, int type) throws SQLException { int i = 0; if (a == b) { return 0; } // null handling: null==null and smaller any value // todo: implement standard SQL null handling // it is also used for grouping ('null' is one group) if (a == null) { if (b == null) { return 0; } return -1; } if (b == null) { return 1; } switch (type) { case Types.NULL : return 0; case Types.VARCHAR : case Types.LONGVARCHAR : if (sql_compare_in_locale) { i = i18nCollator.compare((String) a, (String) b); } else { i = ((String) a).compareTo((String) b); } break;// fredt@users 20020130 - patch 418022 by deforest@users// use of rtrim() to mimic SQL92 behaviour case Types.CHAR : if (sql_compare_in_locale) { i = i18nCollator.compare(Library.rtrim((String) a), Library.rtrim((String) b)); } else { i = (Library.rtrim((String) a)).compareTo( Library.rtrim((String) b)); } break; case VARCHAR_IGNORECASE : if (sql_compare_in_locale) { i = i18nCollator.compare(((String) a).toUpperCase(), ((String) b).toUpperCase()); } else { i = ((String) a).toUpperCase().compareTo( ((String) b).toUpperCase()); } break; 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 : if (((java.sql.Date) a).after((java.sql.Date) b)) { return 1; } else if (((java.sql.Date) a).before((java.sql.Date) b)) { return -1; } else { return 0; } case Types.TIME : if (((Time) a).after((Time) b)) { return 1; } else if (((Time) a).before((Time) b)) { return -1; } else { return 0; } case Types.TIMESTAMP : if (((Timestamp) a).after((Timestamp) b)) { return 1; } else if (((Timestamp) a).before((Timestamp) b)) { return -1; } else { return 0; } case Types.BIT : 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 : i = ByteArray.compareTo((byte[]) a, (byte[]) b); break; case Types.OTHER : return 0; default : throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED, type); } return (i > 0) ? 1 : (i < 0 ? -1 : 0); } /** * Return a java string representation of a java object. * * @param o * @return result (null value for null object) */ private static String convertObject(Object o) { if (o == null) { return null; } return o.toString(); } /** * Convert an object into a Java object that represents its SQL type.<p> * All 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 number that has decimal points * cannot be converted into an integral type, or a very large BIGINT * value cannot be narrowed down to an INTEGER or SMALLINT.<br> * * Integral types may be represented by either Integer or Long. This * works because in the rest of the methods, the java.lang.Number * interface is used to retrieve the values from the object. * * @param o * @param type * @return result * @throws SQLException */ static Object convertObject(Object o, int type) throws SQLException { try { if (o == null) { return null; } switch (type) { case Types.NULL : return null; case Types.TINYINT : if (o instanceof java.lang.String) { o = new Integer((String) o); } if (o instanceof java.lang.Integer || o instanceof java.lang.Long) { int temp = ((Number) o).intValue(); if (Byte.MAX_VALUE < temp || temp < Byte.MIN_VALUE) { throw new java.lang.NumberFormatException(); } // fredt@users - no narrowing for Long values return o; } // fredt@users - direct conversion for JDBC setObject() if (o instanceof java.lang.Byte) { return new Integer(((Number) o).intValue()); } break; case Types.SMALLINT : if (o instanceof java.lang.String) { o = new Integer((String) o); } if (o instanceof java.lang.Integer || o instanceof java.lang.Long) { int temp = ((Number) o).intValue(); if (Short.MAX_VALUE < temp || temp < Short.MIN_VALUE) { throw new java.lang.NumberFormatException(); } // fredt@users - no narrowing for Long values return o; } // fredt@users - direct conversion for JDBC setObject() if (o instanceof java.lang.Byte || o instanceof java.lang.Short) { return new Integer(((Number) o).intValue()); } break; case Types.INTEGER : if (o instanceof java.lang.String) { return new Integer((String) o); } if (o instanceof java.lang.Integer) { return o; } if (o instanceof java.lang.Long) { long temp = ((Number) o).longValue(); if (Integer.MAX_VALUE < temp || temp < Integer.MIN_VALUE) { throw new java.lang.NumberFormatException(); } // fredt@users - narrowing needed for function calls return new Integer(((Number) o).intValue()); } break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -