1 15 package org.josql.expressions; 16 17 import java.util.List ; 18 import java.util.ArrayList ; 19 import java.util.StringTokenizer ; 20 21 import org.josql.Query; 22 import org.josql.QueryExecutionException; 23 import org.josql.QueryParseException; 24 25 import org.josql.internal.Utilities; 26 27 60 public class LikeExpression extends BinaryExpression 61 { 62 63 private boolean not = false; 64 private boolean ignoreCase = false; 65 private List pattern = null; 66 67 public boolean isIgnoreCase () 68 { 69 70 return this.ignoreCase; 71 72 } 73 74 81 public void init (Query q) 82 throws QueryParseException 83 { 84 85 super.init (q); 87 88 if (this.right.hasFixedResult (q)) 89 { 90 91 Object r = null; 93 94 try 95 { 96 97 r = this.right.getValue (null, 98 q); 99 100 } catch (Exception e) { 101 102 throw new QueryParseException ("Unable to get RHS value from: \"" + 103 this.right + 104 "\", expected to RHS to have fixed result.", 105 e); 106 107 } 108 109 if (r == null) 110 { 111 112 return; 114 115 } 116 117 String rs = r.toString (); 118 119 if (this.ignoreCase) 120 { 121 122 rs = rs.toLowerCase (); 123 124 } 125 126 char wc = q.getWildcardCharacter (); 127 128 this.pattern = Utilities.getLikePattern (rs, 129 String.valueOf (wc)); 130 131 } 132 133 } 134 135 public void setIgnoreCase (boolean v) 136 { 137 138 this.ignoreCase = v; 139 140 } 141 142 public boolean isNot () 143 { 144 145 return this.not; 146 147 } 148 149 public void setNot (boolean v) 150 { 151 152 this.not = v; 153 154 } 155 156 168 public boolean isTrue (Object o, 169 Query q) 170 throws QueryExecutionException 171 { 172 173 Object l = this.left.getValue (o, 175 q); 176 177 if (this.pattern != null) 178 { 179 180 return Utilities.matchLikePattern (this.pattern, 181 l, 182 this.not, 183 this.ignoreCase); 184 185 } 186 187 Object r = this.right.getValue (o, 188 q); 189 190 if ((l == null) 191 && 192 (r == null) 193 ) 194 { 195 196 if (this.not) 198 { 199 200 return false; 201 202 } 203 204 return true; 205 206 } 207 208 if ((l == null) 209 || 210 (r == null) 211 ) 212 { 213 214 if (this.not) 215 { 216 217 return true; 218 219 } 220 221 return false; 222 223 } 224 225 String rs = r.toString (); 227 228 if (this.ignoreCase) 229 { 230 231 rs = rs.toLowerCase (); 232 233 } 234 235 char wc = q.getWildcardCharacter (); 237 238 List pat = Utilities.getLikePattern (rs, 239 String.valueOf (wc)); 240 241 return Utilities.matchLikePattern (pat, 242 l, 243 this.not, 244 this.ignoreCase); 245 246 } 247 248 257 public String toString () 258 { 259 260 StringBuffer buf = new StringBuffer (this.left.toString ()); 261 262 if (this.isNot ()) 263 { 264 265 buf.append (" NOT"); 266 267 } 268 269 buf.append (" "); 270 271 if (this.ignoreCase) 272 { 273 274 buf.append ("$"); 275 276 } 277 278 buf.append ("LIKE "); 279 280 buf.append (this.right); 281 282 if (this.isBracketed ()) 283 { 284 285 buf.insert (0, 286 "("); 287 buf.append (")"); 288 289 } 290 291 return buf.toString (); 292 293 } 294 295 } 296 | Popular Tags |