📄 datatype.java
字号:
} /** * Set the Object that this Data Type contains (for an explicit cast). * * @exception StandardException Thrown on error */ public void setObjectForCast(Object value, boolean instanceOf, String resultTypeClassName) throws StandardException { setValue(value); } /** Set the value from an object. Usually overridden. This implementation sets this to NULL if the passed in value is null, otherwise an exception is thrown. */ public void setValue(Object theValue) throws StandardException { if (theValue == null) setToNull(); else throwLangSetMismatch(theValue); } /** * Gets the value in the data value descriptor as a int. * Throws an exception if the data value is not receivable as a int. * * @return The data value as a int. * * @exception StandardException Thrown on error */ public Object getObject() throws StandardException { throw dataTypeConversion("java.lang.Object"); } protected void genericSetObject(Object theValue) throws StandardException { //if (theValue instanceof String) // ((DataValueDescriptor) this).setValue((String) theValue); //else throwLangSetMismatch(theValue); } /** * From CloneableObject * * @return clone of me as an Object */ public Object cloneObject() { return getClone(); } // International support /** * International version of getString(). Overridden for date, time, * and timestamp in SQLDate, SQLTime, SQLTimestamp. * * @exception StandardException Thrown on error */ protected String getNationalString(LocaleFinder localeFinder) throws StandardException { return getString(); } public void throwLangSetMismatch(Object value) throws StandardException { throwLangSetMismatch(value.getClass().getName()); } void throwLangSetMismatch(String argTypeName) throws StandardException { throw StandardException.newException(SQLState.LANG_DATA_TYPE_SET_MISMATCH, argTypeName, this.getTypeName()); } public void setInto(PreparedStatement ps, int position) throws SQLException, StandardException { ps.setObject(position, getObject()); } /** Set this value into a ResultSet for a subsequent ResultSet.insertRow or ResultSet.updateRow. This method will only be called for non-null values. @exception SQLException thrown by the ResultSet object @exception StandardException thrown by me accessing my value. */ public void setInto(ResultSet rs, int position) throws SQLException, StandardException { rs.updateObject(position, getObject()); } /** * Default normalization method. No information needed from DataTypeDescriptor. * * @param desiredType The type to normalize the source column to * @param source The value to normalize * * * @exception StandardException Thrown normalization error. */ public void normalize( DataTypeDescriptor desiredType, DataValueDescriptor source) throws StandardException { ((DataValueDescriptor) this).setValue(source); } /** * Each built-in type in JSQL has a precedence. This precedence determines * how to do type promotion when using binary operators. For example, float * has a higher precedence than int, so when adding an int to a float, the * result type is float. * * The precedence for some types is arbitrary. For example, it doesn't * matter what the precedence of the boolean type is, since it can't be * mixed with other types. But the precedence for the number types is * critical. The SQL standard requires that exact numeric types be * promoted to approximate numeric when one operator uses both. Also, * the precedence is arranged so that one will not lose precision when * promoting a type. * * @return The precedence of this type. */ public int typePrecedence() { return -1; } /** * The = operator as called from the language module, as opposed to * the storage module. This default implementations uses compare(). * * @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.compare(right) == 0); } /** * The <> operator as called from the language module, as opposed to * the storage module. This default implementations uses compare(). * * @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.compare(right) != 0); } /** * 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.compare(right) < 0); } /** * The > operator as called from the language module, as opposed to * the storage module. This default implementations uses compare(). * * @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.compare(right) > 0); } /** * The <= operator as called from the language module, as opposed to * the storage module. This default implementations uses compare(). * * @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.compare(right) <= 0); } /** * The >= operator as called from the language module, as opposed to * the storage module. This default implementation uses compare(). * * @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.compare(right) >= 0); } public boolean compare(int op, DataValueDescriptor other, boolean orderedNulls, boolean unknownRV) throws StandardException { /* Use compare method from dominant type, flipping the operator * to reflect flipping of sides. */ if (typePrecedence() < other.typePrecedence()) { return other.compare(flip(op), this, orderedNulls, unknownRV); } int result = compare(other); switch(op) { case ORDER_OP_LESSTHAN: return (result < 0); // this < other case ORDER_OP_EQUALS: return (result == 0); // this == other case ORDER_OP_LESSOREQUALS: return (result <= 0); // this <= other // flipped operators case ORDER_OP_GREATERTHAN: return (result > 0); // this > other case ORDER_OP_GREATEROREQUALS: return (result >= 0); // this >= other default: if (SanityManager.DEBUG) SanityManager.THROWASSERT("Invalid Operator"); return false; } } /** * Flip the operator used in a comparison (< -> >). * This is useful when flipping a comparison due to * type precedence. * * @param operator The operator to flip. * * @return The flipped operator. */ protected static int flip(int operator) { switch (operator) { case Orderable.ORDER_OP_LESSTHAN: // < -> > return Orderable.ORDER_OP_GREATERTHAN; case Orderable.ORDER_OP_LESSOREQUALS: // <= -> >= return Orderable.ORDER_OP_GREATEROREQUALS; case Orderable.ORDER_OP_EQUALS: // = -> = return Orderable.ORDER_OP_EQUALS; default: // These operators only appear due to flipping. // They should never be flipped themselves. if (SanityManager.DEBUG) { SanityManager.THROWASSERT( "Attempting to flip an operator that is not " + "expected to be flipped."); } return operator; } } /* * DataValueDescriptor interface */ /** * @see DataValueDescriptor#coalesce * @exception StandardException Thrown on error */ public DataValueDescriptor coalesce(DataValueDescriptor[] argumentsList, DataValueDescriptor returnValue) throws StandardException { // arguments list should have at least 2 arguments if (SanityManager.DEBUG) { SanityManager.ASSERT(argumentsList != null, "argumentsList expected to be non-null"); SanityManager.ASSERT(argumentsList.length > 1, "argumentsList.length expected to be > 1"); } /* Walk the arguments list until we find a non-null value. Otherwise we will return null */ int index; for (index = 0; index < argumentsList.length; index++) { if (!(argumentsList[index].isNull())) { returnValue.setValue(argumentsList[index]); return returnValue; } } returnValue.setToNull(); return returnValue; } /** * @see DataValueDescriptor#in * @exception StandardException Thrown on error */ public BooleanDataValue in(DataValueDescriptor left, DataValueDescriptor[] inList, boolean orderedList) throws StandardException { BooleanDataValue retval = null; // in list should be non-empty if (SanityManager.DEBUG) { SanityManager.ASSERT(inList != null, "inList expected to be non-null"); SanityManager.ASSERT(inList.length > 0, "inList.length expected to be > 0"); } // if left is null then just return false if (left.isNull()) { return SQLBoolean.truthValue(left, inList[0], false); } int start = 0; int finish = inList.length; /* Do a binary search if the list is ordered until the * range of values to search is 3 or less. * NOTE: We've ensured that the IN list and the left all have * the same precedence at compile time. If we don't enforce * the same precendence then * we could get the wrong result when doing a binary search. */ if (orderedList) { while (finish - start > 2) { int mid = ((finish - start) / 2) + start; // Search left retval = equals(left, inList[mid]); if (retval.equals(true)) { return retval; } BooleanDataValue goLeft = greaterThan(inList[mid], left); if (goLeft.equals(true)) { // search left finish = mid; } else { // search right start = mid; } } } /* Walk the in list comparing the values. Return as soon as we * find a match. If the list is ordered, return as soon as the left * value is greater than an element in the in list. */ for (int index = start; index < finish; index++) { retval = equals(left, inList[index]); if (retval.equals(true)) { break; } // Can we stop searching? if (orderedList) { BooleanDataValue stop = greaterThan(inList[index], left); if (stop.equals(true)) { break; } } } return retval; } /* * equals */ public boolean equals(Object other) { if (! (other instanceof DataValueDescriptor)) { return false; } try { return compare(ORDER_OP_EQUALS, (DataValueDescriptor) other, true, false); } catch (StandardException se) { return false; } } public void setValue(InputStream theStream, int streamLength) throws StandardException { throwLangSetMismatch("java.io.InputStream"); } /** Check the value to seem if it conforms to the restrictions imposed by DB2/JCC on host variables for this type. @exception StandardException Variable is too big. */ public void checkHostVariable(int declaredLength) throws StandardException { } /** Return an conversion exception for this type. */ protected final StandardException dataTypeConversion(String targetType) { return StandardException.newException(SQLState.LANG_DATA_TYPE_GET_MISMATCH, targetType, this.getTypeName()); } /** Return an out of range exception for this type. */ protected final StandardException outOfRange() { return StandardException.newException( SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, getTypeName()); } /** Return an out of range exception for this type. */ protected final StandardException invalidFormat() { return StandardException.newException( SQLState.LANG_FORMAT_EXCEPTION, getTypeName()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -