db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r411223 - in /db/derby/code/branches/10.1/java: engine/org/apache/derby/iapi/types/ engine/org/apache/derby/impl/sql/compile/ testing/org/apache/derbyTesting/functionTests/master/ testing/org/apache/derbyTesting/functionTests/tests/lang/
Date Fri, 02 Jun 2006 18:00:18 GMT
Author: kahatlen
Date: Fri Jun  2 11:00:17 2006
New Revision: 411223

URL: http://svn.apache.org/viewvc?rev=411223&view=rev
Log:
DERBY-1262: Like-predicates: % does not match tab character

Merged fix from trunk.

Modified:
    db/derby/code/branches/10.1/java/engine/org/apache/derby/iapi/types/Like.java
    db/derby/code/branches/10.1/java/engine/org/apache/derby/impl/sql/compile/LikeEscapeOperatorNode.java
    db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/master/dynamicLikeOptimization.out
    db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/tests/lang/dynamicLikeOptimization.sql

Modified: db/derby/code/branches/10.1/java/engine/org/apache/derby/iapi/types/Like.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.1/java/engine/org/apache/derby/iapi/types/Like.java?rev=411223&r1=411222&r2=411223&view=diff
==============================================================================
--- db/derby/code/branches/10.1/java/engine/org/apache/derby/iapi/types/Like.java (original)
+++ db/derby/code/branches/10.1/java/engine/org/apache/derby/iapi/types/Like.java Fri Jun
 2 11:00:17 2006
@@ -688,22 +688,22 @@
 		return (firstChar != anyChar && firstChar != anyString);
 	}
 
-	public static String greaterEqualStringFromParameter(String pattern)
+	public static String greaterEqualStringFromParameter(String pattern, int maxWidth)
 		throws StandardException {
 
 		if (pattern == null)
 			return null;
 
-		return greaterEqualString(pattern, (String) null);
+		return greaterEqualString(pattern, (String) null, maxWidth);
 	}
 
-	public static String greaterEqualStringFromParameterWithEsc(String pattern, String escape)
+	public static String greaterEqualStringFromParameterWithEsc(String pattern, String escape,
int maxWidth)
 		throws StandardException {
 
 		if (pattern == null)
 			return null;
 
-		return greaterEqualString(pattern, escape);
+		return greaterEqualString(pattern, escape, maxWidth);
 	}
 
 	/**
@@ -711,10 +711,11 @@
 	 *
 	 * @param pattern	The right side of the LIKE
 	 * @param escape	The escape clause
+	 * @param maxWidth	Maximum length of column, for null padding
 	 *
 	 * @return	The String for the >= clause
 	 */
-	public static String greaterEqualString(String pattern, String escape)
+	public static String greaterEqualString(String pattern, String escape, int maxWidth)
 	    throws StandardException
 	{
 
@@ -735,32 +736,29 @@
 				// we return a string stripping out the escape char
 				// leaving the _? in place as normal chars.
                 
-				return greaterEqualString(pattern, escChar);
+				return padWithNulls(greaterEqualString(pattern, escChar), maxWidth);
 			}
 			// drop through if no escape found
 		}
 
 		if (firstAnyChar == -1)
 		{
-			if (firstAnyString == -1)
-			{
-				return pattern;
-			}
-			else	// no _, found %
+			if (firstAnyString != -1) // no _, found %
 			{
-				return pattern.substring(0, firstAnyString);
+				pattern = pattern.substring(0, firstAnyString);
 			}
 		}
 		else if (firstAnyString == -1)
 		{
-			return pattern.substring(0, firstAnyChar);
+			pattern = pattern.substring(0, firstAnyChar);
 		}
 		else
 		{
-			return pattern.substring(0, (firstAnyChar > firstAnyString) ? 
-										firstAnyString :
-										firstAnyChar);
+			pattern = pattern.substring(0, (firstAnyChar > firstAnyString) ? 
+											firstAnyString :
+											firstAnyChar);
 		}
+		return padWithNulls(pattern, maxWidth);
 	}
 
     /** 
@@ -867,11 +865,12 @@
 	 * at the end of the pattern literal.    See LikeEscapeOp*Node.preprocess.
 	 *
 	 * @param pattern	The right side of the LIKE
+	 * @param maxWidth	Maximum length of column, for null padding
 	 *
 	 * @return	The String for the < clause
 	 * @exception StandardException thrown if data invalid
 	 */
