1 21 package oracle.toplink.essentials.internal.expressions; 23 24 import java.util.*; 25 import java.io.*; 26 import oracle.toplink.essentials.expressions.*; 27 import oracle.toplink.essentials.internal.helper.*; 28 import oracle.toplink.essentials.mappings.*; 29 import oracle.toplink.essentials.querykeys.*; 30 import oracle.toplink.essentials.descriptors.ClassDescriptor; 31 32 36 public abstract class DataExpression extends Expression { 37 protected Vector derivedTables; 38 protected Vector derivedFields; 39 protected Expression baseExpression; 40 protected boolean hasBeenNormalized = false; 41 protected TableAliasLookup tableAliases; 42 43 46 public DataExpression() { 47 super(); 48 } 49 50 public void addDerivedField(Expression addThis) { 51 if (derivedFields == null) { 52 derivedFields = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(4); 53 } 54 derivedFields.addElement(addThis); 55 } 56 57 public void addDerivedTable(Expression addThis) { 58 if (derivedTables == null) { 59 derivedTables = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(4); 60 } 61 derivedTables.addElement(addThis); 62 } 63 64 68 public DatabaseTable aliasForTable(DatabaseTable table) { 69 if (tableAliases == null) { 70 if (getBaseExpression() == null) { 71 return null; 72 } 73 return getBaseExpression().aliasForTable(table); 74 } 75 76 return tableAliases.keyAtValue(table); 77 } 78 79 83 protected void assignAlias(String name, DatabaseTable table) { 84 assignAlias(new DatabaseTable(name), table); 85 } 86 87 91 protected void assignAlias(DatabaseTable alias, DatabaseTable table) { 92 if (tableAliases == null) { 93 tableAliases = new TableAliasLookup(); 94 } 95 tableAliases.put(alias, table); 96 } 97 98 101 public void clearAliases() { 102 tableAliases = null; 103 } 104 105 public Vector copyCollection(Vector in, Dictionary alreadyDone) { 106 if (in == null) { 107 return null; 108 } 109 Vector result = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(in.size()); 110 for (Enumeration e = in.elements(); e.hasMoreElements();) { 111 Expression exp = (Expression)e.nextElement(); 112 result.addElement(exp.copiedVersionFrom(alreadyDone)); 113 } 114 return result; 115 } 116 117 120 public Expression existingDerivedField(DatabaseField field) { 121 if (derivedFields == null) { 122 return null; 123 } 124 for (Enumeration e = derivedFields.elements(); e.hasMoreElements();) { 125 FieldExpression exp = (FieldExpression)e.nextElement(); 126 if (exp.getField().equals(field)) { 127 return exp; 128 } 129 } 130 return null; 131 132 } 133 134 137 public Expression existingDerivedTable(DatabaseTable table) { 138 if (derivedTables == null) { 139 return null; 140 } 141 for (Enumeration e = derivedTables.elements(); e.hasMoreElements();) { 142 TableExpression exp = (TableExpression)e.nextElement(); 143 if (exp.getTable().equals(table)) { 144 return exp; 145 } 146 } 147 return null; 148 149 } 150 151 155 public DatabaseField getAliasedField() { 156 return null; 157 158 } 159 160 public Expression getBaseExpression() { 161 return baseExpression; 162 } 163 164 168 public ExpressionBuilder getBuilder() { 169 if (getBaseExpression() == null) { 170 return null; 171 } 172 return getBaseExpression().getBuilder(); 173 } 174 175 public ClassDescriptor getDescriptor() { 176 return null; 177 178 } 179 180 183 public DatabaseField getField() { 184 return null; 185 } 186 187 public Expression getField(String fieldName) { 188 DatabaseField field = new DatabaseField(fieldName); 189 return getField(field); 190 191 } 192 193 public Expression getField(DatabaseField field) { 194 Expression existing = existingDerivedField(field); 195 if (existing != null) { 196 return existing; 197 } 198 return newDerivedField(field); 199 200 } 201 202 public DatabaseMapping getMapping() { 203 if (getBaseExpression() == null) { 204 return null; 205 } 206 ClassDescriptor aDescriptor = ((DataExpression)getBaseExpression()).getDescriptor(); 207 if (aDescriptor == null) { 208 return null; 209 } 210 return aDescriptor.getObjectBuilder().getMappingForAttributeName(getName()); 211 } 212 213 216 public Vector getOwnedTables() { 217 if (getDescriptor() == null) { 218 return null; 219 } else { 220 if (getDescriptor().isAggregateDescriptor()) { 221 return null; 222 } else { 223 return getDescriptor().getTables(); 224 } 225 } 226 } 227 228 public QueryKey getQueryKeyOrNull() { 229 return null; 230 231 } 232 233 public Expression getTable(String tableName) { 234 DatabaseTable table = new DatabaseTable(tableName); 235 return getTable(table); 236 } 237 238 public Expression getTable(DatabaseTable table) { 239 Expression existing = existingDerivedTable(table); 240 if (existing != null) { 241 return existing; 242 } 243 return newDerivedTable(table); 244 245 } 246 247 251 public TableAliasLookup getTableAliases() { 252 return tableAliases; 253 254 } 255 256 268 public boolean hasBeenNormalized() { 269 return hasBeenNormalized; 270 } 271 272 public boolean hasBeenAliased() { 273 return ((tableAliases != null) && (tableAliases.size() != 0)); 274 275 } 276 277 protected boolean hasDerivedFields() { 278 return derivedFields != null; 279 } 280 281 protected boolean hasDerivedTables() { 282 return derivedTables != null; 283 } 284 285 288 public boolean isAttribute() { 289 return false; 290 } 291 292 public boolean isDataExpression() { 293 return true; 294 } 295 296 300 public void iterateOn(ExpressionIterator iterator) { 301 super.iterateOn(iterator); 302 if (baseExpression != null) { 303 baseExpression.iterateOn(iterator); 304 } 305 } 306 307 public Expression mappingCriteria() { 308 return null; 309 } 310 311 314 public Expression newDerivedField(DatabaseField field) { 315 FieldExpression result = new FieldExpression(field, this); 316 addDerivedField(result); 317 return result; 318 319 } 320 321 324 public Expression newDerivedTable(DatabaseTable table) { 325 TableExpression result = new TableExpression(table); 326 result.setBaseExpression(this); 327 addDerivedTable(result); 328 return result; 329 330 } 331 332 337 public Expression normalize(ExpressionNormalizer normalizer) { 338 if (getBaseExpression() != null) { 339 setBaseExpression(getBaseExpression().normalize(normalizer)); 341 } 342 343 return super.normalize(normalizer); 344 } 345 346 350 protected void postCopyIn(Dictionary alreadyDone) { 351 super.postCopyIn(alreadyDone); 352 clearAliases(); 353 if (baseExpression != null) { 354 baseExpression = baseExpression.copiedVersionFrom(alreadyDone); 355 } 356 derivedFields = copyCollection(derivedFields, alreadyDone); 357 derivedTables = copyCollection(derivedTables, alreadyDone); 358 } 359 360 364 public void printSQL(ExpressionSQLPrinter printer) { 365 printer.printField(getAliasedField()); 366 } 367 368 protected void setBaseExpression(Expression e) { 369 baseExpression = e; 370 } 371 372 public void setHasBeenNormalized(boolean value) { 373 hasBeenNormalized = value; 374 } 375 376 381 public void setTableAliases(TableAliasLookup tableAliases) { 382 if (this.tableAliases == null) { 383 this.tableAliases = tableAliases; 384 } 385 } 386 387 public String tableAliasesDescription() { 388 if (tableAliases == null) { 389 return ""; 390 } 391 return tableAliases.toString(); 392 } 393 394 397 public void writeSubexpressionsTo(BufferedWriter writer, int indent) throws IOException { 398 if (getBaseExpression() != null) { 399 getBaseExpression().toString(writer, indent); 400 } 401 } 402 } 403 | Popular Tags |