1 21 package oracle.toplink.essentials.expressions; 23 24 import java.util.*; 25 import java.io.*; 26 import oracle.toplink.essentials.exceptions.*; 27 import oracle.toplink.essentials.queryframework.*; 28 import oracle.toplink.essentials.internal.helper.*; 29 import oracle.toplink.essentials.internal.expressions.*; 30 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 31 import oracle.toplink.essentials.internal.sessions.AbstractSession; 32 import oracle.toplink.essentials.descriptors.ClassDescriptor; 33 34 53 public class ExpressionBuilder extends ObjectExpression { 54 protected transient AbstractSession session; 55 protected Class queryClass; 56 protected SQLSelectStatement statement; 57 protected DatabaseTable viewTable; 58 protected DatabaseTable aliasedViewTable; 59 60 protected boolean wasQueryClassSetInternally = true; 61 62 protected boolean wasAdditionJoinCriteriaUsed = false; 63 64 68 public ExpressionBuilder() { 69 super(); 70 } 71 72 78 public ExpressionBuilder(Class queryClass) { 79 super(); 80 this.queryClass = queryClass; 81 this.wasQueryClassSetInternally = false; 82 } 83 84 88 public DatabaseTable aliasForTable(DatabaseTable table) { 89 if (hasViewTable()) { 90 return getAliasedViewTable(); 91 } 92 93 if (doesNotRepresentAnObjectInTheQuery()) { 94 for (Enumeration e = derivedTables.elements(); e.hasMoreElements();) { 95 TableExpression t = (TableExpression)e.nextElement(); 96 DatabaseTable result = t.aliasForTable(table); 97 if (result != null) { 98 return result; 99 } 100 } 101 } else { 102 return super.aliasForTable(table); 103 } 104 return null; } 106 107 113 public int assignTableAliasesStartingAt(int initialValue) { 114 if (hasBeenAliased()) { 115 return initialValue; 116 } 117 118 if (doesNotRepresentAnObjectInTheQuery()) { 119 return initialValue; 120 } 121 122 if (hasViewTable()) { 131 DatabaseTable aliased = (DatabaseTable)viewTable.clone(); 132 String alias = "t" + initialValue; 133 aliased.setName(alias); 134 assignAlias(alias, viewTable); 135 aliasedViewTable = aliased; 136 return initialValue + 1; 137 } 138 return super.assignTableAliasesStartingAt(initialValue); 139 } 140 141 145 public String descriptionOfNodeType() { 146 return "Base"; 147 } 148 149 158 public boolean doesNotRepresentAnObjectInTheQuery() { 159 return (hasDerivedTables() && !hasDerivedFields() && !hasDerivedExpressions()); 160 } 161 162 165 public DatabaseTable getAliasedViewTable() { 166 return aliasedViewTable; 167 168 } 169 170 175 public ExpressionBuilder getBuilder() { 176 return this; 177 } 178 179 184 public ClassDescriptor getDescriptor() { 185 if (descriptor == null) { 186 if (getQueryClass() == null) { 187 return null; 188 } else { 189 if (getSession() == null) { 190 throw QueryException.noExpressionBuilderFound(this); 191 } 192 descriptor = getSession().getDescriptor(getQueryClass()); 193 } 194 } 195 return descriptor; 196 197 } 198 199 202 public Class getQueryClass() { 203 return queryClass; 204 } 205 206 209 public AbstractSession getSession() { 210 return session; 211 } 212 213 218 public SQLSelectStatement getStatement() { 219 return statement; 220 } 221 222 225 public DatabaseTable getViewTable() { 226 return viewTable; 227 } 228 229 232 public boolean hasViewTable() { 233 return viewTable != null; 234 } 235 236 239 public boolean isExpressionBuilder() { 240 return true; 241 } 242 243 248 public Expression normalize(ExpressionNormalizer normalizer) { 249 if (hasBeenNormalized()) { 250 return this; 251 } else { 252 setHasBeenNormalized(true); 253 } 254 255 if (this.queryClass != null) { 258 Expression criteria = null; 259 260 setSession(normalizer.getSession()); 261 if (getDescriptor() == null) { 263 throw QueryException.noExpressionBuilderFound(this); 264 } 265 if (!this.wasAdditionJoinCriteriaUsed) { 266 criteria = getDescriptor().getQueryManager().getAdditionalJoinExpression(); 267 if (criteria != null) { 268 criteria = twist(criteria, this); 269 } 270 } 271 272 if (isUsingOuterJoinForMultitableInheritance() && getSession().getPlatform().shouldPrintOuterJoinInWhereClause()) { 273 Expression childrenCriteria = getDescriptor().getInheritancePolicy().getChildrenJoinExpression(); 274 childrenCriteria = this.twist(childrenCriteria, this); 275 childrenCriteria.convertToUseOuterJoin(); 276 if(criteria == null) { 277 criteria = childrenCriteria; 278 } else { 279 criteria = criteria.and(childrenCriteria); 280 } 281 } 282 if (isUsingOuterJoinForMultitableInheritance() && (!getSession().getPlatform().shouldPrintOuterJoinInWhereClause())) { 283 normalizer.getStatement().getOuterJoinExpressions().addElement(null); 284 normalizer.getStatement().getOuterJoinedMappingCriteria().addElement(null); 285 normalizer.getStatement().getOuterJoinedAdditionalJoinCriteria().addElement(additionalExpressionCriteriaMap()); 286 normalizer.getStatement().getDescriptorsForMultitableInheritanceOnly().add(this.getDescriptor()); 287 } 289 normalizer.addAdditionalExpression(criteria); 290 291 292 } 293 setStatement(normalizer.getStatement()); 294 295 296 return super.normalize(normalizer); 297 } 298 299 303 public void printJava(ExpressionJavaPrinter printer) { 304 printer.printString(printer.getBuilderString()); 305 } 306 307 313 public Expression rebuildOn(Expression newBase) { 314 return newBase; 315 } 316 317 324 protected Expression registerIn(Dictionary alreadyDone) { 325 Object value = alreadyDone.get(alreadyDone); 327 if ((value == null) || (value == alreadyDone)) { 328 return super.registerIn(alreadyDone); 330 } 331 ObjectExpression copy = (ObjectExpression)value; 332 333 alreadyDone.put(alreadyDone, alreadyDone); 335 alreadyDone.put(this, copy); 336 if (this.derivedExpressions != null) { 338 if (copy.derivedExpressions == null) { 339 copy.derivedExpressions = copyCollection(this.derivedExpressions, alreadyDone); 340 } else { 341 copy.derivedExpressions.addAll(copyCollection(this.derivedExpressions, alreadyDone)); 342 } 343 } 344 345 copy.postCopyIn(alreadyDone, this.derivedFields, this.derivedTables); 347 return copy; 348 } 349 350 354 public void setQueryClass(Class queryClass) { 355 this.queryClass = queryClass; 356 this.descriptor = null; 357 } 358 359 363 public void setSession(AbstractSession session) { 364 this.session = session; 365 } 366 367 372 public void setStatement(SQLSelectStatement statement) { 373 this.statement = statement; 374 } 375 376 380 public void setViewTable(DatabaseTable theTable) { 381 viewTable = theTable; 382 383 } 384 385 391 public void setWasAdditionJoinCriteriaUsed(boolean joinCriteriaUsed){ 392 this.wasAdditionJoinCriteriaUsed = joinCriteriaUsed; 393 } 394 395 405 public Expression twistedForBaseAndContext(Expression newBase, Expression context) { 406 return newBase; 407 } 408 409 413 public Object valueFromObject(Object object, AbstractSession session, AbstractRecord translationRow, InMemoryQueryIndirectionPolicy valueHolderPolicy, boolean isObjectUnregistered) { 414 return object; 415 } 416 417 422 public boolean wasAdditionJoinCriteriaUsed(){ 423 return this.wasAdditionJoinCriteriaUsed; 424 } 425 426 432 public boolean wasQueryClassSetInternally(){ 433 return this.wasQueryClassSetInternally; 434 } 435 436 440 public void writeDescriptionOn(BufferedWriter writer) throws IOException { 441 String className; 442 if (getQueryClass() == null) { 443 className = "QUERY OBJECT"; 444 } else { 445 className = getQueryClass().getName(); 446 } 447 writer.write(className + tableAliasesDescription()); 448 } 449 } 450 | Popular Tags |