-	public static String lessThanString(String pattern)
+	public static String lessThanString(String pattern, int maxWidth)
 		throws StandardException
 	{
 		//int		firstAnyChar = pattern.indexOf(anyChar);
@@ -902,21 +901,23 @@
 		charArray = pattern.substring(0, lastUsableChar + 1).toCharArray();
 		charArray[lastUsableChar] = newLastChar;
 
-		return new String(charArray);
+		return padWithNulls(new String(charArray), maxWidth);
 	}
 
-	public static String lessThanStringFromParameter(String pattern) throws StandardException
{
+	public static String lessThanStringFromParameter(String pattern, int maxWidth)
+		throws StandardException 
+	{
 		if (pattern == null)
 			return null;
-		return lessThanString(pattern, null);
+		return lessThanString(pattern, null, maxWidth);
 	}
 
-	public static String lessThanStringFromParameterWithEsc(String pattern, String escape)
-		 throws StandardException
+	public static String lessThanStringFromParameterWithEsc(String pattern, String escape, int
maxWidth)
+		throws StandardException
 	{
 		if (pattern == null)
 			return null;
-		return lessThanString(pattern, escape);
+		return lessThanString(pattern, escape, maxWidth);
 	}
 
 	/**
@@ -929,11 +930,12 @@
 	 *
 	 * @param pattern	The right side of the LIKE
 	 * @param escape	The escape clause
+	 * @param maxWidth	Maximum length of column, for null padding
 	 *
 	 * @return	The String for the < clause
 	 * @exception StandardException thrown if data invalid
 	 */
-	public static String lessThanString(String pattern, String escape)
+	public static String lessThanString(String pattern, String escape, int maxWidth)
 		throws StandardException
 	{
 		int		lastUsableChar;
@@ -1033,7 +1035,7 @@
 			charArray = pattern.substring(0, lastUsableChar + 1).toCharArray();
 			charArray[lastUsableChar] = newLastChar;
 
-			return new String(charArray);
+			return padWithNulls(new String(charArray), maxWidth);
 		}
 		char[] patternChars = new char[patternLen];
 		char[] result = new char[patternLen];
@@ -1058,7 +1060,7 @@
 		}
 		result[r++] = newLastChar;
 		String gt = new String(result, 0, r);
-		return gt;
+		return padWithNulls(gt, maxWidth);
 	}
 	
 	/**
@@ -1101,5 +1103,24 @@
 		}
 
 		return false;
+	}
+
+	/**
+	 * Pad a string with null characters, in order to make it &gt; and &lt;
+	 * comparable with SQLChar.
+	 * 
+	 * @param string	The string to pad
+	 * @param len		Max number of characters to pad to
+	 * @returns the string padded with 0s up to the given length
+	 */
+	private static String padWithNulls(String string, int len) 
+	{
+		if(string.length() >= len)
+			return string;
+
+		StringBuffer buf = new StringBuffer(len).append(string);
+		buf.setLength(len);
+		
+		return buf.toString();
 	}
 }

Modified: db/derby/code/branches/10.1/java/engine/org/apache/derby/impl/sql/compile/LikeEscapeOperatorNode.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.1/java/engine/org/apache/derby/impl/sql/compile/LikeEscapeOperatorNode.java?rev=411223&r1=411222&r2=411223&view=diff
==============================================================================
--- db/derby/code/branches/10.1/java/engine/org/apache/derby/impl/sql/compile/LikeEscapeOperatorNode.java
(original)
+++ db/derby/code/branches/10.1/java/engine/org/apache/derby/impl/sql/compile/LikeEscapeOperatorNode.java
Fri Jun  2 11:00:17 2006
@@ -554,7 +554,9 @@
 				return this;
 			}
 
-			greaterEqualString = Like.greaterEqualString(pattern, escape);
+			int maxWidth = receiver.getTypeServices().getMaximumWidth();
+			greaterEqualString = Like.greaterEqualString(pattern, escape, 
+														 maxWidth);
 
 
 
