crossconverters.java

来自「derby database source code.good for you.」· Java 代码 · 共 1,410 行 · 第 1/4 页

JAVA
1,410
字号
        case Types.REAL:            if (Configuration.rangeCheckCrossConverters &&                    (source > Float.MAX_VALUE || source < -Float.MAX_VALUE)) {                throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));            }            return new Float((float) source);        case Types.DOUBLE:            if (Configuration.rangeCheckCrossConverters &&                    // change the check from (source > Double.MAX_VALUE || source < -Double.MIN_VALUE))                    // to the following:                    //-------------------------------------------------------------------------------------                    //    -infinity                             0                            +infinity                    //            |__________________________|======|________________________|                    // <-1.79E+308|                          |      |                        |>+1.79E+308                    //            |                          |      |_________________       |                    //            |                          |-4.9E-324 <X< +4.9E-324                    //            |                          |________________________                    //-------------------------------------------------------------------------------------                    (source == Double.POSITIVE_INFINITY || source == Double.NEGATIVE_INFINITY)) {                throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));            }            return new Double(source);        case Types.DECIMAL:            return new java.math.BigDecimal(String.valueOf(source));  // This matches derby semantics        case Types.CHAR:        case Types.VARCHAR:        case Types.LONGVARCHAR:            return String.valueOf(source);        default:            throw new SqlException(agent_.logWriter_, "Illegal Conversion");        }    }    // Convert from big decimal source to target type    // In support of PS.setBigDecimal()    final Object setObject(int targetType, java.math.BigDecimal source) throws SqlException {        switch (targetType) {        case Types.SMALLINT:            if (Configuration.rangeCheckCrossConverters &&                    (source.compareTo(bdMaxShortValue__) == 1 || source.compareTo(bdMinShortValue__) == -1)) {                throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));            }            return new Short(source.shortValue());        case Types.INTEGER:            if (Configuration.rangeCheckCrossConverters &&                    (source.compareTo(bdMaxIntValue__) == 1 || source.compareTo(bdMinIntValue__) == -1)) {                throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));            }            return new Integer(source.intValue());        case Types.BIGINT:            if (Configuration.rangeCheckCrossConverters &&                    (source.compareTo(bdMaxLongValue__) == 1 || source.compareTo(bdMinLongValue__) == -1)) {                throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));            }            return new Long(source.longValue());        case Types.REAL:            if (Configuration.rangeCheckCrossConverters &&                    (source.compareTo(bdMaxFloatValue__) == 1 || source.compareTo(bdMinFloatValue__) == -1)) {                throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));            }            return new Float(source.floatValue());        case Types.DOUBLE:            if (Configuration.rangeCheckCrossConverters &&                    (source.compareTo(bdMaxDoubleValue__) == 1 || source.compareTo(bdMinDoubleValue__) == -1)) {                throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));            }            return new Double(source.doubleValue());        case Types.DECIMAL:            return source;        case Types.CHAR:        case Types.VARCHAR:        case Types.LONGVARCHAR:            return String.valueOf(source);        default:            throw new SqlException(agent_.logWriter_, "Illegal Conversion");        }    }    // Convert from date source to target type    // In support of PS.setDate()    final Object setObject(int targetType, java.sql.Date source) throws SqlException {        switch (targetType) {        case java.sql.Types.DATE:            return source;        case java.sql.Types.TIMESTAMP:            return new java.sql.Timestamp(source.getTime());        case java.sql.Types.CHAR:        case java.sql.Types.VARCHAR:        case java.sql.Types.LONGVARCHAR:            return String.valueOf(source);        default:            throw new SqlException(agent_.logWriter_, "Illegal Conversion");        }    }    // Convert from time source to target type    // In support of PS.setTime()    final Object setObject(int targetType, java.sql.Time source) throws SqlException {        switch (targetType) {        case java.sql.Types.TIME:            return source;        case java.sql.Types.CHAR:        case java.sql.Types.VARCHAR:        case java.sql.Types.LONGVARCHAR:            return String.valueOf(source);        default:            throw new SqlException(agent_.logWriter_, "Illegal Conversion");        }    }    // Convert from date source to target type    // In support of PS.setTimestamp()    final Object setObject(int targetType, java.sql.Timestamp source) throws SqlException {        switch (targetType) {        case java.sql.Types.TIMESTAMP:            return source;        case java.sql.Types.TIME:            return new java.sql.Time(source.getTime());        case java.sql.Types.DATE:            return new java.sql.Date(source.getTime());        case java.sql.Types.CHAR:        case java.sql.Types.VARCHAR:        case java.sql.Types.LONGVARCHAR:            return String.valueOf(source);        default:            throw new SqlException(agent_.logWriter_, "Illegal Conversion");        }    }    // setString() against BINARY columns cannot be implemented consistently because w/out metadata, we'll send char encoding bytes.    // So we refuse setString() requests altogether.    // Convert from string source to target type.    // In support of PS.setString()    final Object setObject(int targetDriverType, String source) throws SqlException {        try {            switch (targetDriverType) {            case Types.SMALLINT:                return Short.valueOf(source);            case Types.INTEGER:                return Integer.valueOf(source);            case Types.BIGINT:                return Long.valueOf(source);            case Types.REAL:                return Float.valueOf(source);            case Types.DOUBLE:                return Double.valueOf(source);            case Types.DECIMAL:                return new java.math.BigDecimal(source);            case java.sql.Types.DATE:                return date_valueOf(source);            case java.sql.Types.TIME:                return time_valueOf(source);            case java.sql.Types.TIMESTAMP:                return timestamp_valueOf(source);            case Types.CHAR:            case Types.VARCHAR:            case Types.LONGVARCHAR:                return source;            case Types.CLOB:                return new Clob(agent_, source);                // setString() against BINARY columns is problematic because w/out metadata, we'll send char encoding bytes.                // So we refuse setString() requests altogether.            case Types.BINARY:            case Types.VARBINARY:            case Types.LONGVARBINARY:            case Types.BLOB:            default:                throw new SqlException(agent_.logWriter_, "Illegal Conversion");            }        } catch (java.lang.NumberFormatException e) {            throw new SqlException(agent_.logWriter_,                    e,                    "Invalid data conversion:"                    + " Parameter instance "                    + source                    + " is invalid for requested conversion.");        }    }    // ------ method to convert to targetJdbcType ------    /**     * Convert the input targetJdbcType to the correct JdbcType used by CrossConverters.     */    public static int getInputJdbcType(int jdbcType) {        switch (jdbcType) {        case java.sql.Types.BIT:        case java.sql.Types.BOOLEAN:        case java.sql.Types.TINYINT:        case java.sql.Types.SMALLINT:            return java.sql.Types.INTEGER;        case java.sql.Types.NUMERIC:            return java.sql.Types.DECIMAL;        case java.sql.Types.FLOAT:            return java.sql.Types.DOUBLE;        default:            return jdbcType;        }    }    // -- methods in support of setObject(String)/getString() on BINARY columns---    // Convert from byte[] source to target type    // In support of PS.setBytes()    final Object setObject(int targetType, byte[] source) throws SqlException {        switch (targetType) {        case Types.BINARY:        case Types.VARBINARY:        case Types.LONGVARBINARY:            return source;        case Types.BLOB:            return new Blob(source, agent_, 0);        default:            throw new SqlException(agent_.logWriter_, "Illegal Conversion");        }    }    // Convert from Reader source to target type    // In support of PS.setCharacterStream()    final Object setObject(int targetType, java.io.Reader source, int length) throws SqlException {        switch (targetType) {        case Types.CHAR:        case Types.VARCHAR:        case Types.LONGVARCHAR:            return setStringFromReader(source, length);        case Types.CLOB:            return new Clob(agent_, source, length);            // setCharacterStream() against BINARY columns is problematic because w/out metadata, we'll send char encoding bytes.            // There's no clean solution except to just not support setObject(String/Reader/Stream)        case Types.BINARY:        case Types.VARBINARY:        case Types.LONGVARBINARY:        case Types.BLOB:        default:            throw new SqlException(agent_.logWriter_, "Illegal Conversion");        }    }    // create a String by reading all of the bytes from reader    private final String setStringFromReader(java.io.Reader r, int length) throws SqlException {        java.io.StringWriter sw = new java.io.StringWriter();        try {            int read = r.read();            int totalRead = 0;            while (read != -1) {                totalRead++;                sw.write(read);                read = r.read();            }            if (length != totalRead) {                throw new SqlException(agent_.logWriter_, "The Reader object does not contain length characters");            }            return sw.toString();        } catch (java.io.IOException e) {            throw new SqlException(agent_.logWriter_, e.getMessage());        }    }    // Convert from InputStream source to target type.    // In support of PS.setAsciiStream, PS.setUnicodeStream    // Note: PS.setCharacterStream() is handled by setObject(Reader)    final Object setObjectFromCharacterStream(int targetType, java.io.InputStream source, String encoding, int length) throws SqlException {        switch (targetType) {        case Types.CHAR:        case Types.VARCHAR:        case Types.LONGVARCHAR:            return setStringFromStream(source, encoding, length);        case Types.CLOB:            return new Clob(agent_, source, encoding, length);        case Types.BINARY:        case Types.VARBINARY:        case Types.LONGVARBINARY:        case Types.BLOB:        default:            throw new SqlException(agent_.logWriter_, "Illegal Conversion");        }    }    // create a String by reading all of the bytes from inputStream, applying encoding    private final String setStringFromStream(java.io.InputStream is, String encoding, int length) throws SqlException {        try {            java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();            int totalRead = 0;            try {                int read = is.read();                while (read != -1) {                    totalRead++;                    baos.write(read);                    read = is.read();                }            } catch (java.io.IOException e) {                throw new SqlException(agent_.logWriter_, e.getMessage());            }            if (length != totalRead) {                throw new SqlException(agent_.logWriter_, "The InputStream object does not contain length bytes");            }            return new String(baos.toByteArray(), encoding);        } catch (java.io.UnsupportedEncodingException e) {            throw new SqlException(agent_.logWriter_, e.getMessage());        }    }    // Convert from Blob source to target type    // In support of PS.setBlob()    final Object setObject(int targetType, java.sql.Blob source) throws SqlException {        switch (targetType) {        case Types.BLOB:            return source;        case Types.BINARY:        case Types.VARBINARY:        case Types.LONGVARBINARY:            try {                return source.getBytes(1L, (int) source.length());

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?