1 21 22 package oracle.toplink.essentials.internal.expressions; 24 25 import java.io.*; 26 import java.util.*; 27 import oracle.toplink.essentials.exceptions.*; 28 import oracle.toplink.essentials.expressions.*; 29 import oracle.toplink.essentials.internal.databaseaccess.*; 30 import oracle.toplink.essentials.internal.helper.*; 31 import oracle.toplink.essentials.queryframework.*; 32 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 33 import oracle.toplink.essentials.internal.sessions.AbstractSession; 34 35 39 public class FunctionExpression extends Expression { 40 protected Expression baseExpression; 41 protected Vector children; 42 protected ExpressionOperator operator; 43 protected transient ExpressionOperator platformOperator; 44 protected Class resultType; 45 46 public FunctionExpression() { 47 this.children = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(2); 48 this.resultType = null; 49 } 50 51 public void addChild(Expression child) { 52 getChildren().addElement(child); 53 } 54 55 59 public DatabaseTable aliasForTable(DatabaseTable table) { 60 return getBaseExpression().aliasForTable(table); 61 } 62 63 66 public Expression create(Expression base, Object singleArgument, ExpressionOperator operator) { 67 baseExpression = base; 68 addChild(base); 69 Expression arg = Expression.from(singleArgument, base); 70 addChild(arg); 71 setOperator(operator); 72 return this; 73 } 74 75 79 public Expression createWithBaseLast(Expression base, Object singleArgument, ExpressionOperator anOperator) { 80 baseExpression = base; 81 Expression arg = Expression.from(singleArgument, base); 82 addChild(arg); 83 addChild(base); 84 setOperator(anOperator); 85 return this; 86 } 87 88 91 public Expression create(Expression base, Vector arguments, ExpressionOperator operator) { 92 baseExpression = base; 93 addChild(base); 94 for (Enumeration e = arguments.elements(); e.hasMoreElements();) { 95 Expression arg = Expression.from(e.nextElement(), base); 96 addChild(arg); 97 } 98 setOperator(operator); 99 return this; 100 } 101 102 106 public String descriptionOfNodeType() { 107 return "Function"; 108 } 109 110 116 public boolean doesConform(Object object, AbstractSession session, AbstractRecord translationRow, InMemoryQueryIndirectionPolicy valueHolderPolicy, boolean isObjectUnregistered) { 117 if (getOperator().getSelector() == ExpressionOperator.Not) { 119 return !getBaseExpression().doesConform(object, session, translationRow, valueHolderPolicy, isObjectUnregistered); 120 } 121 122 if ((getOperator().getSelector() == ExpressionOperator.Between) || (getOperator().getSelector() == ExpressionOperator.NotBetween) 124 ||(getOperator().getSelector() == ExpressionOperator.In) || (getOperator().getSelector() == ExpressionOperator.NotIn)) { 125 Object leftValue = getBaseExpression().valueFromObject(object, session, translationRow, valueHolderPolicy, isObjectUnregistered); 127 128 Vector rightValue = new Vector(getChildren().size()); 130 for (int index = 1; index < getChildren().size(); index++) { 131 Object valueFromRight; 132 if (getChildren().elementAt(index) instanceof Expression) { 133 valueFromRight = ((Expression)getChildren().elementAt(index)).valueFromObject(object, session, translationRow, valueHolderPolicy, isObjectUnregistered); 134 } else { 135 valueFromRight = getChildren().elementAt(index); 136 } 137 if (valueFromRight instanceof Vector) { 140 rightValue = (Vector)valueFromRight; 141 } else { 143 rightValue.addElement(valueFromRight); 144 } 145 146 } 147 148 if (leftValue instanceof Vector) { 151 for (Enumeration leftEnum = (Enumeration)((Vector)leftValue).elements(); 152 leftEnum.hasMoreElements();) { 153 Object tempLeft = leftEnum.nextElement(); 154 if (getOperator().doesRelationConform(tempLeft, rightValue)) { 155 return true; 156 } 157 } 158 159 return false; 161 } else { 162 return getOperator().doesRelationConform(leftValue, rightValue); 163 } 164 } else if ((getOperator().getSelector() == ExpressionOperator.IsNull) || (getOperator().getSelector() == ExpressionOperator.NotNull)) { 165 Object leftValue = getBaseExpression().valueFromObject(object, session, translationRow, valueHolderPolicy, isObjectUnregistered); 167 168 if (leftValue instanceof Vector) { 170 for (Enumeration leftEnum = (Enumeration)((Vector)leftValue).elements(); 171 leftEnum.hasMoreElements();) { 172 Object tempLeft = leftEnum.nextElement(); 173 if (getOperator().doesRelationConform(tempLeft, null)) { 174 return true; 175 } 176 } 177 178 return false; 180 } else { 181 return getOperator().doesRelationConform(leftValue, null); 182 } 183 } 184 185 throw QueryException.cannotConformExpression(); 188 } 189 190 public Expression getBaseExpression() { 191 return baseExpression; 192 } 193 194 198 public ExpressionBuilder getBuilder() { 199 if (getBaseExpression() == null) { 200 return null; 201 } 202 return getBaseExpression().getBuilder(); 203 } 204 205 public Vector getChildren() { 206 return children; 207 } 208 209 215 public Vector getFields() { 216 return getBaseExpression().getFields(); 217 } 218 219 public ExpressionOperator getOperator() { 220 return operator; 221 } 222 223 public ExpressionOperator getPlatformOperator(DatabasePlatform platform) { 224 if (platformOperator == null) { 225 initializePlatformOperator(platform); 226 } 227 return platformOperator; 228 } 229 230 public Class getResultType() { 231 return resultType; 232 } 233 234 public boolean hasResultType() { 235 return resultType != null; 236 } 237 238 241 public void initializePlatformOperator(DatabasePlatform platform) { 242 if (getOperator().isComplete()) { 243 platformOperator = getOperator(); 244 return; 245 } 246 platformOperator = platform.getOperator(getOperator().getSelector()); 247 if (platformOperator == null) { 248 throw QueryException.invalidOperator(getOperator().toString()); 249 } 250 } 251 252 public boolean isFunctionExpression() { 253 return true; 254 } 255 256 260 protected boolean isObjectComparison() { 261 if (getChildren().size() != 1) { 262 return false; 263 } 264 265 int selector = getOperator().getSelector(); 266 if ((selector != ExpressionOperator.IsNull) && (selector != ExpressionOperator.NotNull)) { 267 return false; 268 } 269 270 Expression base = getBaseExpression(); 271 return (base.isObjectExpression() && (!((ObjectExpression)base).isAttribute())); 272 } 273 274 278 public void iterateOn(ExpressionIterator iterator) { 279 super.iterateOn(iterator); 280 for (Enumeration childrenEnum = getChildren().elements(); childrenEnum.hasMoreElements();) { 281 Expression child = (Expression)childrenEnum.nextElement(); 282 child.iterateOn(iterator); 283 } 284 } 285 286 292 public Expression normalize(ExpressionNormalizer normalizer) { 293 validateNode(); 297 if (getChildren().isEmpty()) { 298 return this; 299 } 300 301 if (!isObjectComparison()) { 302 for (int index = 0; index < getChildren().size(); index++) { 303 getChildren().setElementAt(((Expression)getChildren().elementAt(index)).normalize(normalizer), index); 304 } 305 return this; 306 } else { 307 for (int index = 0; index < getChildren().size(); index++) { 310 ((Expression)getChildren().elementAt(index)).validateNode(); 311 } 312 } 313 314 ObjectExpression base = (ObjectExpression)getBaseExpression(); 317 318 base.getBaseExpression().normalize(normalizer); 321 322 Expression foreignKeyJoin = base.getMapping().buildObjectJoinExpression(base, (Object )null, getSession()); 325 326 if (getOperator().getSelector() == ExpressionOperator.NotNull) { 327 foreignKeyJoin = foreignKeyJoin.not(); 328 } 329 return foreignKeyJoin; 330 } 331 332 336 protected void postCopyIn(Dictionary alreadyDone) { 337 super.postCopyIn(alreadyDone); 338 339 baseExpression = baseExpression.copiedVersionFrom(alreadyDone); 340 Vector oldChildren = children; 341 children = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(); 342 for (int i = 0; i < oldChildren.size(); i++) { 343 addChild((Expression)(((Expression)oldChildren.elementAt(i)).copiedVersionFrom(alreadyDone))); 344 } 345 } 346 347 351 public void printSQL(ExpressionSQLPrinter printer) { 352 ExpressionOperator realOperator; 353 realOperator = getPlatformOperator(printer.getPlatform()); 354 if (realOperator.getSelector() == ExpressionOperator.In || realOperator.getSelector() == ExpressionOperator.NotIn) { 357 printInCollection(getChildren(), printer, realOperator); 358 } else { 359 realOperator.printCollection(getChildren(), printer); 360 } 361 } 362 363 366 public void printInCollection(Vector items, ExpressionSQLPrinter printer, ExpressionOperator operator) { 367 ((Expression)items.elementAt(0)).printSQL(printer); 368 printer.printString(operator.getDatabaseStrings()[0]); 369 370 for (int i = 1; i < items.size() - 1; i++) { 371 Expression item = (Expression)items.elementAt(i); 372 item.printSQL(printer); 373 printer.printString(", "); 374 } 375 376 ((Expression)items.elementAt(items.size() - 1)).printSQL(printer); 377 printer.printString(operator.getDatabaseStrings()[1]); 378 } 379 380 384 public void printJava(ExpressionJavaPrinter printer) { 385 ExpressionOperator realOperator = getPlatformOperator(printer.getPlatform()); 386 realOperator.printJavaCollection(getChildren(), printer); 387 } 388 389 394 public Expression rebuildOn(Expression newBase) { 395 Expression newLocalBase = getBaseExpression().rebuildOn(newBase); 396 Vector newChildren = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(getChildren().size()); 397 for (int i = 1; i < getChildren().size(); i++) { newChildren.addElement(((Expression)children.elementAt(i)).rebuildOn(newBase)); 399 } 400 newLocalBase.setSelectIfOrderedBy(getBaseExpression().selectIfOrderedBy()); 401 FunctionExpression rebuilt = (FunctionExpression) newLocalBase.performOperator(getOperator(), newChildren); 402 rebuilt.setResultType(this.getResultType()); return rebuilt; 404 } 405 406 413 public void setBaseExpression(Expression expression) { 414 baseExpression = expression; 415 } 416 417 public void setLocalBase(Expression exp) { 420 getBaseExpression().setLocalBase(exp); 421 } 422 423 public void setOperator(ExpressionOperator theOperator) { 424 operator = theOperator; 425 } 426 427 public void setResultType(Class resultType) { 428 this.resultType = resultType; 429 } 430 431 438 public Expression twistedForBaseAndContext(Expression newBase, Expression context) { 439 if (getChildren().isEmpty()) { 440 return (Expression)clone(); 441 } 442 Vector newChildren = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(getChildren().size()); 443 444 for (int index = 1; index < getChildren().size(); index++) { 446 newChildren.addElement(((Expression)children.elementAt(index)).twistedForBaseAndContext(newBase, context)); 447 } 448 449 Expression oldBase = (Expression)getChildren().elementAt(0); 451 return oldBase.twistedForBaseAndContext(newBase, context).performOperator(getOperator(), newChildren); 452 } 453 454 459 public Object valueFromObject(Object object, AbstractSession session, AbstractRecord translationRow, InMemoryQueryIndirectionPolicy valueHolderPolicy, boolean isObjectUnregistered) { 460 Object baseValue = getBaseExpression().valueFromObject(object, session, translationRow, valueHolderPolicy, isObjectUnregistered); 461 Vector arguments = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(getChildren().size()); 462 for (int index = 1; index < getChildren().size(); index++) { 463 if (getChildren().elementAt(index) instanceof Expression) { 464 arguments.addElement(((Expression)getChildren().elementAt(index)).valueFromObject(object, session, translationRow, valueHolderPolicy, isObjectUnregistered)); 465 } else { 466 arguments.addElement(getChildren().elementAt(index)); 467 } 468 } 469 if (baseValue instanceof Vector) { Vector baseVector = new Vector(); 471 for (Enumeration valuesToCompare = (Enumeration)((Vector)baseValue).elements(); 472 valuesToCompare.hasMoreElements();) { 473 Object baseObject = (Object )valuesToCompare.nextElement(); 474 if (baseObject == null) { 475 baseVector.addElement(baseObject); 476 } else { 477 baseVector.addElement(getOperator().applyFunction(baseObject, arguments)); 478 } 479 } 480 return baseVector; 481 } else { 482 if (baseValue == null) { 484 return null; 485 } else { 486 return getOperator().applyFunction(baseValue, arguments); 487 } 488 } 489 } 490 491 495 public void writeDescriptionOn(BufferedWriter writer) throws IOException { 496 writer.write(operator.toString()); 497 } 498 499 502 public void writeFields(ExpressionSQLPrinter printer, Vector newFields, SQLSelectStatement statement) { 503 if (printer.isFirstElementPrinted()) { 505 printer.printString(", "); 506 } else { 507 printer.setIsFirstElementPrinted(true); 508 } 509 510 if (getBaseExpression().isDataExpression()) { 511 DatabaseField field = ((DataExpression)getBaseExpression()).getField(); 512 if (field == null) { 513 field = new DatabaseField("*"); 515 field.setType(getResultType()); 516 } else { 517 if (hasResultType()) { 521 field = (DatabaseField) field.clone(); 522 field.setType(getResultType()); 523 } 524 } 525 526 newFields.addElement(field); 527 } else { 528 DatabaseField field = new DatabaseField("*"); 530 field.setType(getResultType()); 531 newFields.addElement(field); 532 } 533 534 printSQL(printer); 535 } 536 537 541 public void writeSubexpressionsTo(BufferedWriter writer, int indent) throws IOException { 542 if (baseExpression != null) { 543 baseExpression.toString(writer, indent); 544 } 545 } 546 } | Popular Tags |