@@ -563,7 +565,7 @@
 			 */
             if ( ! receiver.getTypeId().isNationalStringTypeId() )
 			{
-				lessThanString = Like.lessThanString(greaterEqualString);
+				lessThanString = Like.lessThanString(pattern, escape, maxWidth);
 				eliminateLikeComparison = ! Like.isLikeComparisonNeeded(pattern);
 			}
 		}
@@ -610,8 +612,9 @@
 			QueryTreeNode likeLTopt;
 			if (leftOperand.isParameterNode())
 			{
+				int maxWidth = receiver.getTypeServices().getMaximumWidth();
 				likeLTopt = setupOptimizeStringFromParameter(leftOperand, rightOperand,
-								"lessThanStringFromParameter");
+								"lessThanStringFromParameter", maxWidth);
 			}
 			else
 			{
@@ -655,8 +658,9 @@
 			// Create an expression off the parameter
 			// new SQLChar(Like.greaterEqualString(?));
 
+			int maxWidth = receiver.getTypeServices().getMaximumWidth();
 			likeGEopt = setupOptimizeStringFromParameter(leftOperand, rightOperand,
-									"greaterEqualStringFromParameter");
+								"greaterEqualStringFromParameter", maxWidth);
 
 		} else {
 
@@ -789,7 +793,7 @@
 	}
 
 	private ValueNode setupOptimizeStringFromParameter(ValueNode parameterNode,
-						ValueNode escapeNode,String methodName)
+						ValueNode escapeNode,String methodName, int maxWidth)
 		throws StandardException {
 
 		Vector param;
@@ -813,6 +817,12 @@
 		param.addElement(parameterNode);
 		if (escapeNode != null)
 			param.addElement(escapeNode);
+
+		QueryTreeNode maxWidthNode = getNodeFactory().getNode(
+										C_NodeTypes.INT_CONSTANT_NODE,
+										new Integer(maxWidth),
+										getContextManager());
+		param.addElement(maxWidthNode);
 
 		methodCall.addParms(param);
 

Modified: db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/master/dynamicLikeOptimization.out
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/master/dynamicLikeOptimization.out?rev=411223&r1=411222&r2=411223&view=diff
==============================================================================
--- db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/master/dynamicLikeOptimization.out
(original)
+++ db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/master/dynamicLikeOptimization.out
Fri Jun  2 11:00:17 2006
@@ -501,6 +501,24 @@
 ij> execute s using 'values (''Bogus'', ''Name'')';
 ID         |NAME                                                                        
                                                                                         
                         |SOURCE                                                         
                                                                                         
                                                                                         
        
 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ij> -- test control characters
+insert into test values ('asdp', 'asdp', 'asdp');
+1 row inserted/updated/deleted
+ij> insert into test values ('aseg', 'aseg', 'aseg');
+1 row inserted/updated/deleted
+ij> prepare p1 as 'select id from test where c10 like ?';
+ij> execute p1 using 'values ''asd%'' ';
+ID        
+----------
+asdf      
+asdg      
+asdp     
+ij> select c10 from test where c10 like 'asd%';
+C10       
+----------
+asdf      
+asdg      
+asdp     
 ij> -- clean up
 drop table test;
 0 rows inserted/updated/deleted

Modified: db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/tests/lang/dynamicLikeOptimization.sql
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/tests/lang/dynamicLikeOptimization.sql?rev=411223&r1=411222&r2=411223&view=diff
==============================================================================
--- db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/tests/lang/dynamicLikeOptimization.sql
(original)
+++ db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/tests/lang/dynamicLikeOptimization.sql
Fri Jun  2 11:00:17 2006
@@ -169,6 +169,13 @@
 execute s using 'values (''%ing'', ''W\_Unlock\_%Door'')';
 execute s using 'values (''Bogus'', ''Name'')';
 
+-- test control characters
+insert into test values ('asdp', 'asdp', 'asdp');
+insert into test values ('aseg', 'aseg', 'aseg');
+prepare p1 as 'select id from test where c10 like ?';
+execute p1 using 'values ''asd%'' ';
+select c10 from test where c10 like 'asd%';
+
 -- clean up
 drop table test;
 drop table likeable;



Mime
View raw message