📄 sqlchar.java
字号:
{ boolean comparison; if ((left instanceof SQLChar) && (right instanceof SQLChar)) { comparison = stringCompare((SQLChar) left, (SQLChar) right) > 0; } else { comparison = stringCompare(left.getString(), right.getString()) > 0; } return SQLBoolean.truthValue(left, right, comparison); } /** * 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 { boolean comparison; if ((left instanceof SQLChar) && (right instanceof SQLChar)) { comparison = stringCompare((SQLChar) left, (SQLChar) right) <= 0; } else { comparison = stringCompare(left.getString(), right.getString()) <= 0; } return SQLBoolean.truthValue(left, right, comparison); } /** * 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 { boolean comparison; if ((left instanceof SQLChar) && (right instanceof SQLChar)) { comparison = stringCompare((SQLChar) left, (SQLChar) right) >= 0; } else { comparison = stringCompare(left.getString(), right.getString()) >= 0; } return SQLBoolean.truthValue(left, right, comparison); } /* ** Concatable interface */ /** * This method implements the char_length function for char. * * @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(NumberDataValue) */ public NumberDataValue charLength(NumberDataValue result) throws StandardException { if (result == null) { result = new SQLInteger(); } if (this.isNull()) { result.setToNull(); return result; } result.setValue(this.getLength()); return result; } /** * @see StringDataValue#concatenate * * @exception StandardException Thrown on error */ public StringDataValue concatenate( StringDataValue leftOperand, StringDataValue rightOperand, StringDataValue result) throws StandardException { if (leftOperand.isNull() || leftOperand.getString() == null || rightOperand.isNull() || rightOperand.getString() == null) { result.setToNull(); return result; } result.setValue(leftOperand.getString().concat(rightOperand.getString())); return result; } /** * This method implements the like function for char (with no escape value). * * @param value The value to evaluate * @param pattern The pattern to use * * @return A SQL boolean value telling whether the first operand is * like the second operand * * @exception StandardException Thrown on error */ public BooleanDataValue like(DataValueDescriptor pattern) throws StandardException { Boolean likeResult; if (! isNationalString()) { // note that we call getLength() because the length // of the char array may be different than the // length we should be using (i.e. getLength()). // see getCharArray() for more info char[] evalCharArray = getCharArray(); char[] patternCharArray = ((SQLChar)pattern).getCharArray(); likeResult = Like.like(evalCharArray, getLength(), patternCharArray, pattern.getLength()); } else { SQLChar patternSQLChar = (SQLChar) pattern; likeResult = Like.like(getIntArray(), getIntLength(), patternSQLChar.getIntArray(), patternSQLChar.getIntLength(), getLocaleFinder().getCollator()); } return SQLBoolean.truthValue(this, pattern, likeResult); } /** * This method implements the like function for char with an escape value. * * @param pattern The pattern to use * * @return A SQL boolean value telling whether the first operand is * like the second operand * * @exception StandardException Thrown on error */ public BooleanDataValue like( DataValueDescriptor pattern, DataValueDescriptor escape) throws StandardException { Boolean likeResult; if (SanityManager.DEBUG) SanityManager.ASSERT( pattern instanceof StringDataValue && escape instanceof StringDataValue, "All three operands must be instances of StringDataValue"); // ANSI states a null escape yields 'unknown' results // // This method is only called when we have an escape clause, so this // test is valid if (escape.isNull()) { throw StandardException.newException(SQLState.LANG_ESCAPE_IS_NULL); } if (! isNationalString()) { // note that we call getLength() because the length // of the char array may be different than the // length we should be using (i.e. getLength()). // see getCharArray() for more info char[] evalCharArray = getCharArray(); char[] patternCharArray = ((SQLChar)pattern).getCharArray(); char[] escapeCharArray = (((SQLChar) escape).getCharArray()); int escapeLength = escape.getLength(); if (escapeCharArray != null && escapeLength != 1 ) { throw StandardException.newException(SQLState.LANG_INVALID_ESCAPE_CHARACTER, new String(escapeCharArray)); } else { // Make sure we fail for both varchar an nvarchar // for multiple collation characters. SQLChar escapeSQLChar = (SQLChar) escape; int[] escapeIntArray = escapeSQLChar.getIntArray(); if (escapeIntArray != null && (escapeIntArray.length != 1)) { throw StandardException.newException(SQLState.LANG_INVALID_ESCAPE_CHARACTER,new String(escapeSQLChar.getCharArray())); } } likeResult = Like.like(evalCharArray, getLength(), patternCharArray, pattern.getLength(), escapeCharArray, escapeLength); } else { SQLChar patternSQLChar = (SQLChar) pattern; SQLChar escapeSQLChar = (SQLChar) escape; int[] escapeIntArray = escapeSQLChar.getIntArray(); int escapeLength = escapeSQLChar.getIntLength(); if (escapeIntArray != null && (escapeIntArray.length != 1)) { throw StandardException.newException(SQLState.LANG_INVALID_ESCAPE_CHARACTER, new String(escapeSQLChar.getCharArray())); } likeResult = Like.like(getIntArray(), getIntLength(), patternSQLChar.getIntArray(), patternSQLChar.getIntLength(), escapeIntArray, escapeLength, getLocaleFinder().getCollator()); } return SQLBoolean.truthValue(this, pattern, likeResult); } /** * This method implements the locate function for char. * @param searchFrom - The string to search from * @param start - The position to search from in string searchFrom * @param result - The object to return * * Note: use getString() to get the string to search for. * * @return The position in searchFrom the fist occurrence of this.value. * 0 is returned if searchFrom does not contain this.value. * @exception StandardException Thrown on error */ public NumberDataValue locate( StringDataValue searchFrom, NumberDataValue start, NumberDataValue result) throws StandardException { int startVal; if( result == null ) { result = new SQLInteger(); } if( start.isNull() ) { startVal = 1; } else { startVal = start.getInt(); } if( isNull() || searchFrom.isNull() ) { result.setToNull(); return result; } String mySearchFrom = searchFrom.getString(); String mySearchFor = this.getString(); /* the below 2 if conditions are to emulate DB2's behavior */ if( startVal < 1 ) { throw StandardException.newException( SQLState.LANG_INVALID_PARAMETER_FOR_SEARCH_POSITION, new String(getString()), new String(mySearchFrom), new Integer(startVal)); } if( mySearchFor.length() == 0 ) { result.setValue( startVal ); return result; } result.setValue( mySearchFrom.indexOf(mySearchFor, startVal - 1) + 1); 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 ConcatableDataValue substring( NumberDataValue start, NumberDataValue length, ConcatableDataValue result, int maxLen) throws StandardException { int startInt; int lengthInt; StringDataValue stringResult; if (result == null) { result = getNewVarchar(); } stringResult = (StringDataValue) result; /* The result is null if the receiver (this) is null or if the length is negative. * 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())) { stringResult.setToNull(); return stringResult; } startInt = start.getInt(); // If length is not specified, make it till end of the string if (length != null) { lengthInt = length.getInt(); } else lengthInt = maxLen - 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 > maxLen || lengthInt > maxLen - startInt + 1)) throw StandardException.newException(SQLState.LANG_SUBSTR_START_OR_LEN_OUT_OF_RANGE); // Return null if length is non-positive if (lengthInt < 0) { stringResult.setToNull(); return stringResult; } /* If startInt < 0 then we count from the right of the string */ if (startInt < 0) { // Return '' if window is to left of string. if (startInt + getLength() < 0 && (startInt + getLength() + lengthInt <= 0)) { stringResult.setValue(""); return stringResult; } // Convert startInt to positive to get substring from right startInt += getLength(); while (startInt < 0) { startInt++; lengthInt--; } } else if (startInt > 0) { /* java substring() 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. */ if (lengthInt == 0 || lengthInt <= 0 - startInt || startInt > getLength()) { stringResult.setValue(""); return stringResult; } if (lengthInt >= getLength() - startInt) { stringResult.setValue(getString().substring(startInt)); } else { stringResult.setValue(getString().substring(startInt, startInt + lengthInt)); } return stringResult; } /** * The SQL trim(), ltrim() and rtrim() functions. * * @param trimType Type of trim * @param result The result of a previous call to this method, * null if not called yet. * * @return A StringDataValue containing the result of the trim() * * @exception StandardException Thrown on error */ public StringDataValue trim( int trimType, StringDataValue result) throws StandardException { if (result == null) { result = getNewVarchar(); } /* The result is null if any of the parameters is a user null */ if (this.isNull()) { result.setToNull(); return result; } char[] trimChars = {' '}; String tmpValue = getString(); // Trim leading characters if appropriate if (trimType == LEADING) { int start = 0; // Find the 1st character which doesn't get trimmed for ( ; start < tmpValue.length(); start++) { boolean found = false; for (int index = 0; index < trimChars.length; index++) { if (tmpValue.charAt(start) == trimChars[index]) { found = true; break; } } if (! found) { break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -