You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by th...@apache.org on 2004/02/23 13:12:46 UTC
cvs commit: db-ojb/src/java/org/apache/ojb/broker/query LikeCriteria.java
thma 2004/02/23 04:12:46
Modified: src/java/org/apache/ojb/broker/query LikeCriteria.java
Log:
cure NPE for null-strings
Revision Changes Path
1.6 +74 -66 db-ojb/src/java/org/apache/ojb/broker/query/LikeCriteria.java
Index: LikeCriteria.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/query/LikeCriteria.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- LikeCriteria.java 21 Feb 2004 21:41:45 -0000 1.5
+++ LikeCriteria.java 23 Feb 2004 12:12:46 -0000 1.6
@@ -99,72 +99,80 @@
* @param pattern a criteria search pattern containing optional wildcards
* @return a SQL search pattern string with all escape codes processed.
*/
- private static String generateSQLSearchPattern(String pattern)
- {
- StringBuffer sqlpattern = new StringBuffer();
- char[] chars = pattern.toCharArray();
+ private static String generateSQLSearchPattern(String pattern)
+ {
+ if (pattern == null)
+ {
+ return null;
+ }
+ else
+ {
+ StringBuffer sqlpattern = new StringBuffer();
+ char[] chars = pattern.toCharArray();
- for (int i = 0; i < chars.length; i++)
- {
- if (chars[i] == escapeCharacter)
- {
- // for the escape character add the next char as is.
- // find the next non-escape character.
- int x = i + 1;
- for (; (x < chars.length); x++)
- {
- if (chars[x] != escapeCharacter)
- {
- break;
- }
- }
- boolean oddEscapes = (((x - i) % 2) > 0) ? true : false;
- if (oddEscapes)
- {
- // only escape characters allowed are '%', '_', and '\'
- // if the escaped character is a '\', then oddEscapes
- // will be false.
- // if the character following this last escape is not a
- // '%' or an '_', eat this escape character.
- if ((x < chars.length) && ((chars[x] == '%') || (chars[x] == '_')))
- {
- // leave the escape character in, along with the following char
- x++;
- }
- else
- {
- // remove the escape character, will cause problems in sql statement.
- i++; // removing the first escape character.
- if ((x < chars.length) && ((chars[x] == '*') || (chars[x] == '?')))
- {
- // but if it is a '*' or a '?', we want to keep these
- // characters as is, they were 'escaped' out.
- x++; // include the first non-escape character.
- }
- }
- }
- if (i < chars.length)
- {
- sqlpattern.append(chars, i, x - i);
- }
- i = x - 1; // set index to last character copied.
- }
- else if (chars[i] == '*')
- {
- sqlpattern.append("%");
- }
- else if (chars[i] == '?')
- {
- sqlpattern.append("_");
- }
- else
- {
- sqlpattern.append(chars[i]);
- }
- }
-
- return sqlpattern.toString();
- }
+ for (int i = 0; i < chars.length; i++)
+ {
+ if (chars[i] == escapeCharacter)
+ {
+ // for the escape character add the next char as is.
+ // find the next non-escape character.
+ int x = i + 1;
+ for (;(x < chars.length); x++)
+ {
+ if (chars[x] != escapeCharacter)
+ {
+ break;
+ }
+ }
+ boolean oddEscapes = (((x - i) % 2) > 0) ? true : false;
+ if (oddEscapes)
+ {
+ // only escape characters allowed are '%', '_', and '\'
+ // if the escaped character is a '\', then oddEscapes
+ // will be false.
+ // if the character following this last escape is not a
+ // '%' or an '_', eat this escape character.
+ if ((x < chars.length)
+ && ((chars[x] == '%') || (chars[x] == '_')))
+ {
+ // leave the escape character in, along with the following char
+ x++;
+ }
+ else
+ {
+ // remove the escape character, will cause problems in sql statement.
+ i++; // removing the first escape character.
+ if ((x < chars.length)
+ && ((chars[x] == '*') || (chars[x] == '?')))
+ {
+ // but if it is a '*' or a '?', we want to keep these
+ // characters as is, they were 'escaped' out.
+ x++; // include the first non-escape character.
+ }
+ }
+ }
+ if (i < chars.length)
+ {
+ sqlpattern.append(chars, i, x - i);
+ }
+ i = x - 1; // set index to last character copied.
+ }
+ else if (chars[i] == '*')
+ {
+ sqlpattern.append("%");
+ }
+ else if (chars[i] == '?')
+ {
+ sqlpattern.append("_");
+ }
+ else
+ {
+ sqlpattern.append(chars[i]);
+ }
+ }
+ return sqlpattern.toString();
+ }
+ }
/**
* @return Returns the escapeCharacter.
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org