📄 sqlreal.java
字号:
/** * @see NumberDataValue#setValue * */ public void setValue(boolean theValue) { value = theValue?1:0; isnull = false; } /** * @see DataValueDescriptor#setValue * * @exception StandardException Thrown on error */ public void setValue(Object theValue) throws StandardException { if (theValue == null) { setToNull(); } else if (theValue instanceof Number) { // rounding issue to solve!!!/jsk, INF, 0.0f this.setValue(((Number)theValue).floatValue()); } else { // will most likely call .setValue(String) genericSetObject(theValue); } } protected void setFrom(DataValueDescriptor theValue) throws StandardException { // rounding issue to solve!!!/jsk //DOUBLE.getFloat() would make rounding problem if it got called here!!! // need to check where it is called from! if (theValue instanceof StringDataValue) { //System.out.println("\tcalling setValue(string)"); setValue(theValue.getString()); } else if (theValue instanceof SQLDouble) { //System.out.println("\tcalling setValue(double)"); setValue(theValue.getDouble()); } else { setValue(theValue.getFloat()); } } /* * DataValueDescriptor interface */ /** @see DataValueDescriptor#typePrecedence */ public int typePrecedence() { return TypeId.REAL_PRECEDENCE; } /* ** SQL Operators */ /** * The = operator as called from the language module, as opposed to * the storage module. * * @param left The value on the left side of the = * @param right The value on the right side of the = * * @return A SQL boolean value telling whether the two parameters are equal * * @exception StandardException Thrown on error */ public BooleanDataValue equals(DataValueDescriptor left, DataValueDescriptor right) throws StandardException { return SQLBoolean.truthValue(left, right, left.getFloat() == right.getFloat()); } /** * The <> operator as called from the language module, as opposed to * the storage module. * * @param left The value on the left side of the <> * @param right The value on the right side of the <> * * @return A SQL boolean value telling whether the two parameters * are not equal * * @exception StandardException Thrown on error */ public BooleanDataValue notEquals(DataValueDescriptor left, DataValueDescriptor right) throws StandardException { return SQLBoolean.truthValue(left, right, left.getFloat() != right.getFloat()); } /** * The < operator as called from the language module, as opposed to * the storage module. * * @param left The value on the left side of the < * @param right The value on the right side of the < * * @return A SQL boolean value telling whether the first operand is less * than the second operand * * @exception StandardException Thrown on error */ public BooleanDataValue lessThan(DataValueDescriptor left, DataValueDescriptor right) throws StandardException { return SQLBoolean.truthValue(left, right, left.getFloat() < right.getFloat()); } /** * The > operator as called from the language module, as opposed to * the storage module. * * @param left The value on the left side of the > * @param right The value on the right side of the > * * @return A SQL boolean value telling whether the first operand is greater * than the second operand * * @exception StandardException Thrown on error */ public BooleanDataValue greaterThan(DataValueDescriptor left, DataValueDescriptor right) throws StandardException { return SQLBoolean.truthValue(left, right, left.getFloat() > right.getFloat()); } /** * The <= operator as called from the language module, as opposed to * the storage module. * * @param left The value on the left side of the <= * @param right The value on the right side of the <= * * @return A SQL boolean value telling whether the first operand is less * than or equal to the second operand * * @exception StandardException Thrown on error */ public BooleanDataValue lessOrEquals(DataValueDescriptor left, DataValueDescriptor right) throws StandardException { return SQLBoolean.truthValue(left, right, left.getFloat() <= right.getFloat()); } /** * The >= operator as called from the language module, as opposed to * the storage module. * * @param left The value on the left side of the >= * @param right The value on the right side of the >= * * @return A SQL boolean value telling whether the first operand is greater * than or equal to the second operand * * @exception StandardException Thrown on error */ public BooleanDataValue greaterOrEquals(DataValueDescriptor left, DataValueDescriptor right) throws StandardException { return SQLBoolean.truthValue(left, right, left.getFloat() >= right.getFloat()); } /** * This method implements the + operator for "real + real". * The operator uses DOUBLE aritmetic as DB2 does. * * @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 SQLReal 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 SQLReal(); } if (addend1.isNull() || addend2.isNull()) { result.setToNull(); return result; } double dsum = addend1.getDouble() + addend2.getDouble(); // No need to check underflow (result rounded to 0.0), // since the difference between two non-equal valid DB2 DOUBLE values is always non-zero in java.lang.Double precision. result.setValue(dsum); return result; } /** * This method implements the - operator for "real - real". * The operator uses DOUBLE aritmetic as DB2 does. * * @param left The value to be subtracted from * @param right The value to be subtracted * @param result The result of a previous call to this method, null * if not called yet * * @return A SQLReal containing the result of the subtraction * * @exception StandardException Thrown on error */ public NumberDataValue minus(NumberDataValue left, NumberDataValue right, NumberDataValue result) throws StandardException { if (result == null) { result = new SQLReal(); } if (left.isNull() || right.isNull()) { result.setToNull(); return result; } double ddifference = left.getDouble() - right.getDouble(); // No need to check underflow (result rounded to 0.0), // since no difference between two valid DB2 DOUBLE values can be rounded off to 0.0 in java.lang.Double result.setValue(ddifference); return result; } /** * This method implements the * operator for "real * real". * The operator uses DOUBLE aritmetic as DB2 does. * * @param left The first value to be multiplied * @param right The second value to be multiplied * @param result The result of a previous call to this method, null * if not called yet * * @return A SQLReal containing the result of the multiplication * * @exception StandardException Thrown on error */ public NumberDataValue times(NumberDataValue left, NumberDataValue right, NumberDataValue result) throws StandardException { if (result == null) { result = new SQLReal(); } if (left.isNull() || right.isNull()) { result.setToNull(); return result; } double leftValue = left.getDouble(); double rightValue = right.getDouble(); double tempResult = leftValue * rightValue; // check underflow (result rounded to 0.0) if ( (tempResult == 0.0) && ( (leftValue != 0.0) && (rightValue != 0.0) ) ) { throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, TypeId.REAL_NAME); } result.setValue(tempResult); return result; } /** * This method implements the / operator for "real / real". * The operator uses DOUBLE aritmetic as DB2 does. * * @param dividend The numerator * @param divisor The denominator * @param result The result of a previous call to this method, null * if not called yet * * @return A SQLReal containing the result of the division * * @exception StandardException Thrown on error */ public NumberDataValue divide(NumberDataValue dividend, NumberDataValue divisor, NumberDataValue result) throws StandardException { if (result == null) { result = new SQLReal(); } if (dividend.isNull() || divisor.isNull()) { result.setToNull(); return result; } double divisorValue = divisor.getDouble(); if (divisorValue == 0.0e0f) { throw StandardException.newException(SQLState.LANG_DIVIDE_BY_ZERO); } double dividendValue = dividend.getDouble(); double resultValue = dividendValue / divisorValue; if (Double.isNaN(resultValue)) { throw StandardException.newException(SQLState.LANG_DIVIDE_BY_ZERO); } // check underflow (result rounded to 0.0) if ((resultValue == 0.0e0d) && (dividendValue != 0.0e0d)) { throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, TypeId.REAL_NAME); } result.setValue(resultValue); return result; } /** * This method implements the unary minus operator for real. * * @param result The result of a previous call to this method, null * if not called yet * * @return A SQLSmalllint containing the result of the division * * @exception StandardException Thrown on error */ public NumberDataValue minus(NumberDataValue result) throws StandardException { float minusResult; if (result == null) { result = new SQLReal(); } if (this.isNull()) { result.setToNull(); return result; } minusResult = -(this.getFloat()); result.setValue(minusResult); return result; } /** * This method implements the isNegative method. * Note: This method will return true for -0.0f. * * @return A boolean. If this.value is negative, return true. * For positive values or null, return false. */ protected boolean isNegative() { return !isNull() && (value < 0.0f); } /* * String display of value */ public String toString() { if (isNull()) return "NULL"; else return Float.toString(value); } /* * Hash code */ public int hashCode() { long longVal = (long) value; if (longVal != value) { longVal = Double.doubleToLongBits(value); } return (int) (longVal ^ (longVal >> 32)); } static final int REAL_LENGTH = 16; private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( SQLReal.class); public int estimateMemoryUsage() { return BASE_MEMORY_USAGE; } /* * object state */ private float value; private boolean isnull;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -