1 package org.apache.ojb.broker.query; 2 3 17 18 34 public class LikeCriteria extends ValueCriteria 35 { 36 39 public static final char DEFAULT_ESCPAPE_CHARACTER = '\\'; 40 41 44 private static char escapeCharacter = DEFAULT_ESCPAPE_CHARACTER; 45 46 52 public LikeCriteria(Object anAttribute, Object aValue, String aClause, String anAlias) 53 { 54 super(anAttribute, generateSQLSearchPattern(aValue), aClause, anAlias); 55 } 56 57 63 public LikeCriteria(Object anAttribute, Object aValue, String aClause, UserAlias anAlias) 64 { 65 super(anAttribute, generateSQLSearchPattern(aValue), aClause, anAlias); 66 } 67 68 71 public void bind(Object newValue) 72 { 73 super.bind(generateSQLSearchPattern(newValue)); 74 } 75 76 86 private static String generateSQLSearchPattern(Object pattern) 87 { 88 if (pattern == null) 89 { 90 return null; 91 } 92 else 93 { 94 StringBuffer sqlpattern = new StringBuffer (); 95 char[] chars = pattern.toString().toCharArray(); 96 97 for (int i = 0; i < chars.length; i++) 98 { 99 if (chars[i] == escapeCharacter) 100 { 101 int x = i + 1; 104 for (;(x < chars.length); x++) 105 { 106 if (chars[x] != escapeCharacter) 107 { 108 break; 109 } 110 } 111 boolean oddEscapes = (((x - i) % 2) > 0) ? true : false; 112 if (oddEscapes) 113 { 114 if ((x < chars.length) 120 && ((chars[x] == '%') || (chars[x] == '_'))) 121 { 122 x++; 124 } 125 else 126 { 127 i++; if ((x < chars.length) 130 && ((chars[x] == '*') || (chars[x] == '?'))) 131 { 132 x++; } 136 } 137 } 138 if (i < chars.length) 139 { 140 sqlpattern.append(chars, i, x - i); 141 } 142 i = x - 1; } 144 else if (chars[i] == '*') 145 { 146 sqlpattern.append("%"); 147 } 148 else if (chars[i] == '?') 149 { 150 sqlpattern.append("_"); 151 } 152 else 153 { 154 sqlpattern.append(chars[i]); 155 } 156 } 157 return sqlpattern.toString(); 158 } 159 } 160 161 164 public static char getEscapeCharacter() 165 { 166 return escapeCharacter; 167 } 168 169 173 public static void setEscapeCharacter(char escChar) 174 { 175 escapeCharacter = escChar; 176 } 177 178 } 179 | Popular Tags |