Package org.apache.derby.iapi.types
Class Like
java.lang.Object
org.apache.derby.iapi.types.Like
Like matching algorithm. Not too speedy for %s.
SQL92 says the escape character can only and must be followed
by itself, %, or _. So if you choose % or _ as the escape character,
you can no longer do that sort of matching.
Not the most recent Like -- missing the unit tests
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final char
private static final char
private static final String
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static boolean
checkEquality
(char[] val, int vLoc, char[] pat, int pLoc, RuleBasedCollator collator) If the character in val matches the character in pat, then it does not matter if the database is UCS_BASIC or territory based, we simply return TRUE from the method.(package private) static Boolean
checkLengths
(int vLoc, int vEnd, int pLoc, char[] pat, int pEnd) checkLengths Returns null if we are not done.(package private) static int
getMinLen
(char[] pattern, int pStart, int pEnd, boolean haveEsc, char escChar) Calculate the shortest length string that could match this patternprivate static String
greaterEqualString
(String pattern, char escChar) greaterEqualString -- for Escape clause only Walk the pattern character by characterstatic String
greaterEqualString
(String pattern, String escape, int maxWidth) Return the substring from the pattern for the optimization >= clause.static String
greaterEqualStringFromParameter
(String pattern, int maxWidth) static String
greaterEqualStringFromParameterWithEsc
(String pattern, String escape, int maxWidth) static boolean
isLikeComparisonNeeded
(String pattern) Return whether or not the like comparison is still needed after performing the like transformation on a constant string.static boolean
isOptimizable
(String pattern) Determine whether or not this LIKE can be transformed into optimizable clauses.static String
lessThanString
(String pattern, String escape, int maxWidth) Return the substring from the pattern for the < clause.static String
lessThanStringFromParameter
(String pattern, int maxWidth) static String
lessThanStringFromParameterWithEsc
(String pattern, String escape, int maxWidth) static Boolean
like
(char[] val, int valLength, char[] pat, int patLength, char[] escape, int escapeLength, RuleBasedCollator collator) This method gets called for UCS_BASIC and territory based character string types to look for a pattern in a value string.static Boolean
like
(char[] value, int valueLength, char[] pattern, int patternLength, RuleBasedCollator collator) private static Boolean
like
(char[] val, int vLoc, int vEnd, char[] pat, int pLoc, int pEnd, char[] escape, int escapeLength, RuleBasedCollator collator) private static String
padWithNulls
(String string, int len) Pad a string with null characters, in order to make it > and < comparable with SQLChar.static String
stripEscapesNoPatternChars
(String pattern, char escChar) stripEscapesNoPatternChars
-
Field Details
-
anyChar
private static final char anyChar- See Also:
-
anyString
private static final char anyString- See Also:
-
SUPER_STRING
- See Also:
-
-
Constructor Details
-
Like
private Like()
-
-
Method Details
-
like
public static Boolean like(char[] val, int valLength, char[] pat, int patLength, char[] escape, int escapeLength, RuleBasedCollator collator) throws StandardException This method gets called for UCS_BASIC and territory based character string types to look for a pattern in a value string. It also deals with escape character if user has provided one.- Parameters:
val
- value to compare. if null, result is null.valLength
- length of valpat
- pattern to compare. if null, result is null.patLength
- length of patescape
- escape character. Must be 1 char long. if null, no escape character is used.escapeLength
- length of escapecollator
- null if we are dealing with UCS_BASIC character string types. If not null, then we use it to get collation elements for characters in val and non-metacharacters in pat to do the comparison.- Returns:
- null if val or pat null, otherwise true if match and false if not.
- Throws:
StandardException
- thrown if data invalid
-
like
private static Boolean like(char[] val, int vLoc, int vEnd, char[] pat, int pLoc, int pEnd, char[] escape, int escapeLength, RuleBasedCollator collator) throws StandardException - Throws:
StandardException
-
checkEquality
private static boolean checkEquality(char[] val, int vLoc, char[] pat, int pLoc, RuleBasedCollator collator) If the character in val matches the character in pat, then it does not matter if the database is UCS_BASIC or territory based, we simply return TRUE from the method. If the characters do not match and we are running with UCS_BASIC collation, then we will return FALSE. But if the database is territory based, then we want to use the Collator for the territory to determine if the Collator treats the 2 characters as equal (ie if their collation elements match, then the 2 characters are equal even if they are not the same character).- Parameters:
val
- value to compare.vLoc
- character position in val.pat
- pattern to look for in val.pLoc
- character position in pat.collator
- null if we are dealing with UCS_BASIC character string types. If not null, then we use it to determine the equality of the 2 characters in pat and val if they are not same.- Returns:
- TRUE if the character in val and vLoc match based on straight equality or collation element based equality. Otherwise we will return FALSE.
-
getMinLen
static int getMinLen(char[] pattern, int pStart, int pEnd, boolean haveEsc, char escChar) Calculate the shortest length string that could match this pattern -
checkLengths
checkLengths Returns null if we are not done. Returns true if we are at the end of our value and pattern Returns false if there is more pattern left but out of input value- Parameters:
vLoc
- current index into char[] valvEnd
- end index or our valuepLoc
- current index into our char[] patternpat
- pattern char []pEnd
- end index of our pattern []
-
like
public static Boolean like(char[] value, int valueLength, char[] pattern, int patternLength, RuleBasedCollator collator) throws StandardException - Throws:
StandardException
-
isOptimizable
Determine whether or not this LIKE can be transformed into optimizable clauses. It can if the pattern is non-null and if the length == 0 or the first character is not a wild card.- Parameters:
pattern
- The right side of the LIKE- Returns:
- Whether or not the LIKE can be transformed
-
greaterEqualStringFromParameter
public static String greaterEqualStringFromParameter(String pattern, int maxWidth) throws StandardException - Throws:
StandardException
-
greaterEqualStringFromParameterWithEsc
public static String greaterEqualStringFromParameterWithEsc(String pattern, String escape, int maxWidth) throws StandardException - Throws:
StandardException
-
greaterEqualString
public static String greaterEqualString(String pattern, String escape, int maxWidth) throws StandardException Return the substring from the pattern for the optimization >= clause.- Parameters:
pattern
- The right side of the LIKEescape
- The escape clausemaxWidth
- Maximum length of column, for null padding- Returns:
- The String for the >= clause
- Throws:
StandardException
-
greaterEqualString
greaterEqualString -- for Escape clause only Walk the pattern character by character- Parameters:
pattern
- like pattern to build fromescChar
- the escape character in the pattern- Throws:
StandardException
-
stripEscapesNoPatternChars
public static String stripEscapesNoPatternChars(String pattern, char escChar) throws StandardException stripEscapesNoPatternChars- Parameters:
pattern
- pattern String to searchescChar
- the escape character- Returns:
- a stripped of ESC char string if no pattern chars, null otherwise
- Throws:
StandardException
- thrown if data invalid
-
lessThanStringFromParameter
public static String lessThanStringFromParameter(String pattern, int maxWidth) throws StandardException - Throws:
StandardException
-
lessThanStringFromParameterWithEsc
public static String lessThanStringFromParameterWithEsc(String pattern, String escape, int maxWidth) throws StandardException - Throws:
StandardException
-
lessThanString
public static String lessThanString(String pattern, String escape, int maxWidth) throws StandardException Return the substring from the pattern for the < clause.- Parameters:
pattern
- The right side of the LIKEescape
- The escape clausemaxWidth
- Maximum length of column, for null padding- Returns:
- The String for the < clause
- Throws:
StandardException
- thrown if data invalid
-
isLikeComparisonNeeded
Return whether or not the like comparison is still needed after performing the like transformation on a constant string. The comparison is not needed if the constant string is of the form: CONSTANT% (constant followed by a trailing %)- Parameters:
pattern
- The right side of the LIKE- Returns:
- Whether or not the like comparison is still needed.
-
padWithNulls
Pad a string with null characters, in order to make it > and < comparable with SQLChar.- Parameters:
string
- The string to padlen
- Max number of characters to pad to- Returns:
- the string padded with 0s up to the given length
-