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.runtime.IDProvider; 30 import org.xquark.extractor.sql.SqlExpression; 31 import org.xquark.extractor.xfunctions.AfTrim; 32 33 public class UnOpProject extends UnaryAlgebra { 34 private static final String RCSRevision = "$Revision: 1.14 $"; 35 private static final String RCSName = "$Name: $"; 36 37 protected boolean _distinct = false; 38 protected List _itemList = null; 39 protected List _itemNames = null; 40 protected UnOpSort _associatedSort = null; 41 protected IDProvider _attIdProvider = null; 42 43 private Set _providedTableInstances = Collections.singleton(this); 44 45 46 public UnOpProject(IDProvider attIdProvider) { 47 super(); 48 _attIdProvider = attIdProvider; 49 } 50 51 public UnOpProject(Expression operand, IDProvider attIdProvider) { 52 super(operand); 53 _attIdProvider = attIdProvider; 54 } 55 56 public UnOpProject(Expression operand, Expression item, IDProvider attIdProvider) { 57 this(operand, attIdProvider); 58 addItem(item); 59 } 60 61 public UnOpProject(Expression operand, List itemList, boolean distinct, IDProvider attIdProvider) { 62 this(operand, attIdProvider); 63 setItemList(itemList); 64 setDistinct(distinct); 65 } 66 67 synchronized Object clone(Map clonedExprs) throws CloneNotSupportedException { 68 UnOpProject retVal = (UnOpProject) super.clone(clonedExprs); 69 if (null != _itemList) { 70 List itemList = AlgebraTools.clone(_itemList, clonedExprs); 71 List itemNames = AlgebraTools.clone(_itemNames); 72 Expression item = null; 73 for (int i = 0; i < itemList.size(); i++) { 74 item = (Expression) itemList.get(i); 75 item.setFather(retVal); 76 } 77 retVal._itemList = itemList; 78 retVal._itemNames = itemNames; 79 } 80 81 clonedExprs.put(this, retVal); 82 return retVal; 83 } 84 85 90 public boolean getDistinct() { 91 return _distinct; 92 } 93 94 99 public void setDistinct(boolean aDistinct) { 100 _distinct = aDistinct; 101 } 102 103 108 public List getItemList() { 109 return _itemList; 110 } 111 112 public void setAssociatedSort(UnOpSort associatedSort) { 113 _associatedSort = associatedSort; 114 } 115 116 public UnOpSort getAssociatedSort() { 117 return _associatedSort; 118 } 119 120 125 public void setItemList(List itemList) { 126 _itemList = null; 127 _itemNames = null; 128 129 if (null == itemList) { 130 return; 131 } 132 addItemList(itemList); 133 } 134 135 public void addItemList(List itemList) { 136 if (null == itemList) { 137 return; 138 } 139 140 Iterator iter = itemList.iterator(); 141 while (iter.hasNext()) { 142 Expression item = (Expression) iter.next(); 143 addItem(item); 144 } 145 } 146 147 public Expression addItem(Expression itemExpr) { 148 if (null != getItemList()) { 149 return insertItem(getItemList().size(), itemExpr); 150 } else { 151 return insertItem(0, itemExpr); 152 } 153 } 154 155 public Expression setItem(int index, Expression itemExpr) { 156 _itemList.remove(index); 157 _itemNames.remove(index); 158 return insertItem(index, itemExpr); 159 } 160 161 public Expression insertItem(int index, Expression itemExpr) { 162 Expression retVal = null; 163 164 if (null == _itemList) { 165 _itemList = new ArrayList(); 166 _itemNames = new ArrayList(); 167 } 168 169 if (itemExpr instanceof Rowid) { 170 itemExpr = new RenameItem(itemExpr, "OracleRowid"); 171 } 172 173 if (itemExpr instanceof TupleExpression) { 174 addItemTupleExpression((TupleExpression) itemExpr); 175 } else if (itemExpr instanceof Relation) { 176 retVal = new RenameItem(itemExpr, _attIdProvider); 177 retVal.setOrginalXExpr(itemExpr.getOrginalXExpr()); 178 _itemList.add(index, retVal); 179 _itemNames.add(index, retVal.getName()); 180 } else { 181 String name = itemExpr.getName(); 182 if (null == name) { 183 retVal = new RenameItem(itemExpr, _attIdProvider); 185 retVal.setOrginalXExpr(itemExpr.getOrginalXExpr()); 186 _itemNames.add(index, retVal.getName()); 187 _itemList.add(index, retVal); 188 } else { 189 if (!hasNameConflict(itemExpr)) { 191 _itemNames.add(index, name); 193 _itemList.add(index, itemExpr); 194 retVal = itemExpr; 195 } else { 196 _itemNames.add(index, name); if (itemExpr instanceof RenameItem) { 199 ((RenameItem) itemExpr).setName(name, _attIdProvider); 200 retVal = itemExpr; 201 } else { 202 retVal = new RenameItem(itemExpr, name, _attIdProvider); 203 retVal.setOrginalXExpr(itemExpr.getOrginalXExpr()); 204 } 205 _itemList.add(index, retVal); 206 } 207 } 208 } 209 210 if (null != retVal) { 211 retVal.setFather(this); 212 } 213 214 return retVal; 215 } 216 217 public void insertItemWithoutRename(int index, FunAggregate funAggregate) { 218 insertItem(index, funAggregate); 219 } 220 221 private boolean hasNameConflict(Expression itemExpr) { 222 return hasNameConflict(itemExpr, _itemList); 223 } 224 225 public static boolean hasNameConflict(Expression itemExpr, List itemList) { 226 boolean retVal = false; 227 228 String newName = itemExpr.getName(); 229 if (null != itemList && null != itemExpr.getName()) { 230 Expression item = null; 231 for (int i = 0; i < itemList.size(); i++) { 232 item = (Expression) itemList.get(i); 233 234 if (item.getName().equalsIgnoreCase(itemExpr.getName())) { 235 retVal = true; 236 break; 237 } 238 } 239 } 240 return retVal; 241 } 242 243 public boolean removeItem(Expression itemExpr) { 244 boolean retVal = false; 245 246 int index = _itemList.indexOf(itemExpr); 247 if (-1 != index) { 248 _itemList.remove(index); 249 _itemNames.remove(index); 250 retVal = true; 251 } 252 return retVal; 253 } 254 255 private List addItemTupleExpression(TupleExpression tupleExpr) { 256 List attrList = tupleExpr.getItemList(); 257 List retVal = new ArrayList(); 258 259 Iterator iter = attrList.iterator(); 260 Expression item = null; 261 while (iter.hasNext()) { 262 item = (Expression) iter.next(); 263 retVal.add(addItem(item)); 264 } 265 tupleExpr.setItemList(retVal); 266 268 for (int i = 0; i < retVal.size(); i++) { 269 item = (Expression) retVal.get(i); 270 item.setFather(this); 271 } 272 return retVal; 273 } 274 275 public List getKeys() { 276 return _keys; 277 } 278 279 public void setKeys(List keys) { 280 _keys = keys; 281 } 282 283 public Expression SearchItem(Expression expr) { 284 Expression retVal = null; 285 Expression item; 286 for (int i = 0; i < _itemList.size(); i++) { 287 item = (Expression) _itemList.get(i); 288 if (item.equals(expr)) { 289 retVal = item; 290 break; 291 } else { 292 item = getRealExpression(item); 293 if (item.equals(expr)) { 294 retVal = item; 295 break; 296 } 297 } 298 } 299 return retVal; 300 } 301 302 private Expression getRealExpression(Expression expr) { 303 if (expr instanceof RenameItem) { 304 return ((RenameItem) expr).getOperand(); 305 } else { 306 return expr; 307 } 308 } 309 310 public void arithmeticOperation(int operater, Expression leftOperand, Expression rightOperand) { 311 Iterator iter = _itemList.iterator(); 312 Expression item; 313 List newItemList = new ArrayList(); 314 Expression arOp; 315 while (iter.hasNext()) { 316 item = getRealExpression((Expression) iter.next()); 317 if (null != leftOperand) { 318 arOp = new BinOpArithmetic(operater, leftOperand, item); 319 } else { 320 arOp = new BinOpArithmetic(operater, item, rightOperand); 321 } 322 newItemList.add(arOp); 323 } 324 setItemList(newItemList); 325 } 326 327 public void addNameTest(String name) { 328 List itemList = ((Relation) _operand).nameTest(name); 329 Expression item = null; 330 331 AttributeExpression attrExpr = null; 332 for (int i = 0; i < itemList.size(); i++) { 333 item = (Expression) itemList.get(0); 334 335 if (item instanceof Attribute) { 336 if (((Attribute) item).getPrimitiveType().isString()) { 337 item = new AfTrim(item); 338 } 339 } 340 addItem(item); 341 } 342 } 343 344 public List nameTest(String name) { 345 List retVal = null; 346 347 if (name.equals("*")) { 348 retVal = _itemList; 349 } else if (null == name) { 351 retVal = new ArrayList(); 352 for (int i = 0; i < _itemNames.size(); i++) { 353 if (null == _itemNames.get(i)) { 354 retVal.add(_itemList.get(i)); 355 } 356 } 357 if (0 == retVal.size()) { 358 retVal = null; 359 } 360 361 } else { 362 retVal = new ArrayList(); 364 for (int i = 0; i < _itemNames.size(); i++) { 365 if (name.equalsIgnoreCase((String ) _itemNames.get(i))) { 366 retVal.add(_itemList.get(i)); 367 } 368 } 369 if (0 == retVal.size()) { 370 retVal = null; 371 } 372 } 373 374 return retVal; 375 } 376 377 public boolean predicateUp(Expression predicate) { 378 return false; 379 } 380 381 public Mapper getMapper() { 382 383 return _mapper; 384 } 385 386 public boolean replaceChild(Expression oldChild, Expression newChild) { 387 boolean retVal = false; 388 List list = getItemList(); 389 390 Expression item = null; 391 String itemName = null; 392 RenameItem renameItem = null; 393 394 for (int i = 0; i < list.size(); i++) { 395 item = (Expression) list.get(i); 396 if (item.equals(oldChild)) { 397 itemName = item.getName(); 398 Debug.assertTrue(null != itemName, "null != itemName"); 399 if (!itemName.equals(newChild.getName())) { 400 401 renameItem = new RenameItem(newChild, itemName); 402 renameItem.setOrginalXExpr(item.getOrginalXExpr()); 403 list.set(i, renameItem); 404 renameItem.setFather(this); 405 } else { 406 list.set(i, newChild); 407 newChild.setFather(this); 408 } 409 retVal = true; 410 break; 411 } 412 } 413 414 if (getOperand().equals(oldChild)) { 415 setOperand(newChild); 416 retVal = true; 417 } 418 419 return retVal; 420 } 421 422 public Set providedTableInstances() { 423 return _providedTableInstances; 424 } 425 426 public AttributeExpression findNonNullAttribute() { 427 Iterator it = _itemList.iterator(); 428 Expression expr = null; 429 AttributeExpression ret = null; 430 431 while (it.hasNext() && ret == null) { 432 expr = (Expression) it.next(); 433 if (expr instanceof AttributeExpression) { 434 ret = (AttributeExpression) expr; 435 Attribute att = (Attribute) ret.getUnderlyinExpr(); 436 if (!att.getAttribute().getNullable()) { 437 ret = new AttributeExpression(ret.getTableInstance(), ret.getName()); 438 ret.setUnderlyingExpr(att); 439 } 440 } else if (expr instanceof RenameItem) { 441 ret = new AttributeExpression(null, ((RenameItem) expr).getName()); 442 } 443 } 444 return ret; 445 } 446 447 public List getParameterList() { 448 return getItemList(); 449 } 450 451 static public Expression identicalExpr(List existingExprList, AttributeExpression newExpr) { 452 Expression retVal = null; 453 Expression expr = null; 454 for (int i = 0; i < existingExprList.size(); i++) { 455 expr = (Expression) existingExprList.get(i); 456 if (isIdentical(expr, newExpr)) { 457 retVal = expr; 458 break; 459 } 460 } 461 return retVal; 462 } 463 464 static public boolean isIdentical(Expression existingExpr, AttributeExpression newExpr) { 465 boolean retVal = true; 466 if (existingExpr instanceof AttributeExpression) { 467 AttributeExpression attrExpr = (AttributeExpression) existingExpr; 468 retVal = newExpr.getAttribute().equals(attrExpr.getAttribute()); 469 if (retVal && null != newExpr.getTableInstance()) { 470 retVal = newExpr.getTableInstance().equals(attrExpr.getTableInstance()); 471 } else if (retVal && null == newExpr.getTableInstance()) { 472 retVal = (null == attrExpr.getTableInstance()); 473 } 474 } else if (existingExpr instanceof RenameItem) { 475 RenameItem renameItem = (RenameItem) existingExpr; 476 retVal = isIdentical(renameItem.getOperand(), newExpr); 477 } else { 478 retVal = false; 479 } 480 return retVal; 481 } 482 483 public SqlExpression accept(GenSqlVisitor visitor) throws SqlWrapperException { 484 return visitor.visit(this); 485 } 486 487 public void accept(AlgebraVisitor visitor) throws SqlWrapperException { 488 visitor.visit(this); 489 } 490 491 494 public boolean deepEquals(Object o) { 495 if (o instanceof UnOpProject) { 496 UnOpProject cast = (UnOpProject) o; 497 return super.deepEquals(o) && _distinct == cast.getDistinct() && _associatedSort != null && _associatedSort.deepEquals(cast.getAssociatedSort()) && AlgebraTools.areExprListEquivalent(_itemList, cast.getItemList()); 498 } 499 return false; 500 } 501 } 502 | Popular Tags |