1 15 package org.josql.expressions; 16 17 import java.util.List ; 18 import java.util.Map ; 19 import java.util.HashMap ; 20 21 import com.gentlyweb.utils.Getter; 22 23 import org.josql.Query; 24 import org.josql.QueryExecutionException; 25 import org.josql.QueryParseException; 26 27 import org.josql.internal.Utilities; 28 29 95 public class BindVariable extends ValueExpression 96 { 97 98 public static final String SPECIAL_NAME_PREFIX = "_"; 99 private static Map SPECIAL_VAR_NAMES; 100 101 static 102 { 103 104 BindVariable.SPECIAL_VAR_NAMES = new HashMap (); 105 BindVariable.SPECIAL_VAR_NAMES.put (Query.QUERY_BIND_VAR_NAME, 106 ""); 107 BindVariable.SPECIAL_VAR_NAMES.put (Query.CURR_OBJ_VAR_NAME, 108 ""); 109 BindVariable.SPECIAL_VAR_NAMES.put (Query.ALL_OBJS_VAR_NAME, 110 ""); 111 BindVariable.SPECIAL_VAR_NAMES.put (Query.GRPBY_OBJ_VAR_NAME, 112 ""); 113 BindVariable.SPECIAL_VAR_NAMES.put (Query.GRPBY_OBJ_VAR_NAME_SYNONYM, 114 ""); 115 BindVariable.SPECIAL_VAR_NAMES.put (Query.PARENT_BIND_VAR_NAME, 116 ""); 117 118 } 119 120 private String name = null; 121 private Object val = null; 122 private boolean anon = false; 123 private String acc = null; 124 private Getter get = null; 125 private boolean groupByVar = false; 126 private int groupByInd = 0; 127 128 public boolean equals (Object o) 129 { 130 131 if (o == null) 132 { 133 134 return false; 135 136 } 137 138 if (!(o instanceof BindVariable)) 139 { 140 141 return false; 142 143 } 144 145 BindVariable b = (BindVariable) o; 146 147 if ((b.getName () != null) 148 && 149 (this.name != null) 150 && 151 (b.getName ().equals (this.name)) 152 ) 153 { 154 155 if ((this.acc != null) 156 && 157 (b.getAccessor () != null) 158 && 159 (b.getAccessor ().equals (this.acc)) 160 ) 161 { 162 163 return true; 164 165 } 166 167 } 168 169 return false; 170 171 } 172 173 public String getAccessor () 174 { 175 176 return this.acc; 177 178 } 179 180 public void setAccessor (String a) 181 { 182 183 this.acc = a; 184 185 } 186 187 199 public Class getExpectedReturnType (Query q) 200 throws QueryParseException 201 { 202 203 if (this.get != null) 204 { 205 206 return this.get.getType (); 207 208 } 209 210 if (this.val != null) 211 { 212 213 return this.val.getClass (); 214 215 } 216 217 return q.getVariableClass (this.name); 218 219 } 220 221 230 public void init (Query q) 231 throws QueryParseException 232 { 233 234 if (this.anon) 235 { 236 237 this.name = q.getAnonymousBindVariableName (); 238 239 } 240 241 String n = this.name.toLowerCase (); 242 243 if ((n.startsWith (Query.GRPBY_OBJ_VAR_NAME)) 244 || 245 (n.startsWith (Query.GRPBY_OBJ_VAR_NAME_SYNONYM)) 246 ) 247 { 248 249 List grpBys = q.getGroupByColumns (); 250 251 if (grpBys == null) 253 { 254 255 throw new QueryParseException ("Use of special group by object bind variable: " + 256 name + 257 " is not valid when there are no GROUP BY clauses defined."); 258 259 } 260 261 String rest = ""; 262 263 if (n.startsWith (Query.GRPBY_OBJ_VAR_NAME)) 265 { 266 267 rest = n.substring (Query.GRPBY_OBJ_VAR_NAME.length ()); 268 269 } 270 271 if (n.startsWith (Query.GRPBY_OBJ_VAR_NAME_SYNONYM)) 272 { 273 274 rest = n.substring (Query.GRPBY_OBJ_VAR_NAME_SYNONYM.length ()); 275 276 } 277 278 int grpbyind = 1; 279 280 if (rest.length () > 0) 281 { 282 283 try 285 { 286 287 grpbyind = Integer.parseInt (rest); 288 289 } catch (Exception e) { 290 291 throw new QueryParseException ("Special bind variable name: " + 292 this.name + 293 " is not valid, expected an integer number at end of name for indexing into GROUP BYs."); 294 295 } 296 297 if (grpbyind < 1) 298 { 299 300 throw new QueryParseException ("Special bind variable name: " + 301 this.name + 302 " is not valid, integer to index GROUP BYs must be a minimum of 1."); 303 304 } 305 306 if (grpbyind > (grpBys.size ())) 307 { 308 309 throw new QueryParseException ("Special bind variable name: " + 310 this.name + 311 " is not valid, integer references GROUP BY: " + 312 grpbyind + 313 " however there are only: " + 314 grpBys.size () + 315 " GROUP BYs defined."); 316 317 } 318 319 } 320 321 this.groupByVar = true; 322 this.groupByInd = grpbyind; 323 324 } else { 325 326 if (n.startsWith (BindVariable.SPECIAL_NAME_PREFIX)) 327 { 328 329 if (!BindVariable.SPECIAL_VAR_NAMES.containsKey (n)) 331 { 332 333 throw new QueryParseException ("Bind variable name: " + 334 name + 335 " is not valid, bind variable names starting with: " + 336 BindVariable.SPECIAL_NAME_PREFIX + 337 " are reserved, and must be one of: " + 338 BindVariable.SPECIAL_VAR_NAMES.keySet ()); 339 340 } 341 342 } 343 344 } 345 346 this.val = q.getVariable (this.name); 348 349 if ((this.val != null) 351 && 352 (this.acc != null) 353 ) 354 { 355 356 this.initGetter (this.val); 357 358 try 359 { 360 361 this.val = this.get.getValue (this.val); 362 363 } catch (Exception e) { 364 365 throw new QueryParseException ("Unable to get value from accessor: " + 366 this.acc + 367 " and class: " + 368 this.val.getClass ().getName () + 369 " from bind variable: " + 370 this.name, 371 e); 372 373 } 374 375 } 376 377 if ((this.acc != null) 380 && 381 (this.get == null) 382 ) 383 { 384 385 Class c = q.getVariableClass (this.name); 389 390 if (!c.isInstance (new Object ())) 391 { 392 393 this.initGetter (c); 395 396 } 397 398 } 399 400 } 401 402 public String getName () 403 { 404 405 return this.name; 406 407 } 408 409 public boolean isAnonymous () 410 { 411 412 return this.anon; 413 414 } 415 416 public void setAnonymous (boolean v) 417 { 418 419 this.anon = v; 420 421 } 422 423 public void setName (String name) 424 { 425 426 this.name = name; 427 428 } 429 430 private void initGetter (Object o) 431 { 432 433 Class c = o.getClass (); 435 436 this.initGetter (c); 437 438 } 439 440 private void initGetter (Class c) 441 { 442 443 this.get = new Getter (this.acc, 444 c); 445 446 } 447 448 457 public Object getValue (Object o, 458 Query q) 459 throws QueryExecutionException 460 { 461 462 if (this.groupByVar) 463 { 464 465 o = q.getGroupByVariable (this.groupByInd); 466 467 } else { 468 469 o = q.getVariable (this.name); 470 471 } 472 473 if ((this.acc != null) 474 && 475 (this.get == null) 476 && 477 (o != null) 478 ) 479 { 480 481 this.initGetter (o); 483 484 } 485 486 if (this.get != null) 487 { 488 489 try 490 { 491 492 o = this.get.getValue (o); 493 494 } catch (Exception e) { 495 496 throw new QueryExecutionException ("Unable to get value for accessor: " + 497 this.acc + 498 ", class: " + 499 this.get.getBaseClass ().getName () + 500 " from bind variable: " + 501 this.name, 502 e); 503 504 } 505 506 } 507 508 return o; 509 510 } 511 512 522 public boolean isTrue (Object o, 523 Query q) 524 throws QueryExecutionException 525 { 526 527 o = this.getValue (o, 528 q); 529 530 if (o == null) 531 { 532 533 return false; 534 535 } 536 537 if (Utilities.isNumber (o)) 538 { 539 540 return Utilities.getDouble (o) > 0; 541 542 } 543 544 return true; 546 547 } 548 549 558 public Object evaluate (Object o, 559 Query q) 560 throws QueryExecutionException 561 { 562 563 return this.getValue (o, 564 q); 565 566 } 567 568 574 public String toString () 575 { 576 577 StringBuffer buf = new StringBuffer (); 578 579 if (this.anon) 580 { 581 582 buf.append ("?"); 583 584 } else { 585 586 buf.append (":"); 587 buf.append (this.name); 588 589 } 590 591 if (this.acc != null) 592 { 593 594 buf.append ("."); 595 buf.append (this.acc); 596 597 } 598 599 if (this.isBracketed ()) 600 { 601 602 buf.insert (0, 603 "("); 604 buf.append (")"); 605 606 } 607 608 return buf.toString (); 609 610 } 611 612 618 public boolean hasFixedResult (Query q) 619 { 620 621 return false; 623 624 } 625 626 } 627 | Popular Tags |