📄 sqldecimal.java
字号:
public String getTypeName() { return TypeId.DECIMAL_NAME; } /* * Storable interface, implies Externalizable, TypedFormat */ /** Return my format identifier. @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId */ public int getTypeFormatId() { return StoredFormatIds.SQL_DECIMAL_ID; } /* * see if the decimal value is null. */ /** @see Storable#isNull */ public boolean isNull() { return (value == null) && (rawData == null); } /** * Distill the BigDecimal to a byte array and * write out: <UL> * <LI> scale (zero or positive) as a byte </LI> * <LI> length of byte array as a byte</LI> * <LI> the byte array </LI> </UL> * */ public void writeExternal(ObjectOutput out) throws IOException { // never called when value is null if (SanityManager.DEBUG) SanityManager.ASSERT(! isNull()); int scale; byte[] byteArray; if (value != null) { scale = value.scale(); // J2SE 5.0 introduced negative scale value for BigDecimals. // In previouse Java releases a negative scale was not allowed // (threw an exception on setScale and the constructor that took // a scale). // // Thus the Derby format for DECIMAL implictly assumed a // positive or zero scale value, and thus now must explicitly // be positive. This is to allow databases created under J2SE 5.0 // to continue to be supported under JDK 1.3/JDK 1.4, ie. to continue // the platform independence, independent of OS/cpu and JVM. // // If the scale is negative set the scale to be zero, this results // in an unchanged value with a new scale. A BigDecimal with a // negative scale by definition is a whole number. // e.g. 1000 can be represented by: // a BigDecimal with scale -3 (unscaled value of 1) // or a BigDecimal with scale 0 (unscaled value of 1000) if (scale < 0) { scale = 0; value = value.setScale(0); } BigInteger bi = value.unscaledValue(); byteArray = bi.toByteArray(); } else { scale = rawScale; byteArray = rawData; } if (SanityManager.DEBUG) { if (scale < 0) SanityManager.THROWASSERT("DECIMAL scale at writeExternal is negative " + scale + " value " + toString()); } out.writeByte(scale); out.writeByte(byteArray.length); out.write(byteArray); } /** * Note the use of rawData: we reuse the array if the * incoming array is the same length or smaller than * the array length. * * @see java.io.Externalizable#readExternal */ public void readExternal(ObjectInput in) throws IOException { // clear the previous value to ensure that the // rawData value will be used value = null; rawScale = in.readUnsignedByte(); int size = in.readUnsignedByte(); /* ** Allocate a new array if the data to read ** is larger than the existing array, or if ** we don't have an array yet. Need to use readFully below and NOT just read because read does not guarantee getting size bytes back, whereas readFully does (unless EOF). */ if ((rawData == null) || size != rawData.length) { rawData = new byte[size]; } in.readFully(rawData); } public void readExternalFromArray(ArrayInputStream in) throws IOException { // clear the previous value to ensure that the // rawData value will be used value = null; rawScale = in.readUnsignedByte(); int size = in.readUnsignedByte(); /* ** Allocate a new array if the data to read ** is larger than the existing array, or if ** we don't have an array yet. Need to use readFully below and NOT just read because read does not guarantee getting size bytes back, whereas readFully does (unless EOF). */ if ((rawData == null) || size != rawData.length) { rawData = new byte[size]; } in.readFully(rawData); } /** * @see Storable#restoreToNull * */ public void restoreToNull() { value = null; rawData = null; } /** @exception StandardException Thrown on error */ protected int typeCompare(DataValueDescriptor arg) throws StandardException { BigDecimal otherValue = SQLDecimal.getBigDecimal(arg); return getBigDecimal().compareTo(otherValue); } /* * DataValueDescriptor interface */ /** * <B> WARNING </B> clone is a shallow copy * @see DataValueDescriptor#getClone */ public DataValueDescriptor getClone() { return new SQLDecimal(getBigDecimal()); } /** * @see DataValueDescriptor#getNewNull */ public DataValueDescriptor getNewNull() { return new SQLDecimal(); } /** * @see DataValueDescriptor#setValueFromResultSet * * @exception SQLException Thrown on error */ public void setValueFromResultSet(ResultSet resultSet, int colNumber, boolean isNullable) throws SQLException { value = resultSet.getBigDecimal(colNumber); rawData = null; } /** Set the value into a PreparedStatement. @exception SQLException Error setting value in PreparedStatement */ public final void setInto(PreparedStatement ps, int position) throws SQLException { if (isNull()) { ps.setNull(position, java.sql.Types.DECIMAL); return; } ps.setBigDecimal(position, getBigDecimal()); } /** * * <B> WARNING </B> there is no checking to make sure * that theValue doesn't exceed the precision/scale of * the current SQLDecimal. It is just assumed that the * SQLDecimal is supposed to take the precision/scale of * the BigDecimalized String. * * @exception StandardException throws NumberFormatException * when the String format is not recognized. */ public void setValue(String theValue) throws StandardException { rawData = null; if (theValue == null) { value = null; } else { try { theValue = theValue.trim(); value = new BigDecimal(theValue); rawData = null; } catch (NumberFormatException nfe) { throw invalidFormat(); } } } /** * @see NumberDataValue#setValue * * @exception StandardException Thrown on error */ public void setValue(double theValue) throws StandardException { setCoreValue(NumberDataType.normalizeDOUBLE(theValue)); } /** * @see NumberDataValue#setValue * */ public void setValue(float theValue) throws StandardException { setCoreValue((double)NumberDataType.normalizeREAL(theValue)); } /** * @see NumberDataValue#setValue * */ public void setValue(long theValue) { value = BigDecimal.valueOf(theValue); rawData = null; } /** * @see NumberDataValue#setValue * */ public void setValue(int theValue) { setValue((long)theValue); } /** Only to be called when the application sets a value using BigDecimal through setBigDecimal calls. */ public void setBigDecimal(Number theValue) throws StandardException { setCoreValue((BigDecimal) theValue); } /** Called when setting a DECIMAL value internally or from through a procedure or function. Handles long in addition to BigDecimal to handle identity being stored as a long but returned as a DECIMAL. */ public void setValue(Number theValue) throws StandardException { if (SanityManager.ASSERT) { if (theValue != null && !(theValue instanceof java.math.BigDecimal) && !(theValue instanceof java.lang.Long)) SanityManager.THROWASSERT("SQLDecimal.setValue(Number) passed a " + theValue.getClass()); } if (theValue instanceof BigDecimal || theValue == null) setCoreValue((BigDecimal) theValue); else setValue(theValue.longValue()); } /** * @see NumberDataValue#setValue * */ public void setValue(boolean theValue) { setCoreValue(theValue ? ONE : ZERO); } /* * DataValueDescriptor interface */ /** @see DataValueDescriptor#typePrecedence */ public int typePrecedence() { return TypeId.DECIMAL_PRECEDENCE; } // END DataValueDescriptor interface private void setCoreValue(BigDecimal theValue) { value = theValue; rawData = null; } private void setCoreValue(double theValue) { value = new BigDecimal(Double.toString(theValue)); rawData = null; } /** * Normalization method - this method may be called when putting * a value into a SQLDecimal, for example, when inserting into a SQLDecimal * column. See NormalizeResultSet in execution. * <p> * Note that truncation is allowed on the decimal portion * of a numeric only. * * @param desiredType The type to normalize the source column to * @param source The value to normalize * * @throws StandardException Thrown for null into * non-nullable column, and for * truncation error */ public void normalize( DataTypeDescriptor desiredType, DataValueDescriptor source) throws StandardException { int desiredScale = desiredType.getScale(); int desiredPrecision = desiredType.getPrecision(); setFrom(source); setWidth(desiredPrecision, desiredScale, true); } /* ** SQL Operators */ /** * This method implements the + operator for DECIMAL. * * @param addend1 One of the addends * @param addend2 The other addend * @param result The result of a previous call to this method, null * if not called yet * * @return A SQLDecimal containing the result of the addition * * @exception StandardException Thrown on error */ public NumberDataValue plus(NumberDataValue addend1, NumberDataValue addend2, NumberDataValue result) throws StandardException { if (result == null) { result = new SQLDecimal(); } if (addend1.isNull() || addend2.isNull()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -