1 package com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table; 2 3 import java.util.*; 4 5 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*; 6 import com.daffodilwoods.daffodildb.server.datasystem.utility.*; 7 import com.daffodilwoods.daffodildb.server.sql99.common.*; 8 import com.daffodilwoods.daffodildb.server.sql99.dql.common.*; 9 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*; 10 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 11 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*; 12 import com.daffodilwoods.daffodildb.server.sql99.dql.queryexpression.*; 13 import com.daffodilwoods.daffodildb.server.sql99.dql.resultsetmetadata.*; 14 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 15 import com.daffodilwoods.database.resource.*; 16 import com.daffodilwoods.database.utility.*; 17 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.order.TemporaryIndexIterator; 18 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.order.TemporaryIndexWrapperIterator; 19 import com.daffodilwoods.daffodildb.server.sql99.dql.common.SelectedRecord; 20 import com.daffodilwoods.daffodildb.server.sql99._Executer; 21 import com.daffodilwoods.daffodildb.server.serversystem.CallResult; 22 import com.daffodilwoods.daffodildb.server.serversystem._ServerSession; 23 24 33 34 public abstract class AbstractColumnIterator { 35 36 41 Object [][] groupMapping; 45 int state; 46 49 _Iterator iterator; 50 53 _ColumnCharacteristics columnCharacteristics; 54 58 _RowReader rowReader; 59 62 _VariableValues variableValues; 63 66 protected int type = TypeConstants.OTHER; 67 70 protected Object [][] tableDetailsCC; 71 74 protected Object [][] tableBVE; 75 78 protected queryexpressionbody queryExpressionbody; 79 82 protected _KeyColumnInformation[] keyColumns; 83 86 protected ColumnDetails[] selectColumnDetails; 87 88 91 protected _Reference[] referencesPassed; 92 95 protected Object [] valuesPassed; 96 protected _Reference[] underlyingReferencesSolvableByCurrentIterator; 97 98 protected _Reference[] underlyingRef; 99 100 abstract Object getColumnValues() throws DException; 101 abstract Object getColumnValues(ArrayList arraylist) throws DException; 102 103 abstract Object getObject() throws DException; 104 105 110 public _ColumnCharacteristics getColumnCharacteristics() throws DException { 111 return columnCharacteristics; 112 } 113 114 118 public void beforeFirst() throws DException { 119 state = -1; 120 } 121 122 129 public Object [] fetchForward(int rowCount) throws DException { 130 if (state == 1 || state == 2) { 131 return null; 132 } 133 int count = 0; 134 ArrayList result = new ArrayList(100); 135 ArrayList alist = new ArrayList(20); 136 boolean flag = state == -1 ? iterator.first() : iterator.next(); 137 if (!flag) { 138 state = 1; 139 return null; 140 } 141 count++; 142 state = 0; 143 boolean nextFlag = true; 144 do { 145 try { 146 Object o = getColumnValues(alist); 147 result.add(o); 148 alist.clear(); 149 } catch (DException ex) { 150 if (!ex.getDseCode().equalsIgnoreCase("DSE2004")) { 151 throw ex; 152 } 153 } 154 if (count == rowCount) { 155 break; 156 } 157 count++; 158 } while ( (nextFlag = iterator.next())); 159 if (!nextFlag) { 160 state = 1; 161 } 162 return result.size() == 0 ? null : result.toArray(); 163 } 164 165 176 177 protected Object [] getRow() throws DException { 178 int length = groupMapping.length; 179 ArrayList row = new ArrayList(length); 180 for (int i = 0; i < length; i++) { 181 _Iterator iter = (_Iterator) groupMapping[i][0]; 182 if (iter != null) { Object [] row1 = (Object []) ( (_Iterator) groupMapping[i][0]).getColumnValues( (_Reference[]) groupMapping[i][1]); 184 row.addAll(Arrays.asList(row1)); 185 } else { 186 ColumnDetails[] functional = (ColumnDetails[]) groupMapping[i][1]; 187 for (int j = 0; j < functional.length; j++) { 188 row.add(functional[j].run(variableValues)); 189 } 190 } 191 } 192 return row.toArray(); 193 } 194 195 199 public void afterLast() throws DException { 200 state = 1; 201 } 202 203 210 public Object [] fetchBackward(int rowCount) throws DException { 211 if (state == -1 || state == 2) { 212 return null; 213 } 214 int count = 0; 215 ArrayList result = new ArrayList(rowCount); 216 217 boolean flag = state == 1 ? iterator.last() : iterator.previous(); 218 if (!flag) { 219 state = -1; 220 return null; 221 } 222 count++; 223 state = 0; 224 boolean previousFlag = true; 225 ArrayList alist = new ArrayList(20); 226 227 do { 228 try { 229 result.add(getColumnValues(alist)); 230 alist.clear(); 231 } catch (DException ex) { 232 if (!ex.getDseCode().equalsIgnoreCase("DSE2004")) { 233 throw ex; 234 } 235 } 236 if (count == rowCount) { 237 break; 238 } 239 count++; 240 } while ( (previousFlag = iterator.previous())); 241 if (!previousFlag) { 242 state = -1; 243 } 244 return result.size() == 0 ? null : result.toArray(); 245 } 246 247 253 public void moveToRow(Object keys) throws DException { 254 try { 255 iterator.move(keys); 256 state = 0; 257 } catch (DException ex) { 258 if (ex.getDseCode().equalsIgnoreCase("DSE5518") || 259 ex.getDseCode().equalsIgnoreCase("DSE5510")) { GeneralPurposeStaticClass.printProperties(null, null, null, iterator.getKeyColumnInformations(), null); 261 throw new DException("DSE4123", new Object [] {keys}); 262 } 263 } 264 state = 0; 265 } 266 267 272 public void move(Object key) throws DException { 273 moveToRow(key); 274 } 275 276 284 public int move(int rowCount) throws DException { 285 int count = 0; 286 if (rowCount >= 0) { 287 if (state == -1) { 288 if (iterator.first()) { 289 state = 0; 290 count++; 291 } 292 } while (rowCount > count) { 293 if (iterator.next()) { 294 count++; 295 } else { 296 state = 1; 297 break; 298 } 299 } 300 } else { 301 rowCount = -rowCount; 302 if (state == 1) { 303 if (iterator.last()) { 304 state = 0; 305 count++; 306 } 307 } while (rowCount > count) { 308 if (iterator.previous()) { 309 count++; 310 } else { 311 state = -1; 312 break; 313 } 314 } 315 } 316 return count; 317 } 318 319 326 private _Iterator returnFunctionalCOlumnTableDetail(ColumnDetails column) throws DException { 327 for (int i = 0; i < selectColumnDetails.length; i++) { 328 if (selectColumnDetails[i].getAppropriateColumn().trim().equalsIgnoreCase(column.getAppropriateColumn().trim())) { 329 return (_Iterator)this; 330 } 331 } 332 return null; 333 } 334 335 341 public _RowReader getRowReader() throws DException { 342 return rowReader; 343 } 344 345 350 public _KeyColumnInformation[] getKeyColumnInformation() throws DException { 351 return keyColumns; 352 } 353 354 359 public _KeyColumnInformation[] getKeyColumnInformations() throws DException { 360 return keyColumns; 361 } 362 363 368 public int getRowCount() throws DException { 369 int rowCount = 0; 370 if (iterator.first()) { 371 rowCount++; 372 } else { 373 return rowCount; 374 } while (iterator.next()) { 375 rowCount++; 376 } 377 return rowCount; 378 } 379 380 385 public void releaseResource() throws DException { 386 iterator.releaseResource(); 387 if (variableValues != null) { 388 variableValues.releaseResource(); 389 } 390 } 391 392 397 public _SelectColumnCharacteristics getSelectColumnCharacteristics() throws DException { 398 return (_SelectColumnCharacteristics) columnCharacteristics; 399 } 400 401 413 public void setConditionVariableValue(_Reference[] references, Object [] values, int parm3) throws DException { 414 mergeUnderlyingReferences(references, values); 415 if (underlyingRef != null) { 416 references = GeneralPurposeStaticClass.getJointReferences(references, underlyingRef); 417 values = GeneralPurposeStaticClass.getJointValues(this, values, underlyingRef.length); 418 } 419 iterator.setConditionVariableValue(referencesPassed, valuesPassed, parm3); 420 variableValues.setConditionVariableValue(referencesPassed, valuesPassed, parm3); 421 if (selectColumnDetails != null) { 422 VariableValues vv = new VariableValues(references, variableValues.getServerSession()); 423 vv.setConditionVariableValue(references, values, parm3); 424 for (int i = 0; i < selectColumnDetails.length; i++) { 425 selectColumnDetails[i].setByteComparisonObject(vv); 426 } 427 } 428 } 429 430 437 public ForeignKeyColumns[] getColumnNamesToIndexesMapping(ColumnDetails[] selectListColumnDetails) throws DException { 438 int length = selectListColumnDetails.length; 439 ArrayList columnAndIndex = new ArrayList(); 440 for (int i = 0; i < length; i++) { 441 if (selectListColumnDetails[i].getTableAliasArray() != null) { 442 columnAndIndex.add(new TableAndIndex(selectListColumnDetails[i], i + 1)); 443 } 444 } 445 if (columnAndIndex.isEmpty()) { 446 return null; 447 } 448 ArrayList mapping = new ArrayList(); 449 while (columnAndIndex.size() != 0) { 450 TableAndIndex columnIndex = (TableAndIndex) columnAndIndex.remove(0); 451 String [] source = columnIndex.getColumnDetails().getTableAliasArray(); 452 ArrayList indexes = new ArrayList(); 453 indexes.add(new Integer (columnIndex.getIndex())); 454 for (int i = 0; i < columnAndIndex.size(); i++) { 455 TableAndIndex columnIndexTarget = (TableAndIndex) columnAndIndex.get(i); 456 if (GeneralPurposeStaticClass.compareArrays(source, columnIndexTarget.getColumnDetails().getTableAliasArray())) { 457 indexes.add(new Integer (columnIndexTarget.getIndex())); 458 columnAndIndex.remove(i); 459 --i; 460 } 461 } 462 mapping.add(new ForeignKeyColumns(source, (Integer []) indexes.toArray(new Integer [indexes.size()]))); 463 } 464 return (ForeignKeyColumns[]) mapping.toArray(new ForeignKeyColumns[mapping.size()]); 465 } 466 467 477 private void mergeUnderlyingReferences(_Reference[] references, Object [] values) throws DException { 478 479 int length = 0; 480 if (references != null) { 481 length += references.length; 482 } 483 if (underlyingReferencesSolvableByCurrentIterator != null) { 484 length += underlyingReferencesSolvableByCurrentIterator.length; 485 } 486 if (length == 0) { 487 return; 488 } 489 _Reference[] newReferences = new _Reference[length]; 490 Object [] newValues = new Object [length]; 491 int k = 0; 492 if (references != null) { 493 k = references.length; 494 System.arraycopy(references, 0, newReferences, 0, k); 495 System.arraycopy(values, 0, newValues, 0, k); 496 } 497 if (underlyingReferencesSolvableByCurrentIterator != null) { 498 for (int i = 0; i < underlyingReferencesSolvableByCurrentIterator.length; i++, k++) { 499 newReferences[k] = (_Reference) underlyingReferencesSolvableByCurrentIterator[i]; 500 if (iterator instanceof TemporaryIndexIterator) 501 newValues[k] = ( (TemporaryIndexIterator) iterator).getUnderlyingIterator(); 502 else if (iterator instanceof TemporaryIndexWrapperIterator) 503 newValues[k] = ( (TemporaryIndexWrapperIterator) iterator). 504 getUnderlyingIterator(); 505 else 506 newValues[k] = this; 507 508 } 509 } 510 referencesPassed = newReferences; 511 valuesPassed = newValues; 512 } 513 514 524 public _Iterator getBaseIterator(ColumnDetails parm1) throws DException { 525 return parm1.getTableDetails() != null ? 526 iterator.getBaseIterator(parm1) 527 : returnFunctionalCOlumnTableDetail(parm1); 528 } 529 530 540 protected Object [] getNonSharableFieldRow() throws DException { 541 int length = groupMapping.length; 542 ArrayList row = new ArrayList(length); 543 for (int i = 0; i < length; i++) { 544 _Iterator iter = (_Iterator) groupMapping[i][0]; 545 if (iter != null) { Object [] row1 = (Object []) ( (_Iterator) groupMapping[i][0]).getColumnValues( (_Reference[]) groupMapping[i][1]); 547 row.addAll(Arrays.asList(row1)); 548 } else { 549 ColumnDetails[] functional = (ColumnDetails[]) groupMapping[i][1]; 550 for (int j = 0; j < functional.length; j++) { 551 row.add(functional[j].run(variableValues)); 552 } 553 } 554 } 555 return row.toArray(); 556 } 557 558 protected Object [] getNonSharableFieldRow(ArrayList alist) throws DException { 559 int length = groupMapping.length; 560 for (int i = 0; i < length; i++) { 561 _Iterator iter = (_Iterator) groupMapping[i][0]; 562 if (iter != null) { Object [] row1 = (Object []) ( (_Iterator) groupMapping[i][0]).getColumnValues( (_Reference[]) groupMapping[i][1]); 564 alist.addAll(Arrays.asList(row1)); 565 } else { 566 ColumnDetails[] functional = (ColumnDetails[]) groupMapping[i][1]; 567 for (int j = 0; j < functional.length; j++) { 568 alist.add(functional[j].run(variableValues)); 569 } 570 } 571 } 572 return alist.toArray(); 573 } 574 575 576 577 580 581 public boolean first() throws DException { 582 return iterator.first(); 583 } 584 585 public boolean last() throws DException { 586 return iterator.last(); 587 } 588 589 public boolean next() throws DException { 590 return iterator.next(); 591 } 592 593 public boolean previous() throws DException { 594 return iterator.previous(); 595 } 596 597 public _Iterator getBaseIteratorHasRecord(ColumnDetails parm1) throws DException { 598 return iterator.getBaseIteratorHasRecord(parm1); 599 } 600 601 public void setKeyCount(Object [][] tableAndKeyCount) throws DException { 602 iterator.setKeyCount(tableAndKeyCount); 603 } 604 605 public boolean seekFromTopRelative(Object parm1) throws DException { 606 return iterator.seekFromTopRelative(parm1); 607 } 608 609 public boolean seekFromBottomRelative(Object parm1) throws DException { 610 return iterator.seekFromBottomRelative(parm1); 611 } 612 613 public _OrderCount getOrderCounts() throws DException { 614 return iterator.getOrderCounts(); 615 } 616 617 public _Record getRecord() throws DException { 618 return new SelectedRecord(columnCharacteristics, rowReader, getColumnValues()); 619 } 620 621 public TableDetails[] getTableDetails() throws DException { 622 return iterator.getTableDetails(); 623 } 624 625 public void setSpecificUnderlyingReferences(_Reference[] specificUnderlyingReferences) throws DException { 626 underlyingRef = specificUnderlyingReferences; 627 } 628 629 public boolean seek(Object indexKey) throws DException { 630 throw new UnsupportedOperationException (); 631 } 632 633 public boolean seekFromBottom(_IndexPredicate[] parm1) throws DException { 634 throw new java.lang.UnsupportedOperationException ("Method seekFromBottom() not yet implemented."); 635 } 636 637 public boolean seekFromTop(_IndexPredicate[] parm1) throws DException { 638 639 throw new java.lang.UnsupportedOperationException ("Method seekFromTop() not yet implemented."); 640 } 641 642 public Object [] getUniqueColumnReference() throws DException { 643 throw new UnsupportedOperationException (); 644 } 645 646 public Object refreshRow(Object key) throws DException { 647 throw new UnsupportedOperationException ("Method refreshRow Not Supported in SelectedColumn Iterator"); 648 } 649 650 public Object [] getFunctionalColumnIteratorMapping() throws DException { 651 throw new DException("DSE565", new Object [] {"getFunctionalColumnIteratorMapping()"}); 652 } 653 654 public Object [][] getFunctionalColumnMapping() throws DException { 655 throw new DException("DSE565", new Object [] {"getFunctionalColumnIteratorMapping()"}); 656 } 657 658 public String toString() { 659 return "AbstractColumnIterator[" + iterator + "]"; 660 } 661 } 662 | Popular Tags |