KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > iterator > table > AbstractColumnIterator


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 /**
25  * <p>Title: AbstractColumnIterator </p>
26  * <p>Description: This Class provides the common navigation and
27  * retrieval methods for Scrollable, Non-scrollable and Updatable iterators. </p>
28  * <p>Copyright: Copyright (c) 2004</p>
29  * <p>Company: </p>
30  * @author not attributable
31  * @version 1.0
32  */

33
34 public abstract class AbstractColumnIterator {
35
36    /**
37     * An Mapping of iterators corresponding to tables involved in the given result
38     * set and specified columns with respect to specified columns, reference checking
39     * and table.
40     */

41    Object JavaDoc[][] groupMapping; // 0 - Iterator , 1 - ColumnDetails[] , 2 - boolean true for reference false for Functional
42
/**
43     * Variable representing the state of the iterator.
44     */

45    int state;
46    /**
47     * Itertor representing the result set data or underlying iterator.
48     */

49    _Iterator iterator;
50    /**
51     * Columns specified in the select list corresponding to iterator or result set.
52     */

53    _ColumnCharacteristics columnCharacteristics;
54    /**
55     * Variable representing the one row of the result set that assists to perform
56     * data manipulations in this row.
57     */

58    _RowReader rowReader;
59    /**
60     * Variable to maintain the values corresponding to references given in query.
61     */

62    _VariableValues variableValues;
63    /**
64     * NOT USED NOT USED NOT USED NOT USED NOT USED
65     */

66    protected int type = TypeConstants.OTHER;
67    /**
68     * Mapping of tables and given corresponding columns.
69     */

70    protected Object JavaDoc[][] tableDetailsCC;
71    /**
72     * NOT USED NOT USED NOT USED NOT USED NOT USED
73     */

74    protected Object JavaDoc[][] tableBVE;
75    /**
76     * NOT USED NOT USED NOT USED NOT USED NOT USED
77     */

78    protected queryexpressionbody queryExpressionbody;
79    /**
80     * Index columns of given query.
81     */

82    protected _KeyColumnInformation[] keyColumns;
83    /**
84     * Columns specified in given select query.
85     */

86    protected ColumnDetails[] selectColumnDetails;
87
88    /**
89     * References involved in given query.
90     */

91    protected _Reference[] referencesPassed;
92    /**
93     * Values corres. to given references.
94     */

95    protected Object JavaDoc[] valuesPassed;
96    protected _Reference[] underlyingReferencesSolvableByCurrentIterator;
97
98    protected _Reference[] underlyingRef;
99
100    abstract Object JavaDoc getColumnValues() throws DException;
101    abstract Object JavaDoc getColumnValues(ArrayList arraylist) throws DException;
102
103    abstract Object JavaDoc getObject() throws DException;
104
105    /**
106     * This method is used to get columns specified in select query
107     * @return columns in select list
108     * @throws DException
109     */

110    public _ColumnCharacteristics getColumnCharacteristics() throws DException {
111       return columnCharacteristics;
112    }
113
114    /**
115     * Sets the iterators state to before first.
116     * @throws DException
117     */

118    public void beforeFirst() throws DException {
119       state = -1;
120    }
121
122    /**
123     * Navigates the underlying iterator in the forward direction from the
124     * first record till the rowCount th record found.
125     * @param rowCount number of records to be fetched
126     * @return an array of rowCount records
127     * @throws DException
128     */

129    public Object JavaDoc[] 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 JavaDoc 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    /**
166     * This method returns the particular row of the underlying iterator.
167     * This method checkes the selected columns, if the columns corresponds to
168     * any of the iterator, its value is retrieved from the corresponding iterator,
169     * otherwise, corresponding column value is retrieved from excuting the given
170     * value expression.
171     * @return one complete row of the resultset having NonShared FieldBases
172     * denoting the values of all columns of the row. Non Shared FieldBases are
173     * those for which BufferRange is not shared with some other FieldBase.
174     * @throws DException
175     */

176
177    protected Object JavaDoc[] 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) { // Reference Type
183
Object JavaDoc[] row1 = (Object JavaDoc[]) ( (_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    /**
196     * Sets the iterators state to after last.
197     * @throws DException
198     */

199    public void afterLast() throws DException {
200       state = 1;
201    }
202
203    /**
204     * Navigates the underlying iterator in the backward direction from the
205     * last record till the rowCount th record found.
206     * @param rowCount number of records to be fetched
207     * @return an array of rowCount records
208     * @throws DException
209     */

210    public Object JavaDoc[] 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    /**
248     * This method is used to set the iterator at the passed key.
249     * Set the iterator state to VALID State.
250     * @param keys key where to move the iterator
251     * @throws DException
252     */

253    public void moveToRow(Object JavaDoc 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")) { // code changed from 5510 to 5518
260
GeneralPurposeStaticClass.printProperties(null, null, null, iterator.getKeyColumnInformations(), null);
261             throw new DException("DSE4123", new Object JavaDoc[] {keys});
262          }
263       }
264       state = 0;
265    }
266
267    /**
268     * This method is used to set the iterator at the passed key.
269     * @param key key where to move the iterator
270     * @throws DException
271     */

272    public void move(Object JavaDoc key) throws DException {
273       moveToRow(key);
274    }
275
276    /**
277     * This method is used to set the iterator at the rowCount th record.
278     * Checks the passed rowCount and navigates the underlying iterator in the
279     * forward direction till rowCount th record is found.
280     * @param rowCount row number where to move the iteator
281     * @return row number of current row
282     * @throws DException
283     */

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    /**
320     * This method checks the passed column if the column belongs to the query,
321     * current iterator is returned.
322     * @param column column for which iterator is to be return
323     * @return iterator if column corresponds to resultset, null otherwise.
324     * @throws DException
325     */

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    /**
336     * This method is used to get the row of the result set that helps to perform
337     * DML operations in that particular row.
338     * @return current row of result set
339     * @throws DException
340     */

341    public _RowReader getRowReader() throws DException {
342       return rowReader;
343    }
344
345    /**
346     * This method is used to get key column information of given resultset.
347     * @return key columns
348     * @throws DException
349     */

350    public _KeyColumnInformation[] getKeyColumnInformation() throws DException {
351       return keyColumns;
352    }
353
354    /**
355     * This method is used to get key column information of given resultset.
356     * @return key columns
357     * @throws DException
358     */

359    public _KeyColumnInformation[] getKeyColumnInformations() throws DException {
360       return keyColumns;
361    }
362
363    /**
364     * This method is used to get the number of rows in the given resultset.
365     * @return number of rows in resultset.
366     * @throws DException
367     */

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    /**
381     * This method is used to release the resources like comparators from the
382     * iterator.
383     * @throws DException
384     */

385    public void releaseResource() throws DException {
386       iterator.releaseResource();
387       if (variableValues != null) {
388          variableValues.releaseResource();
389       }
390    }
391
392    /**
393     * Returns the selected columns given in select query.
394     * @return selected columns
395     * @throws DException
396     */

397    public _SelectColumnCharacteristics getSelectColumnCharacteristics() throws DException {
398       return (_SelectColumnCharacteristics) columnCharacteristics;
399    }
400
401    /**
402     * This method is used to set the values of the unresolved references in the
403     * given query. Unresolved references may be of following types:
404     * 1. ? in given query, if any.
405     * 2. Outer query column i.e. used in the inner query. For the execution of
406     * the inner query, values of outer columns are supplied as argument to
407     * inner query.
408     * @param references Unresolved references for which values are to set
409     * @param values values to be set corresonding to references
410     * @param parm3 - NOT USED
411     * @throws DException
412     */

413    public void setConditionVariableValue(_Reference[] references, Object JavaDoc[] 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    /**
431     * This method is used to get the chained columns of a select Query.
432     * @param selectListColumnDetails A mapping of selected column and its
433     * index in the given select query list.
434     * @return chained columns of query
435     * @throws DException
436     */

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 JavaDoc[] source = columnIndex.getColumnDetails().getTableAliasArray();
452          ArrayList indexes = new ArrayList();
453          indexes.add(new Integer JavaDoc(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 JavaDoc(columnIndexTarget.getIndex()));
458                columnAndIndex.remove(i);
459                --i;
460             }
461          }
462          mapping.add(new ForeignKeyColumns(source, (Integer JavaDoc[]) indexes.toArray(new Integer JavaDoc[indexes.size()])));
463       }
464       return (ForeignKeyColumns[]) mapping.toArray(new ForeignKeyColumns[mapping.size()]);
465    }
466
467    /**
468     * This method is used to merge the query specification level and sub query
469     * level references. These references are passed to underlying iterators to
470     * set the values of unresolved references so as to execute the predicates
471     * at their level using the values of references given by the user or
472     * retrieved from the current row of the upper level iterator.
473     * @param references references
474     * @param values values of passed corresponding references
475     * @throws DException
476     */

