sqlbinary.java

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

JAVA
1,092
字号
	 * @see SQLBit#setValue	 *	 */	public final void setValue(InputStream theStream, int streamLength)	{		dataValue = null;		stream = theStream;		this.streamLength = streamLength;	}	protected final void setFrom(DataValueDescriptor theValue) throws StandardException {		if (theValue instanceof SQLBinary)		{			SQLBinary theValueBinary = (SQLBinary) theValue;			dataValue = theValueBinary.dataValue;			stream = theValueBinary.stream;			streamLength = theValueBinary.streamLength;		}		else		{			setValue(theValue.getBytes());		}	}	/*	** 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 =	 *						is not.	 * @return	A SQL boolean value telling whether the two parameters are equal	 *	 * @exception StandardException		Thrown on error	 */	public final BooleanDataValue equals(DataValueDescriptor left,							 DataValueDescriptor right)								throws StandardException	{		boolean isEqual;		if (left.isNull() || right.isNull())		{			isEqual = false;		}		else		{				isEqual = SQLBinary.compare(left.getBytes(), right.getBytes()) == 0;		}		return SQLBoolean.truthValue(left,									 right,									 isEqual);	}	/**	 * 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 final BooleanDataValue notEquals(DataValueDescriptor left,							 DataValueDescriptor right)								throws StandardException	{		boolean isNotEqual;		if (left.isNull() || right.isNull())		{			isNotEqual = false;		}		else		{				isNotEqual = SQLBinary.compare(left.getBytes(), right.getBytes()) != 0;		}		return SQLBoolean.truthValue(left,									 right,									 isNotEqual);	}	/**	 * 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 <	 * @param result	The result of a previous call to this method, null	 *					if not called yet.  NOTE: This is unused in this	 *					method, because comparison operators always return	 *					pre-allocated values.	 *	 * @return	A SQL boolean value telling whether the first operand is	 *			less than the second operand	 *	 * @exception StandardException		Thrown on error	 */	public final BooleanDataValue lessThan(DataValueDescriptor left,							 DataValueDescriptor right)								throws StandardException	{		boolean isLessThan;		if (left.isNull() || right.isNull())		{			isLessThan = false;		}		else		{				isLessThan = SQLBinary.compare(left.getBytes(), right.getBytes()) < 0;		}		return SQLBoolean.truthValue(left,									 right,									 isLessThan);	}	/**	 * 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 final BooleanDataValue greaterThan(DataValueDescriptor left,							 DataValueDescriptor right)								throws StandardException	{		boolean isGreaterThan = false;		if (left.isNull() || right.isNull())		{			isGreaterThan = false;		}		else		{				isGreaterThan = SQLBinary.compare(left.getBytes(), right.getBytes()) > 0;		}		return SQLBoolean.truthValue(left,									 right,									 isGreaterThan);	}	/**	 * 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 final BooleanDataValue lessOrEquals(DataValueDescriptor left,							 DataValueDescriptor right)								throws StandardException	{		boolean isLessEquals = false;		if (left.isNull() || right.isNull())		{			isLessEquals = false;		}		else		{				isLessEquals = SQLBinary.compare(left.getBytes(), right.getBytes()) <= 0;		}		return SQLBoolean.truthValue(left,									 right,									 isLessEquals);	}	/**	 * 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 final BooleanDataValue greaterOrEquals(DataValueDescriptor left,							 DataValueDescriptor right)								throws StandardException	{		boolean isGreaterEquals = false;		if (left.isNull() || right.isNull())		{			isGreaterEquals = false;		}		else		{				isGreaterEquals = SQLBinary.compare(left.getBytes(), right.getBytes()) >= 0;		}		return SQLBoolean.truthValue(left,									 right,									 isGreaterEquals);	}	/**	 *	 * This method implements the char_length function for bit.	 *	 * @param result	The result of a previous call to this method, null	 *					if not called yet	 *	 * @return	A SQLInteger containing the length of the char value	 *	 * @exception StandardException		Thrown on error	 *	 * @see ConcatableDataValue#charLength	 */	public final NumberDataValue charLength(NumberDataValue result)							throws StandardException	{		if (result == null)		{			result = new SQLInteger();		}		if (this.isNull())		{			result.setToNull();			return result;		}		result.setValue(getValue().length);		return result;	}	/**	 * @see BitDataValue#concatenate	 *	 * @exception StandardException		Thrown on error	 */	public final BitDataValue concatenate(				BitDataValue left,				BitDataValue right,				BitDataValue result)		throws StandardException	{		if (left.isNull() || right.isNull())		{			result.setToNull();			return result;		}		byte[] leftData = left.getBytes();		byte[] rightData = right.getBytes();		byte[] concatData = new byte[leftData.length + rightData.length];		System.arraycopy(leftData, 0, concatData, 0, leftData.length);		System.arraycopy(rightData, 0, concatData, leftData.length, rightData.length);		result.setValue(concatData);		return result;	}  	/**	 * The SQL substr() function.	 *	 * @param start		Start of substr	 * @param length	Length of substr	 * @param result	The result of a previous call to this method,	 *					null if not called yet.	 * @param maxLen	Maximum length of the result	 *	 * @return	A ConcatableDataValue containing the result of the substr()	 *	 * @exception StandardException		Thrown on error	 */	public final ConcatableDataValue substring(				NumberDataValue start,				NumberDataValue length,				ConcatableDataValue result,				int maxLen)		throws StandardException	{		int startInt;		int lengthInt;		BitDataValue varbitResult;		if (result == null)		{			result = new SQLVarbit();		}		varbitResult = (BitDataValue) result;		/* The result is null if the receiver (this) is null or if the length is negative.		 * Oracle docs don't say what happens if the start position or the length is a usernull.		 * We will return null, which is the only sensible thing to do.		 * (If the user did not specify a length then length is not a user null.)		 */		if (this.isNull() || start.isNull() || (length != null && length.isNull()))		{			varbitResult.setToNull();			return varbitResult;		}		startInt = start.getInt();		// If length is not specified, make it till end of the string		if (length != null)		{			lengthInt = length.getInt();		}		else lengthInt = getLength() - startInt + 1;		/* DB2 Compatibility: Added these checks to match DB2. We currently enforce these		 * limits in both modes. We could do these checks in DB2 mode only, if needed, so		 * leaving earlier code for out of range in for now, though will not be exercised		 */		if ((startInt <= 0 || lengthInt < 0 || startInt > getLength() ||				lengthInt > getLength() - startInt + 1))			throw StandardException.newException(SQLState.LANG_SUBSTR_START_OR_LEN_OUT_OF_RANGE);					// Return null if length is non-positive		if (lengthInt < 0)		{			varbitResult.setToNull();			return varbitResult;		}		/* If startInt < 0 then we count from the right of the string */		if (startInt < 0)		{			startInt += getLength();			if (startInt < 0)			{				lengthInt += startInt;				startInt = 0;			}			if (lengthInt + startInt > 0)			{				lengthInt += startInt;			}			else			{				lengthInt = 0;			}		}		else if (startInt > 0)		{			/* java substr() is 0 based */			startInt--;		}		/* Oracle docs don't say what happens if the window is to the		 * left of the string.  Return "" if the window		 * is to the left or right or if the length is 0.		 */		if (lengthInt == 0 ||			lengthInt <= 0 - startInt ||			startInt > getLength())		{			varbitResult.setValue(new byte[0]);			return varbitResult;		}		if (lengthInt >= getLength() - startInt)		{			byte[] substring = new byte[dataValue.length - startInt];			System.arraycopy(dataValue, startInt, substring, 0, substring.length);			varbitResult.setValue(substring);		}		else		{			byte[] substring = new byte[lengthInt];			System.arraycopy(dataValue, startInt, substring, 0, substring.length);			varbitResult.setValue(substring);		}		return varbitResult;	}	/**		Host variables are rejected if their length is		bigger than the declared length, regardless of		if the trailing bytes are the pad character.		@exception StandardException Variable is too big.	*/	public final void checkHostVariable(int declaredLength) throws StandardException	{		// stream length checking occurs at the JDBC layer		int variableLength = -1;		if (stream == null)		{			if (dataValue != null)				variableLength = dataValue.length;		}		else		{			variableLength = streamLength;		}		if (variableLength != -1 && variableLength > declaredLength)				throw StandardException.newException(SQLState.LANG_STRING_TRUNCATION, getTypeName(), 							"XX-RESOLVE-XX",							String.valueOf(declaredLength));	}	/*	 * String display of value	 */	public final String toString()	{		if (dataValue == null)		{			if (stream == null)			{				return "NULL";			}			else			{				if (SanityManager.DEBUG)					SanityManager.THROWASSERT(						"value is null, stream is not null");				return "";			}		}		else		{			return org.apache.derby.iapi.util.StringUtil.toHexString(dataValue, 0, dataValue.length);		}	}	/*	 * Hash code	 */	public final int hashCode()	{		try {			if (getValue() == null)				{					return 0;				}		}		catch (StandardException se)		{			if (SanityManager.DEBUG)				SanityManager.THROWASSERT("Unexpected exception " + se);			return 0;		}		/* Hash code is simply the sum of all of the bytes */		byte[] bytes = dataValue;		int hashcode = 0;		// Build the hash code		for (int index = 0 ; index < bytes.length; index++)		{			byte bv = bytes[index];			if (bv != SQLBinary.PAD)				hashcode += bytes[index];		}		return hashcode;	}	private static int compare(byte[] left, byte[] right) {		int minLen = left.length;		byte[] longer = right;		if (right.length < minLen) {			minLen = right.length;			longer = left;		}		for (int i = 0; i < minLen; i++) {			int lb = left[i] & 0xff;			int rb = right[i] & 0xff;			if (lb == rb)				continue;			return lb - rb;		}		// complete match on all the bytes for the smallest value.		// if the longer value is all pad characters		// then the values are equal.		for (int i = minLen; i < longer.length; i++) {			byte nb = longer[i];			if (nb == SQLBinary.PAD)				continue;			// longer value is bigger.			if (left == longer)				return 1;			return -1;		}		return 0;	}      /** Adding this method to ensure that super class' setInto method doesn't get called      * that leads to the violation of JDBC spec( untyped nulls ) when batching is turned on.      */     public void setInto(PreparedStatement ps, int position) throws SQLException, StandardException {                  ps.setBytes(position, getBytes());     }}

⌨️ 快捷键说明

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