private Object convertUpdateValue()

in modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetImpl.java [2771:2993]


    private Object convertUpdateValue(int columnIndex, Object value)
            throws SQLException {

        if (value == null) {
            return value;
        }

        Class<?> type = columnTypes[columnIndex - 1];

        /*
         * TODO if type == null, the type mapping is not supported by Harmony
         * now, leave this type check to JDBC driver
         */

        if (type == null) {
            return value;
        }

        // convert to serializable object
        if (type.isInstance(value)) {
            if (type.equals(Array.class) && !(value instanceof SerialArray)) {
                return new SerialArray((Array) value);
            }

            if (type.equals(Blob.class) && !(value instanceof SerialBlob)) {
                return new SerialBlob((Blob) value);
            }

            if (type.equals(Clob.class) && !(value instanceof SerialClob)) {
                return new SerialClob((Clob) value);
            }

            if (type.equals(Ref.class) && !(value instanceof SerialRef)) {
                return new SerialRef((Ref) value);
            }

            return value;
        }

        if (type.equals(byte[].class)) {
            return value;
        }

        if (type.equals(String.class)) {
            if (!(value instanceof Array) && !(value instanceof byte[])) {
                return value.toString();
            }
        }

        if (type.equals(Integer.class)) {
            if (value instanceof Integer || value instanceof Short
                    || value instanceof Byte) {
                return value;
            }

            if (value instanceof Long) {
                long l = ((Long) value).longValue();
                if (l >= Integer.MIN_VALUE && l <= Integer.MAX_VALUE) {
                    return (int) l;
                }
            }

            if (value instanceof BigDecimal) {
                BigDecimal bigDecimal = (BigDecimal) value;
                try {
                    return bigDecimal.intValueExact();

                } catch (ArithmeticException e) {
                    // rowset.10=Data Type Mismatch
                    throw new SQLException(Messages.getString("rowset.10")); //$NON-NLS-1$
                }
            }

            if (value instanceof String) {
                return value;
            }
        }

        if (type.equals(Short.class)) {
            if (value instanceof Short || value instanceof Byte) {
                return value;
            }
            if (value instanceof Long) {
                long l = ((Long) value).longValue();
                if (l >= Short.MIN_VALUE && l <= Short.MAX_VALUE) {
                    return (short) l;
                }
            }
            if (value instanceof Integer) {
                int i = ((Integer) value).intValue();
                if (i >= Short.MIN_VALUE && i <= Short.MAX_VALUE) {
                    return (short) i;
                }
            }
            if (value instanceof BigDecimal) {
                BigDecimal bigDecimal = (BigDecimal) value;
                try {
                    return bigDecimal.intValueExact();

                } catch (ArithmeticException e) {
                    // rowset.10=Data Type Mismatch
                    throw new SQLException(Messages.getString("rowset.10")); //$NON-NLS-1$
                }
            }
            if (value instanceof String) {
                return value;
            }
        }

        if (type.equals(Byte.class)) {
            if (value instanceof Byte) {
                return value;
            }
            if (value instanceof Long) {
                long l = ((Long) value).longValue();
                if (l >= Byte.MIN_VALUE && l <= Byte.MAX_VALUE) {
                    return (byte) l;
                }
            }
            if (value instanceof Integer) {
                int i = ((Integer) value).intValue();
                if (i >= Byte.MIN_VALUE && i <= Byte.MAX_VALUE) {
                    return (byte) i;
                }
            }
            if (value instanceof Short) {
                int i = ((Short) value).shortValue();
                if (i >= Byte.MIN_VALUE && i <= Byte.MAX_VALUE) {
                    return (byte) i;
                }
            }
            if (value instanceof BigDecimal) {
                BigDecimal bigDecimal = (BigDecimal) value;
                try {
                    return bigDecimal.byteValueExact();

                } catch (ArithmeticException e) {
                    // rowset.10=Data Type Mismatch
                    throw new SQLException(Messages.getString("rowset.10")); //$NON-NLS-1$
                }
            }
            if (value instanceof String) {
                return value;
            }
        }

        if (type.equals(Long.class)) {
            if (value instanceof Integer || value instanceof Short
                    || value instanceof Byte || value instanceof Long) {
                return value;
            }
            if (value instanceof BigDecimal) {
                BigDecimal bigDecimal = (BigDecimal) value;
                try {
                    return bigDecimal.longValueExact();

                } catch (ArithmeticException e) {
                    // rowset.10=Data Type Mismatch
                    throw new SQLException(Messages.getString("rowset.10")); //$NON-NLS-1$
                }
            }
            if (value instanceof String) {
                return value;
            }
        }

        if (type.equals(Float.class) || type.equals(Double.class)) {
            if (value instanceof Float || value instanceof Double
                    || value instanceof BigDecimal) {
                return value;
            }
            if (value instanceof Number) {
                return ((Number) value).longValue();
            }
            if (value instanceof String) {
                return value;
            }
        }

        if (type.equals(BigDecimal.class)) {
            return value;
        }

        if (type.equals(Date.class)) {
            if (value instanceof Timestamp) {
                Timestamp timestamp = (Timestamp) value;
                return new Date(timestamp.getTime());
            }

            if (value instanceof String) {
                return value;
            }
        }

        if (type.equals(Time.class)) {
            if (value instanceof Timestamp) {
                Timestamp timestamp = (Timestamp) value;
                return new Time(timestamp.getTime());
            }

            if (value instanceof String) {
                return value;
            }
        }

        if (type.equals(Timestamp.class)) {
            if (value instanceof Date) {
                Date date = (Date) value;
                return new Timestamp(date.getTime());
            }
            if (value instanceof Time) {
                Time time = (Time) value;
                return new Timestamp(time.getTime());
            }

            if (value instanceof String) {
                return value;
            }
        }

        // rowset.10=Data Type Mismatch
        throw new SQLException(Messages.getString("rowset.10")); //$NON-NLS-1$
    }