crossconverters.java
来自「derby database source code.good for you.」· Java 代码 · 共 1,410 行 · 第 1/4 页
JAVA
1,410 行
/* Derby - Class org.apache.derby.client.am.CrossConverters Copyright (c) 2001, 2005 The Apache Software Foundation or its licensors, where applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.*/package org.apache.derby.client.am;// All currently supported derby types are mapped to one of the following jdbc types:// java.sql.Types.SMALLINT;// java.sql.Types.INTEGER;// java.sql.Types.BIGINT;// java.sql.Types.REAL;// java.sql.Types.DOUBLE;// java.sql.Types.DECIMAL;// java.sql.Types.DATE;// java.sql.Types.TIME;// java.sql.Types.TIMESTAMP;// java.sql.Types.CHAR;// java.sql.Types.VARCHAR;// java.sql.Types.LONGVARCHAR;// java.sql.Types.CLOB;// java.sql.Types.BLOB;//final class CrossConverters { private final static java.math.BigDecimal bdMaxByteValue__ = java.math.BigDecimal.valueOf(Byte.MAX_VALUE); private final static java.math.BigDecimal bdMinByteValue__ = java.math.BigDecimal.valueOf(Byte.MIN_VALUE); private final static java.math.BigDecimal bdMaxShortValue__ = java.math.BigDecimal.valueOf(Short.MAX_VALUE); private final static java.math.BigDecimal bdMinShortValue__ = java.math.BigDecimal.valueOf(Short.MIN_VALUE); private final static java.math.BigDecimal bdMaxIntValue__ = java.math.BigDecimal.valueOf(Integer.MAX_VALUE); private final static java.math.BigDecimal bdMinIntValue__ = java.math.BigDecimal.valueOf(Integer.MIN_VALUE); private final static java.math.BigDecimal bdMaxLongValue__ = java.math.BigDecimal.valueOf(Long.MAX_VALUE); private final static java.math.BigDecimal bdMinLongValue__ = java.math.BigDecimal.valueOf(Long.MIN_VALUE); private final static java.math.BigDecimal bdMaxFloatValue__ = new java.math.BigDecimal(Float.MAX_VALUE); private final static java.math.BigDecimal bdMinFloatValue__ = new java.math.BigDecimal(-Float.MAX_VALUE); private final static java.math.BigDecimal bdMaxDoubleValue__ = new java.math.BigDecimal(Double.MAX_VALUE); private final static java.math.BigDecimal bdMinDoubleValue__ = new java.math.BigDecimal(-Double.MAX_VALUE); // Since BigDecimals are immutable, we can return pointers to these canned 0's and 1's. private final static java.math.BigDecimal bdZero__ = java.math.BigDecimal.valueOf(0); private final static java.math.BigDecimal bdOne__ = java.math.BigDecimal.valueOf(1); // ---------------------- state ---------------------------------------------- Agent agent_; // ----------------------constructors/finalizer------------------------------- CrossConverters(Agent agent) { agent_ = agent; } // --------------------------------------------------------------------------- // The following methods are used for input cross conversion. // --------------------------------------------------------------------------- //---------------------------- setObject() methods --------------------------- // Convert from boolean source to target type. // In support of PS.setBoolean(). // See differences.html for DNC setBoolean() semantics. final Object setObject(int targetType, boolean source) throws SqlException { return setObject(targetType, (short) (source ? 1 : 0)); } // Convert from byte source to target type // In support of PS.setByte() final Object setObject(int targetType, byte source) throws SqlException { return setObject(targetType, (short) source); } // Convert from short source to target type // In support of PS.setShort() final Object setObject(int targetType, short source) throws SqlException { switch (targetType) { case Types.SMALLINT: return new Short(source); case Types.INTEGER: return new Integer(source); case Types.BIGINT: return new Long(source); case Types.REAL: return new Float(source); case Types.DOUBLE: return new Double(source); case Types.DECIMAL: return java.math.BigDecimal.valueOf(source); case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: return String.valueOf(source); default: throw new SqlException(agent_.logWriter_, "Illegal Conversion"); } } // Convert from integer source to target type // In support of PS.setInt() final Object setObject(int targetType, int source) throws SqlException { switch (targetType) { case Types.SMALLINT: if (Configuration.rangeCheckCrossConverters && (source > Short.MAX_VALUE || source < Short.MIN_VALUE)) { throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); } return new Short((short) source); case Types.INTEGER: return new Integer(source); case Types.BIGINT: return new Long(source); case Types.REAL: return new Float(source); case Types.DOUBLE: return new Double(source); case Types.DECIMAL: return java.math.BigDecimal.valueOf(source); case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: return String.valueOf(source); default: throw new SqlException(agent_.logWriter_, "Illegal Conversion"); } } // This method is used in lieu of setObject(targetType, sourceObject) because we // don't support the BIT/BOOLEAN as underlying DERBY targetTypes. final boolean setBooleanFromObject(Object source, int sourceType) throws SqlException { switch (sourceType) { case Types.SMALLINT: return getBooleanFromShort(((Short) source).shortValue()); case Types.INTEGER: return getBooleanFromInt(((Integer) source).intValue()); case Types.BIGINT: return getBooleanFromLong(((java.math.BigInteger) source).longValue()); case Types.REAL: return getBooleanFromFloat(((Float) source).floatValue()); case Types.DOUBLE: return getBooleanFromDouble(((Double) source).doubleValue()); case Types.DECIMAL: return getBooleanFromLong(((java.math.BigDecimal) source).longValue()); case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: return getBooleanFromString((String) source); default: throw new ColumnTypeConversionException(agent_.logWriter_); } } // This method is used in lieu of setObject(targetType, sourceObject) because we // don't support the BIT/BOOLEAN as underlying DERBY targetTypes. final byte setByteFromObject(Object source, int sourceType) throws SqlException { switch (sourceType) { case Types.SMALLINT: return getByteFromShort(((Short) source).shortValue()); case Types.INTEGER: return getByteFromInt(((Integer) source).intValue()); case Types.BIGINT: return getByteFromLong(((java.math.BigInteger) source).longValue()); case Types.REAL: return getByteFromFloat(((Float) source).floatValue()); case Types.DOUBLE: return getByteFromDouble(((Double) source).doubleValue()); case Types.DECIMAL: return getByteFromLong(((java.math.BigDecimal) source).longValue()); case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: return getByteFromString((String) source); default: throw new ColumnTypeConversionException(agent_.logWriter_); } } // Convert from long source to target type // In support of PS.setLong() final Object setObject(int targetType, long source) throws SqlException { switch (targetType) { case Types.SMALLINT: if (Configuration.rangeCheckCrossConverters && (source > Short.MAX_VALUE || source < Short.MIN_VALUE)) { throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); } return new Short((short) source); case Types.INTEGER: if (Configuration.rangeCheckCrossConverters && (source > Integer.MAX_VALUE || source < Integer.MIN_VALUE)) { throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); } return new Integer((int) source); case Types.BIGINT: return new Long(source); case Types.REAL: return new Float(source); case Types.DOUBLE: return new Double(source); case Types.DECIMAL: return java.math.BigDecimal.valueOf(source); case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: return String.valueOf(source); default: throw new SqlException(agent_.logWriter_, "Illegal Conversion"); } } // Convert from floating point source to target type // In support of PS.setFloat() final Object setObject(int targetType, float source) throws SqlException { switch (targetType) { case Types.SMALLINT: if (Configuration.rangeCheckCrossConverters && (source > Short.MAX_VALUE || source < Short.MIN_VALUE)) { throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); } return new Short((short) source); case Types.INTEGER: if (Configuration.rangeCheckCrossConverters && (source > Integer.MAX_VALUE || source < Integer.MIN_VALUE)) { throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); } return new Integer((int) source); case Types.BIGINT: if (Configuration.rangeCheckCrossConverters && (source > Long.MAX_VALUE || source < Long.MIN_VALUE)) { throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); } return new Long((long) source); case Types.REAL: if (Configuration.rangeCheckCrossConverters && // change the check from (source > Float.MAX_VALUE || source < -Float.MIN_VALUE)) // to the following: //----------------------------------------------------------------------------------- // -infinity 0 +infinity // |__________________________|======|________________________| // <-3.4E+38| | | |>+3.4E+38 // | | |_________________ | // | |-1.4E-45 <X< +1.4E-45 // | |________________________ //----------------------------------------------------------------------------------- (source == Float.POSITIVE_INFINITY || source == Float.NEGATIVE_INFINITY)) { throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); } return new Float(source); case Types.DOUBLE: if (Configuration.rangeCheckCrossConverters && //------------------------------------------------------------------------------------- // -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)); } // source passed in is a float, do we need to check if the source already contains "infinity"?? return new Double(String.valueOf(source)); case Types.DECIMAL: // Can't use the following commented out line because it changes precision of the result. //return new java.math.BigDecimal (source); 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 double floating point source to target type // In support of PS.setDouble() final Object setObject(int targetType, double source) throws SqlException { switch (targetType) { case Types.SMALLINT: if (Configuration.rangeCheckCrossConverters && (source > Short.MAX_VALUE || source < Short.MIN_VALUE)) { throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); } return new Short((short) source); case Types.INTEGER: if (Configuration.rangeCheckCrossConverters && (source > Integer.MAX_VALUE || source < Integer.MIN_VALUE)) { throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); } return new Integer((int) source); case Types.BIGINT: if (Configuration.rangeCheckCrossConverters && (source > Long.MAX_VALUE || source < Long.MIN_VALUE)) { throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source)); } return new Long((long) source);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?