477    private void mergeUnderlyingReferences(_Reference[] references, Object JavaDoc[] 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 JavaDoc[] newValues = new Object JavaDoc[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    /**
515     * This method is used to get the base levekl iterator acc. to column passed
516     * as an argument.
517     * This method checks the passed column, if the passed column is functional
518     * column, its value is retrived from the the maintained mapping corresponding
519     * to passed column, otherwise, value is retrieved form the underlying iterator.
520     * @param parm1 column for which value is to be retrieved
521     * @return value of passed column
522     * @throws DException
523     */

524    public _Iterator getBaseIterator(ColumnDetails parm1) throws DException {
525       return parm1.getTableDetails() != null ?
526           iterator.getBaseIterator(parm1)
527           : returnFunctionalCOlumnTableDetail(parm1);
528    }
529
530    /**
531     * This method returns the particular row of the underlying iterator.
532     * This method checkes the selected columns, if the columns corresponds to
533     * any of the iterator, its value is retrieved from the corresponding iterator,
534     * otherwise, corresponding column value is retrieved from excuting the given
535     * value expression.
536     * @return one complete row of the resultset having values in the form of
537     * shared field bases.
538     * @throws DException
539     */

540    protected Object JavaDoc[] 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) { // Reference Type
546
Object JavaDoc[] row1 = (Object JavaDoc[]) ( (_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 JavaDoc[] 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) { // Reference Type
563
Object JavaDoc[] row1 = (Object JavaDoc[]) ( (_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    /**
578     * The following methods transfer the call to underlying iterator.
579     */

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 JavaDoc[][] tableAndKeyCount) throws DException {
602       iterator.setKeyCount(tableAndKeyCount);
603    }
604
605    public boolean seekFromTopRelative(Object JavaDoc parm1) throws DException {
606       return iterator.seekFromTopRelative(parm1);
607    }
608
609    public boolean seekFromBottomRelative(Object JavaDoc 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 JavaDoc indexKey) throws DException {
630       throw new UnsupportedOperationException JavaDoc();
631    }
632
633    public boolean seekFromBottom(_IndexPredicate[] parm1) throws DException {
634       throw new java.lang.UnsupportedOperationException JavaDoc("Method seekFromBottom() not yet implemented.");
635    }
636
637    public boolean seekFromTop(_IndexPredicate[] parm1) throws DException {
638       /**@todo: Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._IndexIterator method*/
639       throw new java.lang.UnsupportedOperationException JavaDoc("Method seekFromTop() not yet implemented.");
640    }
641
642    public Object JavaDoc[] getUniqueColumnReference() throws DException {
643       throw new UnsupportedOperationException JavaDoc();
644    }
645
646    public Object JavaDoc refreshRow(Object JavaDoc key) throws DException {
647       throw new UnsupportedOperationException JavaDoc("Method refreshRow Not Supported in SelectedColumn Iterator");
648    }
649
650    public Object JavaDoc[] getFunctionalColumnIteratorMapping() throws DException {
651       throw new DException("DSE565", new Object JavaDoc[] {"getFunctionalColumnIteratorMapping()"});
652    }
653
654    public Object JavaDoc[][] getFunctionalColumnMapping() throws DException {
655       throw new DException("DSE565", new Object JavaDoc[] {"getFunctionalColumnIteratorMapping()"});
656    }
657
658    public String JavaDoc toString() {
659       return "AbstractColumnIterator[" + iterator + "]";
660    }
661 }
662
Popular Tags