1 21 package oracle.toplink.essentials.internal.expressions; 23 24 import java.io.*; 25 import java.util.*; 26 import oracle.toplink.essentials.exceptions.*; 27 import oracle.toplink.essentials.internal.helper.*; 28 import oracle.toplink.essentials.expressions.*; 29 import oracle.toplink.essentials.internal.databaseaccess.*; 30 31 34 public abstract class CompoundExpression extends Expression { 35 protected ExpressionOperator operator; 36 protected transient ExpressionOperator platformOperator; 37 protected Expression firstChild; 38 protected Expression secondChild; 39 40 public CompoundExpression() { 41 super(); 42 } 43 44 48 public DatabaseTable aliasForTable(DatabaseTable table) { 49 DatabaseTable alias = null; 50 if (getFirstChild() != null) { 51 alias = getFirstChild().aliasForTable(table); 52 } 53 54 if ((alias == null) && (getSecondChild() != null)) { 55 alias = getSecondChild().aliasForTable(table); 56 } 57 58 return alias; 59 } 60 61 64 public Expression create(Expression base, Object singleArgument, ExpressionOperator operator) { 65 setFirstChild(base); 66 Expression argument = Expression.from(singleArgument, base); 67 setSecondChild(argument); 68 setOperator(operator); 69 return this; 70 } 71 72 75 public Expression create(Expression base, Vector arguments, ExpressionOperator operator) { 76 setFirstChild(base); 77 if (!arguments.isEmpty()) { 78 setSecondChild((Expression)arguments.firstElement()); 79 } 80 setOperator(operator); 81 return this; 82 } 83 84 88 public String descriptionOfNodeType() { 89 return "Compound Expression"; 90 } 91 92 96 public ExpressionBuilder getBuilder() { 97 ExpressionBuilder builder = getFirstChild().getBuilder(); 98 if (builder == null) { 99 return getSecondChild().getBuilder(); 100 } else { 101 return builder; 102 } 103 } 104 105 public Expression getFirstChild() { 106 return firstChild; 107 } 108 109 public ExpressionOperator getOperator() { 110 return operator; 111 } 112 113 public ExpressionOperator getPlatformOperator(DatabasePlatform platform) { 114 if (platformOperator == null) { 115 initializePlatformOperator(platform); 116 } 117 return platformOperator; 118 } 119 120 public Expression getSecondChild() { 121 return secondChild; 122 } 123 124 127 public void initializePlatformOperator(DatabasePlatform platform) { 128 if (getOperator().isComplete()) { 129 platformOperator = getOperator(); 130 return; 131 } 132 platformOperator = platform.getOperator(getOperator().getSelector()); 133 if (platformOperator == null) { 134 throw QueryException.invalidOperator(getOperator().toString()); 135 } 136 } 137 138 public boolean isCompoundExpression() { 139 return true; 140 } 141 142 146 public void iterateOn(ExpressionIterator iterator) { 147 super.iterateOn(iterator); 148 if (getFirstChild() != null) { 149 getFirstChild().iterateOn(iterator); 150 } 151 if (getSecondChild() != null) { 152 getSecondChild().iterateOn(iterator); 153 } 154 } 155 156 161 public Expression normalize(ExpressionNormalizer normalizer) { 162 validateNode(); 163 if (getFirstChild() != null) { 164 ExpressionBuilder builder = getFirstChild().getBuilder(); 166 if (builder != null){ 167 builder.setSession(normalizer.getSession()); 168 } 169 setFirstChild(getFirstChild().normalize(normalizer)); 170 } 171 if (getSecondChild() != null) { 172 ExpressionBuilder builder = getSecondChild().getBuilder(); 174 if (builder != null){ 175 builder.setSession(normalizer.getSession()); 176 } 177 setSecondChild(getSecondChild().normalize(normalizer)); 178 } 179 180 if (getFirstChild() == null) { 183 return getSecondChild(); 184 } else if (getSecondChild() == null) { 185 return getFirstChild(); 186 } 187 return this; 188 } 189 190 194 public void validateNode() { 195 if (getFirstChild() != null) { 196 if (getFirstChild().isDataExpression() || getFirstChild().isConstantExpression()) { 197 throw QueryException.invalidExpression(this); 198 } 199 } 200 if (getSecondChild() != null) { 201 if (getSecondChild().isDataExpression() || getSecondChild().isConstantExpression()) { 202 throw QueryException.invalidExpression(this); 203 } 204 } 205 } 206 207 211 protected void postCopyIn(Dictionary alreadyDone) { 212 super.postCopyIn(alreadyDone); 213 if (getFirstChild() != null) { 214 setFirstChild(getFirstChild().copiedVersionFrom(alreadyDone)); 215 } 216 if (getSecondChild() != null) { 217 setSecondChild(getSecondChild().copiedVersionFrom(alreadyDone)); 218 } 219 } 220 221 225 public void printSQL(ExpressionSQLPrinter printer) { 226 ExpressionOperator realOperator = getPlatformOperator(printer.getPlatform()); 227 printer.printString("("); 228 realOperator.printDuo(getFirstChild(), getSecondChild(), printer); 229 printer.printString(")"); 230 } 231 232 236 public void printJava(ExpressionJavaPrinter printer) { 237 ExpressionOperator realOperator = getPlatformOperator(printer.getPlatform()); 238 realOperator.printJavaDuo(getFirstChild(), getSecondChild(), printer); 239 } 240 241 246 public Expression rebuildOn(Expression newBase) { 247 Vector arguments; 248 249 Expression first = getFirstChild().rebuildOn(newBase); 250 if (getSecondChild() == null) { 251 arguments = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(0); 252 } else { 253 arguments = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(1); 254 arguments.addElement(getSecondChild().rebuildOn(newBase)); 255 } 256 return first.performOperator(getOperator(), arguments); 257 } 258 259 protected void setFirstChild(Expression firstChild) { 260 this.firstChild = firstChild; 261 } 262 263 public void setOperator(ExpressionOperator newOperator) { 264 operator = newOperator; 265 } 266 267 protected void setSecondChild(Expression secondChild) { 268 this.secondChild = secondChild; 269 } 270 271 276 public Expression twistedForBaseAndContext(Expression newBase, Expression context) { 277 Vector arguments; 278 279 if (getSecondChild() == null) { 280 arguments = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(0); 281 } else { 282 arguments = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(1); 283 arguments.addElement(getSecondChild().twistedForBaseAndContext(newBase, context)); 284 } 285 286 Expression first = getFirstChild().twistedForBaseAndContext(newBase, context); 287 return first.performOperator(getOperator(), arguments); 288 } 289 290 294 public void writeDescriptionOn(BufferedWriter writer) throws IOException { 295 writer.write(operator.toString()); 296 } 297 298 302 public void writeSubexpressionsTo(BufferedWriter writer, int indent) throws IOException { 303 if (getFirstChild() != null) { 304 getFirstChild().toString(writer, indent); 305 } 306 if (getSecondChild() != null) { 307 getSecondChild().toString(writer, indent); 308 } 309 } 310 } 311 | Popular Tags |