1 21 package oracle.toplink.essentials.internal.expressions; 23 24 import java.io.*; 25 import java.util.*; 26 import oracle.toplink.essentials.queryframework.*; 27 import oracle.toplink.essentials.expressions.*; 28 import oracle.toplink.essentials.internal.queryframework.*; 29 30 35 public class SubSelectExpression extends Expression { 36 protected Expression baseExpression; 37 protected ReportQuery subQuery; 38 39 public SubSelectExpression() { 40 super(); 41 } 42 43 public SubSelectExpression(ReportQuery query, Expression baseExpression) { 44 this(); 45 this.subQuery = query; 46 this.baseExpression = baseExpression; 47 } 48 49 53 public String descriptionOfNodeType() { 54 return "SubSelect"; 55 } 56 57 public Expression getBaseExpression() { 58 return baseExpression; 59 } 60 61 64 public ExpressionBuilder getBuilder() { 65 return getBaseExpression().getBuilder(); 66 } 67 68 public ReportQuery getSubQuery() { 69 return subQuery; 70 } 71 72 76 public void iterateOn(ExpressionIterator iterator) { 77 super.iterateOn(iterator); 78 if (baseExpression != null) { 79 baseExpression.iterateOn(iterator); 80 } 81 82 if (iterator.shouldIterateOverSubSelects()) { 85 if (getSubQuery().getSelectionCriteria() != null) { 86 getSubQuery().getSelectionCriteria().iterateOn(iterator); 87 } else { 88 getSubQuery().getExpressionBuilder().iterateOn(iterator); 89 } 90 } 91 } 92 93 99 public Expression normalize(ExpressionNormalizer normalizer) { 100 validateNode(); 102 normalizer.addSubSelectExpression(this); 104 normalizer.getStatement().setRequiresAliases(true); 105 return this; 106 } 107 108 113 public Expression normalizeSubSelect(ExpressionNormalizer normalizer, Dictionary clonedExpressions) { 114 if (!getSubQuery().isCallQuery() && (getSubQuery().getReferenceClass() == null)) { 117 ReportQuery subQuery = getSubQuery(); 118 Expression criteria = subQuery.getSelectionCriteria(); 119 120 if (criteria instanceof LogicalExpression) { 124 criteria = ((LogicalExpression)criteria).getFirstChild(); 125 } 126 if (criteria instanceof RelationExpression) { 127 Expression rightChild = ((RelationExpression)criteria).getSecondChild(); 128 if (rightChild instanceof QueryKeyExpression) { 129 subQuery.setReferenceClass(((QueryKeyExpression)rightChild).getDescriptor().getJavaClass()); 130 } 131 } 132 } 133 134 validateNode(); 136 getSubQuery().prepareSubSelect(normalizer.getSession(), null, clonedExpressions); 137 if (!getSubQuery().isCallQuery()) { 138 SQLSelectStatement statement = (SQLSelectStatement)((StatementQueryMechanism)getSubQuery().getQueryMechanism()).getSQLStatement(); 139 140 statement.setRequiresAliases(true); 142 statement.setParentStatement(normalizer.getStatement()); 143 statement.normalize(normalizer.getSession(), getSubQuery().getDescriptor(), clonedExpressions); 144 } 145 return this; 146 } 147 148 151 protected void postCopyIn(Dictionary alreadyDone) { 152 super.postCopyIn(alreadyDone); 153 setBaseExpression(getBaseExpression().copiedVersionFrom(alreadyDone)); 154 ReportQuery clonedQuery = (ReportQuery)getSubQuery().clone(); 155 if ((!clonedQuery.isCallQuery()) && (clonedQuery.getSelectionCriteria() != null)) { 156 clonedQuery.setSelectionCriteria(getSubQuery().getSelectionCriteria().copiedVersionFrom(alreadyDone)); 157 158 if (alreadyDone.get(alreadyDone) != null) { 161 clonedQuery.copyReportItems(alreadyDone); 162 } 163 } 164 setSubQuery(clonedQuery); 165 } 166 167 170 protected void printCustomSQL(ExpressionSQLPrinter printer) { 171 172 176 SQLCall call = (SQLCall)getSubQuery().getCall(); 177 call.translateCustomQuery(); 178 printer.getCall().getParameters().addAll(call.getParameters()); 179 printer.getCall().getParameterTypes().addAll(call.getParameterTypes()); 180 printer.printString(call.getCallString()); 181 } 182 183 186 public void printSQL(ExpressionSQLPrinter printer) { 187 ReportQuery query = getSubQuery(); 188 printer.printString("("); 189 if (query.isCallQuery()) { 190 printCustomSQL(printer); 191 } else { 192 SQLSelectStatement statement = (SQLSelectStatement)((ExpressionQueryMechanism)query.getQueryMechanism()).getSQLStatement(); 193 boolean isFirstElementPrinted = printer.isFirstElementPrinted(); 194 printer.setIsFirstElementPrinted(false); 195 boolean requiresDistinct = printer.requiresDistinct(); 196 statement.printSQL(printer); 197 printer.setIsFirstElementPrinted(isFirstElementPrinted); 198 printer.setRequiresDistinct(requiresDistinct); 199 } 200 printer.printString(")"); 201 } 202 203 206 public Expression rebuildOn(Expression newBase) { 207 return this; 208 } 209 210 protected void setBaseExpression(Expression baseExpression) { 211 this.baseExpression = baseExpression; 212 } 213 214 public void setSubQuery(ReportQuery subQuery) { 215 this.subQuery = subQuery; 216 } 217 218 222 public void writeDescriptionOn(BufferedWriter writer) throws IOException { 223 writer.write(String.valueOf(getSubQuery())); 224 } 225 226 230 public void writeSubexpressionsTo(BufferedWriter writer, int indent) throws IOException { 231 if (getSubQuery().getSelectionCriteria() != null) { 232 getSubQuery().getSelectionCriteria().toString(writer, indent); 233 } 234 } 235 } 236 | Popular Tags |