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 30 public final class AddKeys extends DefaultAlgebraVisitor { 31 private static final String RCSRevision = "$Revision: 1.7 $"; 32 private static final String RCSName = "$Name: $"; 33 34 private Stack _returnStack = new Stack(); 35 private boolean _cleBidon = false; 36 37 public AddKeys() { 38 } 39 40 public void reinit() { 41 _returnStack.clear(); 42 _cleBidon = false; 43 } 44 45 public boolean isHeavyEliminationNeeded() { 46 return _cleBidon; 47 } 48 49 52 public List getKeys() { 53 List retVal = (List) _returnStack.peek(); 54 Debug.assertTrue(retVal.size() > 0, "retVal.size()>0"); 55 return retVal; 56 } 57 58 public void visit(DummyTable arg) throws SqlWrapperException { 59 } 62 63 public void visit(Expression arg) throws SqlWrapperException { 64 Debug.assertTrue(false, "AddKeys.visit() not implemented for " + arg.pprint()); 66 } 68 69 public void visit(BinOpOuterJoin arg) throws SqlWrapperException { 70 List allKeys = new ArrayList(); 72 73 List keys; 74 Expression operand; 75 76 operand = arg.getLeftOperand(); 77 operand.accept(this); 78 keys = (List) _returnStack.pop(); 79 allKeys.addAll(keys); 80 81 operand = arg.getRightOperand(); 82 operand.accept(this); 83 keys = (List) _returnStack.pop(); 84 allKeys.addAll(keys); 85 86 arg.setKeys(allKeys); 87 _returnStack.push(allKeys); 88 } 90 91 public void visit(Join arg) throws SqlWrapperException { 92 94 List allKeys = new ArrayList(); 95 List keys; 96 97 Iterator iter = arg.getOperandList().iterator(); 98 Expression operand = null; 99 while (iter.hasNext()) { 100 operand = (Expression) iter.next(); 101 operand.accept(this); 102 keys = (List) _returnStack.pop(); 103 allKeys.addAll(keys); 104 } 105 106 arg.setKeys(allKeys); 107 _returnStack.push(allKeys); 108 110 } 111 112 public void visit(RenameRelation arg) throws SqlWrapperException { 113 115 arg.getOperand().accept(this); 116 List allKeys = (List) _returnStack.peek(); 117 118 List keys = null; 119 try { 120 keys = AlgebraTools.clone(allKeys); } catch (CloneNotSupportedException ex) { 122 throw new SqlWrapperException(ex.getMessage(), ex); 123 } 124 125 126 Iterator iter = keys.iterator(); 127 while (iter.hasNext()) { 128 ((AttributeExpression) iter.next()).setTableInstance(arg); 129 } 130 131 arg.setKeys(keys); 132 _returnStack.push(keys); 133 } 135 136 public void visit(Table arg) throws SqlWrapperException { 137 139 List allKeys = new ArrayList(); 140 141 List keysList = arg.getKeys(); 142 List key = null; 143 if (null == keysList || keysList.isEmpty()) { 144 key = arg.getAttributes(); 145 _cleBidon = true; 146 } else 147 key = (List) keysList.get(0); 149 arg.setUsedKeyAttributeList(key); 150 151 Attribute keyAttribute = null; 152 AttributeExpression keyAttributeExpression = null; 153 154 for (int i = 0; i < key.size(); i++) { 155 keyAttribute = (Attribute) key.get(i); 156 if (keyAttribute instanceof PseudoAttribute) { 157 keyAttributeExpression = new Rowid(null, keyAttribute.getName()); 158 } else { 159 keyAttributeExpression = new AttributeExpression(null, keyAttribute.getName()); 160 } 161 keyAttributeExpression.setUnderlyingExpr(keyAttribute); 162 allKeys.add(keyAttributeExpression); 163 } 164 165 _returnStack.push(allKeys); 166 } 168 169 public void visit(UnaryOperator arg) throws SqlWrapperException { 170 arg.getOperand().accept(this); 172 } 174 175 public void visit(UnOpProject arg) throws SqlWrapperException { 176 178 List retVal = null; 179 AttributeExpression newAttrItem; 180 181 if (arg.getDistinct()) { 182 187 List selectList = arg.getItemList(); 188 List allKeys = new ArrayList(); 189 Iterator iter = selectList.iterator(); 190 Expression item; 191 while (iter.hasNext()) { 192 item = (Expression) iter.next(); 193 newAttrItem = new AttributeExpression(null, item.getName()); 194 newAttrItem.setUnderlyingExpr(item); 195 allKeys.add(newAttrItem); 196 } 197 retVal = allKeys; 198 } else { 199 202 if (null == arg.getKeys()) { 203 arg.getOperand().accept(this); 204 List keys = (List) _returnStack.pop(); 205 206 List newKeys = new ArrayList(); 207 208 Iterator iter = keys.iterator(); 209 AttributeExpression newItem; 210 Expression selectItem, keyItem; 211 List selectList = arg.getItemList(); 212 while (iter.hasNext()) { 213 keyItem = (AttributeExpression) iter.next(); 214 selectItem = UnOpProject.identicalExpr(selectList, (AttributeExpression) keyItem); 215 if (null == selectItem) { 216 keyItem = arg.addItem(keyItem); 217 } else { 218 keyItem = selectItem; 219 } 220 newAttrItem = new AttributeExpression(null, keyItem.getName()); 221 newAttrItem.setUnderlyingExpr(keyItem); 222 223 newKeys.add(newAttrItem); 224 } 225 arg.setKeys(newKeys); 226 retVal = newKeys; 227 } else { 228 retVal = arg.getKeys(); 229 } 230 } 231 232 _returnStack.push(retVal); 233 } 235 236 public void visit(UnOpGroup arg) throws SqlWrapperException { 237 List keys = null; 239 240 try { 241 keys = AlgebraTools.clone(arg.getKeys()); 242 } catch (CloneNotSupportedException ex) { 243 throw new SqlWrapperException(ex.getMessage(), ex); 244 } 245 246 _returnStack.push(keys); 247 } 249 } 250 | Popular Tags |