1 22 23 package org.xquark.extractor.algebra; 24 25 import java.util.*; 26 27 import org.xquark.extractor.common.Debug; 28 import org.xquark.extractor.common.SqlWrapperException; 29 import org.xquark.extractor.xfunctions.AfCast; 30 import org.xquark.extractor.xfunctions.XFunction; 31 import org.xquark.extractor.xfunctions.XfCast; 32 33 public final class RemoveProjectReplaceVisitor extends DefaultAlgebraVisitor { 34 private static final String RCSRevision = "$Revision: 1.4 $"; 35 private static final String RCSName = "$Name: $"; 36 37 public RemoveProjectReplaceVisitor() { 38 } 39 40 public RemoveProjectReplaceVisitor(Map map) { 41 setMap(map); 42 } 43 44 private Map _map; 45 46 51 public Map getMap() { 52 return _map; 53 } 54 55 60 public void setMap(Map aMap) { 61 _map = aMap; 62 } 63 64 public void updateTableInstance(RenameRelation tableInstance) { 65 67 HashMap newMap = new HashMap(); 68 Set keySet = _map.keySet(); 69 Iterator iter = keySet.iterator(); 70 AttributeExpression item = null; 71 Object value = null; 72 while (iter.hasNext()) { 73 item = (AttributeExpression) iter.next(); 74 value = _map.get(item); 75 76 item.setTableInstance(tableInstance); 77 newMap.put(item, value); 78 } 79 setMap(newMap); 80 81 } 83 84 public void visit(Expression arg) throws SqlWrapperException { 85 Debug.assertTrue(false, "NYI!! for " + arg.pprint()); 87 } 89 90 public void visit(Attribute arg) throws SqlWrapperException { 91 Debug.assertTrue(false, "exceptional case:" + arg.toString()); 93 } 95 96 public void visit(AttributeExpression arg) throws SqlWrapperException { 97 99 Expression replacer = (Expression) _map.get(arg); 100 if (null != replacer) { 101 arg.getFather().replaceChild(arg, replacer); 102 } else { 104 Object tableInstance = _map.get(arg.getTableInstance()); 105 if (null == tableInstance) { 106 } else if (tableInstance instanceof NullPointer) { 107 arg.setTableInstance(null); 108 } else { 109 arg.setTableInstance((RenameRelation) tableInstance); 110 } 111 } 112 113 replacer = (Expression) _map.get(arg.getUnderlyinExpr()); 114 if (null != replacer) { 115 arg.getFather().replaceChild(arg, replacer); 116 } 118 119 } 121 122 127 public void visit(BinaryAtomicOp arg) throws SqlWrapperException { 128 arg.getLeftOperand().accept(this); 130 arg.getRightOperand().accept(this); 131 } 133 134 public void visit(BinaryAlgebra arg) throws SqlWrapperException { 135 137 arg.getLeftOperand().accept(this); 138 arg.getRightOperand().accept(this); 139 } 141 142 public void visit(BinOpOuterJoin arg) throws SqlWrapperException { 143 145 List list = arg.getPredicateList(); 146 Debug.assertTrue(null != list, "null!=list"); 147 Expression expr = null; 148 for (int i = 0; i < list.size(); i++) { 149 expr = (Expression) list.get(i); 150 expr.accept(this); 151 } 152 153 arg.getLeftOperand().accept(this); 154 arg.getRightOperand().accept(this); 155 156 } 158 159 public void visit(FunAggregate arg) throws SqlWrapperException { 160 arg.getOperand().accept(this); 162 } 164 165 170 public void visit(Join arg) throws SqlWrapperException { 171 173 List list = arg.getPredicateList(); 174 Debug.assertTrue(null != list, "null!=list"); 175 Expression expr = null; 176 for (int i = 0; i < list.size(); i++) { 177 expr = (Expression) list.get(i); 178 expr.accept(this); 179 } 180 181 list = arg.getOperands(); 182 Debug.assertTrue(null != list, "null!=list"); 183 expr = null; 184 for (int i = 0; i < list.size(); i++) { 185 expr = (Expression) list.get(i); 186 expr.accept(this); 187 } 188 189 } 191 192 public void visit(Literal arg) throws SqlWrapperException { 193 } 196 197 public void visit(RenameRelation arg) throws SqlWrapperException { 198 arg.getOperand().accept(this); 200 } 202 203 public void visit(Table arg) throws SqlWrapperException { 204 } 207 208 214 219 public void visit(UnaryAtomicOp arg) throws SqlWrapperException { 220 arg.getOperand().accept(this); 222 } 224 225 230 236 241 246 251 public void visit(UnOpProject arg) throws SqlWrapperException { 252 254 List list = arg.getItemList(); 255 Debug.assertTrue(null != list, "null!=list"); 256 Expression expr = null; 257 for (int i = 0; i < list.size(); i++) { 258 expr = (Expression) list.get(i); 259 expr.accept(this); 260 } 261 262 arg.getOperand().accept(this); 263 264 } 266 267 public void visit(UnOpRestrict arg) throws SqlWrapperException { 268 List list = arg.getPredicateList(); 270 Debug.assertTrue(null != list, "null!=list"); 271 Expression expr = null; 272 for (int i = 0; i < list.size(); i++) { 273 expr = (Expression) list.get(i); 274 expr.accept(this); 275 } 276 277 arg.getOperand().accept(this); 278 279 } 281 282 public void visit(UnOpSort arg) throws SqlWrapperException { 283 285 List list = arg.getSortSpecificationList(); 286 Debug.assertTrue(null != list, "null!=list"); 287 SortSpecification spec = null; 288 Expression expr = null; 289 for (int i = 0; i < list.size(); i++) { 290 spec = (SortSpecification) list.get(i); 291 expr = spec.getSortExpression(); 292 expr.accept(this); 293 } 294 295 arg.getOperand().accept(this); 296 297 } 299 300 public void visit(AfCast arg) throws SqlWrapperException { 301 arg.getExpression().accept(this); 303 } 305 306 public void visit(XfCast arg) throws SqlWrapperException { 307 arg.getExpression().accept(this); 309 } 311 312 public void visit(XFunction arg) throws SqlWrapperException { 313 315 if (arg.getArgumentNumber() > 0) { 316 List argList = arg.getArguments(); 317 Object argument = null; 318 for (int i = 0; i < argList.size(); i++) { 319 argument = argList.get(i); 320 if (argument instanceof Expression) { 321 ((Expression) argument).accept(this); 322 } 323 } 324 } 325 326 } 328 329 public String printMap() { 330 332 String newLine = System.getProperty("line.separator"); 333 StringBuffer retVal = new StringBuffer (); 334 retVal.append(newLine); 335 retVal.append("<================ RemoveProjectReplaceVisitor ===========>"); 336 retVal.append(newLine); 337 Object keyList[] = _map.keySet().toArray(); 338 Expression key = null; 339 Object value = null; 340 341 for (int i = 0; i < keyList.length; i++) { 342 key = (Expression) keyList[i]; 343 retVal.append(Integer.toString(i)); 344 retVal.append("\t"); 345 346 retVal.append(key.pprint()); 347 retVal.append("\t"); 348 349 value = _map.get(key); 350 if (value instanceof Expression) { 351 retVal.append(((Expression) value).pprint()); 352 } else { 353 retVal.append(value.toString()); 354 } 355 retVal.append(newLine); 356 } 357 358 retVal.append("</================ RemoveProjectReplaceVisitor ===========>"); 359 360 return retVal.toString(); 362 } 363 364 } 365 | Popular Tags |