KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table;
2
3 import java.util.*;
4
5 import com.daffodilwoods.daffodildb.client.*;
6 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
7 import com.daffodilwoods.daffodildb.server.serversystem.*;
8 import com.daffodilwoods.daffodildb.server.sql99.common.*;
9 import com.daffodilwoods.daffodildb.server.sql99.dql.common.*;
10 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*;
11 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
12 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*;
13 import com.daffodilwoods.daffodildb.server.sql99.dql.queryexpression.queryspecification.*;
14 import com.daffodilwoods.daffodildb.server.sql99.dql.resultsetmetadata.*;
15 import com.daffodilwoods.daffodildb.server.sql99.dql.semanticchecker.*;
16 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
17 import com.daffodilwoods.daffodildb.utils.field.*;
18 import com.daffodilwoods.database.resource.*;
19 import com.daffodilwoods.database.sqlinitiator.*;
20 import com.daffodilwoods.database.utility.*;
21 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.Utility;
22 import com.daffodilwoods.daffodildb.server.sql99.expression.expressionprimary.subquery;
23
24 /**
25  *
26  * <p>Title: SelectedColumnIterator</p>
27  * <p>Description:
28  * This class represents Top level Iterator for Simple Query(Except SetOperators).
29  *
30  * This class maintains the mapping of selectedColumn and their base level Iterator.
31  * This is done for optimization purpose, to retrieve the value by directly getting
32  * it from mapping instead of traversal of whole tree of Iterators.
33  *
34  * Also an array of int is maintained to indicate the indices of selected column
35  * in the row. Because we group the selected columns according to their base
36  * level Iterator. So their positions are changed in resultant row. Thats why this
37  * mapping is maintained.
38  * </p>
39  * <p>Copyright: Copyright (c) 2003</p>
40  * <p>Company: Daffodil S/W Ltd.</p>
41  * @author SelectTeam
42  * @version 1.0
43  */

44
45 public class SelectedColumnIterator extends AbstractColumnIterator implements _SelectQueryIterator, TypeConstants, _FunctionalMapping {
46
47   /**
48    * Mapping of selected column and its index in the given select query list.
49    */

50   private int[] selectedColumnMapping;
51   /**
52    * Total length of selected columns specified in select list of given query.
53    */

54   private int allColumnLength;
55   /**
56    * Columns corresponding to given expression(s) in the select list.
57    */

58   private ColumnDetails[] functionalColumns;
59   /**
60    * Mapping of functional columns and their iterators.
61    */

62   private Object JavaDoc[][] functionalColumnIteratorMapping;
63   /**
64    * Mapping of selelected columns and their base level iterators.
65    */

66   protected Object JavaDoc[][] tableAndIteratorMapping;
67   /**
68    * A list of Objects reperesenting the VIEWS involved in the given query.
69    */

70   private TableDetails[] viewTables;
71   /**
72    * Variable representing the iterator is scorllable, non-scrollable or
73    * updatable.
74    */

75   private int iteratorType;
76   /**
77    * Variable representing the actual query.
78    */

79   private queryspecification qs;
80 /*Not used--to remove*/
81   private _Reference[] underlyingReferences;
82    /**
83     * This constructor is initiated in case of NONSCROLLABLE Type. The major works
84     * done are as follows
85     * 1. To maintain the mapping of Selected Columns and Base level Iterator and
86     * also maintain the indices of selected columns in the row.
87     * 2. Initialize the SelectedColumnCharacteristics. This is needed to get all
88     * the information about Selected Columns
89     * 3. Initialize the SelectedRowReader. This is needed for reading the row.
90     * @param selectListColumnDetails list of specified columns in select list
91     * @param iterator0 underlying iterator having the actual data
92     * @param selectReferences columns of select whose values are to be provided by user
93     * @param tableDetails list of all tables involved in the from clause
94     * @param isView boolean variable representing the table or view
95     * @param underlyingReferencesSolvableByCurrentIterator0
96     * @param serverSession
97     * @param qs
98     * @throws DException
99     */

100
101    public SelectedColumnIterator(ColumnDetails[] selectListColumnDetails, _Iterator iterator0, _Reference[] selectReferences, TableDetails[] tableDetails, boolean isView, _Reference[] underlyingReferencesSolvableByCurrentIterator0, _ServerSession serverSession, queryspecification qs ,_Reference[] ref,_Iterator[] iter ) throws DException {
102       this.qs = qs;
103       init(iterator0, selectListColumnDetails, tableDetails, selectReferences, null, serverSession ,ref,iter);
104       keyColumns = iterator0.getKeyColumnInformations();
105       selectedColumnMapping = groupColumnsAccToIterators(selectListColumnDetails, true); // INDEX IS ONE BASE
106
if (!isView) {
107          columnCharacteristics = new SelectColumnCharacteristics(selectListColumnDetails, tableDetailsCC, false);
108          rowReader = new SelectRowReader(columnCharacteristics, getColumnNamesToIndexesMapping(selectListColumnDetails));
109       }
110       state = 2;
111       GeneralPurposeStaticClass.printProperties(iterator0, null, null, null, null);
112       underlyingReferencesSolvableByCurrentIterator = underlyingReferencesSolvableByCurrentIterator0;
113       keyColumns = iterator.getKeyColumnInformations();
114       iteratorType = IteratorConstants.NONSCROLLABLE;
115    }
116
117    /**
118     * This constructor is initiated in case of SCROLLABLE Type. The major works
119     * done are as follows
120     * 1. To maintain the mapping of Selected Columns and Base level Iterator and
121     * also maintain the indices of selected columns in the row.
122     * 2. Initialize the SelectedColumnCharacteristics. This is needed to get all
123     * the information about Selected Columns
124     * 3. Initialize the RowReaderScrollable. This is needed for reading the row.
125     * @param selectListColumnDetails list of specified columns in select list
126     * @param iterator0 underlying iterator having the actual data
127     * @param selectReferences columns of select whose values are to be provided by user
128     * @param tableDetails list of all tables involved in the from clause
129     * @param isView boolean variable representing the table or view
130     * @param iteratorType iterator is scrollable or non-scrollable
131     * @param underlyingReferencesSolvableByCurrentIterator0
132     * @param serverSession
133     * @param qs
134     * @throws DException
135     */

136
137    public SelectedColumnIterator(ColumnDetails[] selectListColumnDetails, _Iterator iterator0, _Reference[] selectReferences, TableDetails[] tableDetails, boolean isView, int iteratorType, _Reference[] underlyingReferencesSolvableByCurrentIterator0, _ServerSession serverSession, queryspecification qs ,_Reference[] ref,_Iterator[] iter) throws DException {
138       this.qs = qs;
139       init(iterator0, selectListColumnDetails, tableDetails, selectReferences, null, serverSession , ref, iter);
140       keyColumns = iterator.getKeyColumnInformations();
141       selectedColumnMapping = groupColumnsAccToIterators(selectListColumnDetails, false); // for getSelectedColumnValues()
142

143       selectListColumnDetails = SemanticChecker.addColumnDetails(selectListColumnDetails, getColumnDetails(keyColumns));
144
145       int[] selectKeyMapping = groupColumnsAccToIterators(selectListColumnDetails, true); // just to initialize Group Mapping to give value of Key Columns + select Columns
146
if (!isView) {
147          columnCharacteristics = new SelectColumnCharacteristics(selectColumnDetails, tableDetailsCC, false);
148          rowReader = getRowReaderScrollable(selectColumnDetails, selectKeyMapping);
149       }
150       state = 2;
151       underlyingReferencesSolvableByCurrentIterator = underlyingReferencesSolvableByCurrentIterator0;
152       iteratorType = IteratorConstants.SCROLLABLE;
153    }
154
155    private _RowReader getRowReaderScrollable(ColumnDetails[] selectColumnDetails, int[] selectKeyMapping) throws DException {
156       int[] selectRowMapping = new int[selectColumnDetails.length];
157       int[] keyRowMapping = new int[keyColumns.length];
158       int countSelect = 0, countKey = 0, count = 0;
159       for (int i = 0, length = groupMapping.length; i < length; i++) {
160          ColumnDetails[] columns = (ColumnDetails[]) groupMapping[i][1];
161          for (int j = 0, length1 = columns.length; j < length1; j++) {
162             if (ifMatch(selectColumnDetails, columns[j])) {
163                selectRowMapping[countSelect++] = selectKeyMapping[count++];
164             } else {
165                keyRowMapping[countKey++] = selectKeyMapping[count++];
166             }
167          }
168       }
169       return new RowReaderScrollable(selectRowMapping, keyRowMapping,
170                                      getOrderSpecification(keyColumns));
171    }
172
173    private boolean ifMatch(ColumnDetails[] columns, ColumnDetails column) throws DException {
174       for (int i = 0, length = columns.length; i < length; i++) {
175          if (columns[i] == column) {
176             return true;
177          }
178       }
179       return false;
180    }
181
182    private boolean[] getOrderSpecification(_KeyColumnInformation[] keyColumnInformation) throws DException {
183       int length = keyColumnInformation.length;
184       boolean[] result = new boolean[length];
185       for (int i = 0; i < length; i++) {
186          result[i] = keyColumnInformation[i].getOrderSpecification();
187       }
188       return result;
189    }
190
191    /**
192     * This constructor is initiated in case of UPDATABLE Type. The major works
193     * done are as follows
194     * 1. To maintain the mapping of Selected Columns and Base level Iterator and
195     * also maintain the indices of selected columns in the row.
196     * 2. Initialize the SelectedColumnCharacteristics. This is needed to get all
197     * the information about Selected Columns
198     * 3. Initialize the AllColumnsRowReader. This is needed for reading the row.
199     * @param selectListColumnDetails list of specified columns in select list
200     * @param iterator0 underlying iterator having the actual data
201     * @param selectReferences columns of select whose values are to be provided by user
202     * @param allTableDetails list of all tables involved in the from clause
203     * @param viewTables0 tables involving in view defintion
204     * @param groupByPrimaryColumns
205     * @param isView boolean variable representing the table or view
206     * @param underlyingReferencesSolvableByCurrentIterator0
207     * @param serverSession
208     * @param qs
209     * @throws DException
210     */

211
212    public SelectedColumnIterator(ColumnDetails[] selectListColumnDetails, _Iterator iterator0, _Reference[] selectReferences, TableDetails[] allTableDetails, TableDetails[] viewTables0, String JavaDoc[] groupByPrimaryColumns, boolean isView, _Reference[] underlyingReferencesSolvableByCurrentIterator0, _ServerSession serverSession, queryspecification qs ,_Reference[] ref,_Iterator[] iter) throws DException {
213       this.qs = qs;
214       init(iterator0, selectListColumnDetails, allTableDetails, selectReferences, viewTables0, serverSession , ref,iter);
215       keyColumns = iterator.getKeyColumnInformations();
216       variableValues = getNewVariableValues(variableValues);
217
218       functionalColumns = getFunctionalColumns(selectListColumnDetails, keyColumns);
219       if (!isView) {
220          initializeSelectCCAndRowReader(groupByPrimaryColumns);
221          tableAndIteratorMapping = getTableAndIteratorMapping(allTableDetails);
222       }
223
224       selectedColumnMapping = groupColumnsAccToIterators(selectListColumnDetails, false); // For getSelectedColumnValues()
225
selectListColumnDetails = SemanticChecker.addColumnDetails(selectListColumnDetails, getColumnDetails(keyColumns));
226       groupColumnsAccToIterators(selectListColumnDetails, true); // for giving keyColumns value
227
underlyingReferencesSolvableByCurrentIterator = underlyingReferencesSolvableByCurrentIterator0;
228       iteratorType = IteratorConstants.UPDATABLE;
229       state = 2;
230    }
231
232    private _VariableValues getNewVariableValues(_VariableValues variableValues) throws DException {
233       ArrayList list = new ArrayList(5);
234       for (int i = 0, length = selectColumnDetails.length; i < length; i++) {
235          if (selectColumnDetails[i].getType() == HAS_RECORD) {
236             list.add(new Object JavaDoc[] {selectColumnDetails[i], iterator.getBaseIteratorHasRecord(selectColumnDetails[i])});
237          }
238       }
239       if (list.isEmpty()) {
240          return variableValues;
241       }
242       HasRecordVariableValues has = new HasRecordVariableValues(variableValues, (Object JavaDoc[][]) list.toArray(new Object JavaDoc[list.size()][2]));
243       return has;
244    }
245
246    private void initializeSelectCCAndRowReader(String JavaDoc[] groupByPrimaryColumns) throws DException {
247       functionalColumnIteratorMapping = reArrangeFunctionalColumns(iterator.getFunctionalColumnMapping());
248       Object JavaDoc[][] functionalKeyColumnMapping = getFunctionalKeyColumnMapping();
249       columnCharacteristics = new SelectColumnCharacteristics(selectColumnDetails, tableDetailsCC, groupByPrimaryColumns, false, true);
250       ForeignKeyColumns[] fKeyColumns = getColumnNamesToIndexesMapping(selectColumnDetails);
251       rowReader = new AllColumnRowReader(selectColumnDetails, keyColumns, functionalKeyColumnMapping, tableDetailsCC, allColumnLength, columnCharacteristics, fKeyColumns, false);
252       ( (SelectColumnCharacteristics) columnCharacteristics).setRowReader(rowReader);
253       ( (SelectColumnCharacteristics) columnCharacteristics).setForeignKeyMapping(fKeyColumns);
254    }
255
256    /**
257     * This method rearrange the mapping of View and functional columns according
258     * to the sequence specified in from clause. This method is needed in case of
259     * listener/insert/update/delete to make the whole row including all system
260     * fields.
261     * @param functionalColumns list of functional Columns
262     * @return list of functional columns solvable at view level
263     * @throws DException
264     */

265
266    private Object JavaDoc[][] reArrangeFunctionalColumns(Object JavaDoc[][] functionalColumns) throws DException {
267       if (functionalColumns == null) {
268          return null;
269       }
270       int finalLength = functionalColumns.length;
271       ArrayList list = new ArrayList();
272       for (int i = 0, length = viewTables.length; i < length; i++) {
273          int j = 0;
274          for (; j < finalLength; j++) {
275             if (functionalColumns[j][0] == viewTables[i]) {
276                list.add(functionalColumns[j]);
277                break;
278             }
279          }
280       }
281       if (list.size() != finalLength) {
282          throw new DException("DSE3521", null);
283       }
284       return (Object JavaDoc[][]) list.toArray(new Object JavaDoc[list.size()][3]);
285    }
286
287    private ColumnDetails[] getColumnDetails(_KeyColumnInformation[] keyColumns) throws DException {
288       int length = keyColumns.length;
289       ColumnDetails[] columns = new ColumnDetails[length];
290       for (int i = 0; i < length; i++) {
291          columns[i] = keyColumns[i].getColumnDetails();
292       }
293       return columns;
294    }
295
296    /**
297     * NOT USED
298     */

299    public SelectedColumnIterator(_KeyColumnInformation[] orderKeyColumns0, _Iterator iterator0, TableDetails[] allTableDetails, _ServerSession serverSession, queryspecification qs ,_Reference[] ref,_Iterator[] iter) throws DException {
300       this.qs = qs;
301       init(iterator0, null, allTableDetails, null, null, serverSession , ref, iter);
302       keyColumns = addKeyColumns(orderKeyColumns0, iterator.getKeyColumnInformations());
303
304       functionalColumns = getFunctionalColumns(null, keyColumns);
305       Object JavaDoc[][] functionalKeyColumnMapping = getFunctionalKeyColumnMapping();
306       tableAndIteratorMapping = getTableAndIteratorMapping(allTableDetails);
307
308       columnCharacteristics = new ColCharacteristicsTempIndex(tableDetailsCC, false, 0, true);
309       rowReader = new AllColumnRowReaderTempIndex(keyColumns, functionalKeyColumnMapping, tableDetailsCC, allColumnLength, columnCharacteristics, false);
310       ( (ColCharacteristicsTempIndex) columnCharacteristics).setRowReader( (_AllColumnRowReader) rowReader);
311       iteratorType = IteratorConstants.UPDATABLE;
312       state = 2;
313    }
314
315    /**
316     * NOT USED
317     */

318    private _KeyColumnInformation[] addKeyColumns(_KeyColumnInformation[] orderKeyColumns0, _KeyColumnInformation[] keyColumnInformations) throws DException {
319       int length = orderKeyColumns0.length, length1 = keyColumnInformations.length, totalLength = length + length1;
320       _KeyColumnInformation[] keyColumns = new _KeyColumnInformation[totalLength];
321       System.arraycopy(orderKeyColumns0, 0, keyColumns, 0, length);
322       System.arraycopy(keyColumnInformations, 0, keyColumns, length, length1);
323       return keyColumns;
324    }
325
326    /**
327     * This method is used to perform initialization and to create
328     * the mapping of view columns vs. main table columns.
329     * @param iterator0 underlying main table or view iterator
330     * @param selectListColumnDetails select column list of main table
331     * @param allTableDetails tables involved in query
332     * @param selectReferences columns whose values to be provided by user
333     * @param viewTables0 list of tables involved in view
334     * @param serverSession
335     * @throws DException
336     */

337    private void init(_Iterator iterator0, ColumnDetails[] selectListColumnDetails, TableDetails[] allTableDetails, _Reference[] selectReferences, TableDetails[] viewTables0, _ServerSession serverSession ,_Reference[] ref, _Iterator[] iter) throws DException {
338       iterator = iterator0;
339       viewTables = viewTables0;
340       selectColumnDetails = selectListColumnDetails;
341       tableDetailsCC = getMappingOfTableCC(allTableDetails);
342       iterator.getTableDetails();
343
344      variableValues = GeneralPurposeStaticClass.getVariableValues123(selectReferences, serverSession,ref,iter);
345
346       variableValues.setIterator(iterator); // for Inner Query References
347
/* -----------------FOR SCALARSUBQUERY IN SELCTLIST-------------*/
348
349
350       /*For Support of scalar subquery in selectList
351  _Reference[] ref = GeneralPurposeStaticClass.getAllReferences(selectReferences);
352      ref= GeneralPurposeStaticClass.getUnderLyingReferencesOnly(ref,allTableDetails);
353      if( ref != null ){
354        Object[] values = new Object[ref.length];
355        Arrays.fill(values,iterator);
356        ((VariableValues)variableValues).addColumnValues(ref,values,0);
357      }
358  */

359
360    }
361
362    /**
363     * This method returns the mapping of TableDetails - ColumnCharacteristics -
364     * the starting index of table in the row, which is one based.
365     * @param allTableDetails
366     * @return
367     * @throws DException
368     */

369
370    private Object JavaDoc[][] getMappingOfTableCC(TableDetails[] allTableDetails) throws DException {
371       int length = allTableDetails.length;
372       Object JavaDoc[][] newMapping = new Object JavaDoc[length][3];
373       int index = 1;
374       for (int i = 0; i < length; ++i) {
375          newMapping[i][0] = allTableDetails[i];
376          _ColumnCharacteristics cc = allTableDetails[i].cc;
377          newMapping[i][1] = cc;
378          newMapping[i][2] = new Integer JavaDoc(index);
379          index = index + cc.getColumnCount();
380       }
381       allColumnLength = index;
382       return newMapping;
383    }
384
385    private Object JavaDoc[][] getTableAndIteratorMapping(TableDetails[] allTableDetails) throws DException {
386       ArrayList list = new ArrayList();
387       for (int i = 0, lengthOfTables = allTableDetails.length; i < lengthOfTables; i++) {
388          Object JavaDoc[] array = new Object JavaDoc[3];
389          array[0] = allTableDetails[i];
390          array[1] = getColumnDetails(allTableDetails[i]);
391          array[2] = iterator.getBaseIterator( ( (ColumnDetails[]) array[1])[0]);
392          list.add(array);
393       }
394       if (functionalColumnIteratorMapping != null) {
395          list.addAll(Arrays.asList(functionalColumnIteratorMapping));
396       }
397       if (functionalColumns != null) {
398          Object JavaDoc[] array = new Object JavaDoc[3];
399          array[0] = null;
400          array[1] = functionalColumns;
401          array[2] = this;
402          list.add(array);
403       }
404       return list.isEmpty() ? null : (Object JavaDoc[][]) list.toArray(new Object JavaDoc[list.size()][3]);
405    }
406
407    private ColumnDetails[] getColumnDetails(TableDetails tableDetails) throws DException {
408       _ColumnCharacteristics cc = tableDetails.cc;
409       String JavaDoc[] columnNames = cc.getColumnNames();
410       int length1 = columnNames.length;
411       ColumnDetails[] columns = new ColumnDetails[length1];
412       for (int j = 0; j < length1; ++j) {
413          ColumnDetails columnDetail = new ColumnDetails();
414          String JavaDoc[] tableName = tableDetails.getTableName();
415          String JavaDoc[] column = new String JavaDoc[4];
416          System.arraycopy(tableName, 0, column, 0, tableName.length);
417          column[3] = columnNames[j];
418          columnDetail.setColumnName(column);
419          columnDetail.setTableDetails(tableDetails);
420          columnDetail.setTableForDisplay(tableDetails);
421          int index = cc.getColumnIndex(columnNames[j]);
422          columnDetail.setDatatype(cc.getColumnType(index));
423          columnDetail.setSize(cc.getSize(index));
424          columnDetail.setType(REFERENCE);
425          columnDetail.setExpression(column[2] + "." + column[3]);
426          columns[j] = columnDetail;
427       }
428       return columns;
429    }
430
431    private Object JavaDoc[][] getFunctionalKeyColumnMapping() throws DException {
432       ArrayList list = new ArrayList();
433       if (functionalColumnIteratorMapping != null) {
434          int length = functionalColumnIteratorMapping.length;
435          for (int i = 0; i < length; i++) {
436             ColumnDetails[] columns = (ColumnDetails[]) functionalColumnIteratorMapping[i][1];
437             for (int j = 0, length1 = columns.length; j < length1; j++) {
438                list.add(new Object JavaDoc[] {columns[j], new Integer JavaDoc(allColumnLength++)});
439             }
440          }
441       }
442       if (functionalColumns != null) {
443          for (int i = 0, length = functionalColumns.length; i < length; i++) {
444             list.add(new Object JavaDoc[] {functionalColumns[i], new Integer JavaDoc(allColumnLength++)});
445          }
446       }
447       --allColumnLength;
448       return (Object JavaDoc[][]) list.toArray(new Object JavaDoc[list.size()][2]);
449    }
450
451    protected ColumnDetails[] getFunctionalColumns(ColumnDetails[] selectedColumns, _KeyColumnInformation[] keyColumns) throws DException {
452       ArrayList list = new ArrayList();
453       for (int i = 0, length = selectedColumns.length; i < length; i++) {
454          TableDetails tableDetail = selectedColumns[i].getTableDetails();
455          if (tableDetail == null || selectedColumns[i].getTableAliasArray() != null) {
456             list.add(selectedColumns[i]);
457          }
458       }
459       for (int i = 0, length = keyColumns.length; i < length; i++) {
460          ColumnDetails column = keyColumns[i].getColumnDetails();
461          TableDetails tableDetail = column.getTableDetails();
462          if (tableDetail == null || column.getTableAliasArray() != null) {
463             list.add(column);
464          }
465       }
466       return list.isEmpty() ? null : (ColumnDetails[]) list.toArray(new ColumnDetails[list.size()]);
467    }
468
469    /**
470     * This method is required to create a mapping of base level iterators and
471     * corresponding columns involved in given query.
472     * @param columnDetails all the columns involved in select query
473     * @param makeGroupMapping
474     * @return
475     * @throws DException
476     */

477    private int[] groupColumnsAccToIterators(ColumnDetails[] columnDetails, boolean makeGroupMapping) throws DException {
478       /*dst*/
479       int length = columnDetails.length;
480          ArrayList aList = new ArrayList(length);
481          for (int i = 0; i < length; i++) {
482             aList.add(new TableAndIndex(columnDetails[i].getTableDetails(), i + 1));
483          }
484          TableDetails[] tableDetails = new TableDetails[length];
485          int[] index = new int[length];
486          TableAndIndex tableIndex = (TableAndIndex) aList.remove(0);
487          tableDetails[0] = tableIndex.getTableDetails();
488          index[0] = tableIndex.getIndex();
489          for (int i = 1; i < length; i++) {
490             for (int j = 0; j < aList.size(); j++) {
491                tableIndex = (TableAndIndex) aList.get(j);
492                if (tableDetails[i - 1] == tableIndex.getTableDetails()) {
493                   aList.remove(j);
494                   tableDetails[i] = tableIndex.getTableDetails();
495                   index[i] = tableIndex.getIndex();
496                   i++;
497                   j--;
498                }
499             }
500             if (aList.size() != 0) {
501                tableIndex = (TableAndIndex) aList.remove(0);
502                tableDetails[i] = tableIndex.getTableDetails();
503                index[i] = tableIndex.getIndex();
504             } else {
505                break;
506             }
507          }
508          if (makeGroupMapping) {
509             initializeMapping(tableDetails, index, columnDetails);
510          }
511          int[] finalIndex = new int[length];
512          for (int i = 0; i < length; i++) {
513             finalIndex[i] = getPositionFromArray(index, i + 1);
514          }
515          return finalIndex;
516          /*dend*/
517    }
518
519    private int getPositionFromArray(int[] index, int toSearch) throws DException {
520       for (int i = 0, len = index.length; i < len; i++) {
521          if (index[i] == toSearch) {
522             return i + 1;
523          }
524       }
525       throw new DException("DSE3564", null);
526    }
527
528    /**
529     * This method is used to create mapping of single table or base table iterator
530     * and columns and table name
531     * @param tableDetails table for which grouping is to be done
532     * @param indexes column indexes of all the columns corresponding to tables
533     * @param selectColumnDetails list of selected columns
534     * @throws DException
535     */

536
537    public void initializeMapping(TableDetails[] tableDetails, int[] indexes, ColumnDetails[] selectColumnDetails) throws DException {
538      ArrayList mapping = new ArrayList();
539       int length = tableDetails.length;
540       for (int i = 0; i < length; i++) {
541          ArrayList aList = new ArrayList(length);
542          aList.add(selectColumnDetails[indexes[i] - 1]);
543          for (int j = i + 1; j < length && tableDetails[i] != null; j++) {
544             if (tableDetails[i] == tableDetails[j]) {
545                aList.add(selectColumnDetails[indexes[j] - 1]);
546                i = j;
547             } else { // because tableDetails are grouped
548
break;
549             }
550          }
551          _Iterator baseLevelIterator = null;
552          if (tableDetails[i] == null || selectColumnDetails[indexes[i] - 1].getType() != REFERENCE) {
553             variableValues.setIterator(iterator);
554          } else {
555             baseLevelIterator = iterator.getBaseIterator(selectColumnDetails[indexes[i] - 1]);
556          }
557          /*Done by vibha acc to findbug*/
558            mapping.add(new Object JavaDoc[] {baseLevelIterator, (ColumnDetails[]) aList.toArray(new ColumnDetails[0]), Utility.getBooleanValue(tableDetails[i] != null), tableDetails[i]});
559       }
560       groupMapping = (Object JavaDoc[][]) mapping.toArray(new Object JavaDoc[0][]);
561    }
562
563    /**
564     * This method return the row according to different type. It can be
565     * 1. Scrollable
566     * 2. NonScrollable
567     * In above two case row is made with the help of the mapping of
568     * Selected columns and Base Level Iterator
569     * 3. Updatable
570     * In this case row is made with the help of mapping of TableDetails and
571     * ite Iterator.
572     * @return values of all the columns of row
573     * @throws com.daffodilwoods.database.resource.DException
574     */

575
576    public Object JavaDoc getColumnValues() throws com.daffodilwoods.database.resource.DException {
577       switch (iteratorType) {
578          case IteratorConstants.NONSCROLLABLE:
579             return getNonSharableFieldSelColumnValues();
580          case IteratorConstants.SCROLLABLE:
581             return getNonSharableFieldRow();
582          case IteratorConstants.UPDATABLE:
583             return getNonSharableAllCOlumnValues();
584       }
585       throw new DException("DSE3522", new Object JavaDoc[] {new Integer JavaDoc(iteratorType)});
586    }
587
588    private Object JavaDoc[] getNonSharableFieldSelColumnValues() throws DException {
589       Object JavaDoc[] row = getNonSharableFieldRow();
590       int length = selectColumnDetails.length;
591       Object JavaDoc[] result = new Object JavaDoc[length];
592       for (int i = 0; i < length; i++) {
593          result[i] = getFieldObject(i + 1, row);
594       }
595       return result;
596    }
597
598    private Object JavaDoc[] getNonSharableFieldSelColumnValues(ArrayList alist) throws DException {
599         getNonSharableFieldRow(alist);
600          int length = selectColumnDetails.length;
601          Object JavaDoc[] result = new Object JavaDoc[length];
602          for (int i = 0; i < length; i++) {
603           result[i] = alist.get(selectedColumnMapping[i] - 1);
604         }
605        return result;
606       }
607
608    private Object JavaDoc getFieldObject(int index, Object JavaDoc[] row) throws DException {
609       try {
610          return row[selectedColumnMapping[index - 1] - 1];
611       } catch (ArrayIndexOutOfBoundsException JavaDoc ex) {
612          throw ex;
613       }
614    }
615
616    private Object JavaDoc[] getNonSharableAllCOlumnValues() throws DException {
617       try {
618          ArrayList list = new ArrayList();
619          for (int i = 0, length = tableAndIteratorMapping.length; i < length; i++) {
620             Object JavaDoc[] values = (Object JavaDoc[]) ( (_Iterator) tableAndIteratorMapping[i][2]).getColumnValues( (_Reference[]) tableAndIteratorMapping[i][1]);
621             list.addAll(Arrays.asList(values));
622          }
623          return (Object JavaDoc[]) list.toArray(new Object JavaDoc[list.size()]);
624       } catch (NullPointerException JavaDoc ex) {
625          throw new DException("DSE3523", null);
626       }
627    }
628
629    public Object JavaDoc getColumnValues(_Reference[] parm1) throws com.daffodilwoods.database.resource.DException {
630       int length = parm1.length;
631       Object JavaDoc[] result = new Object JavaDoc[length];
632       for (int i = 0; i < length; i++) {
633          result[i] = getColumnValues(parm1[i]);
634       }
635       return result;
636    }
637
638    public Object JavaDoc getColumnValues(ArrayList alist) throws com.daffodilwoods.database.resource.DException {
639       switch (iteratorType) {
640          case IteratorConstants.NONSCROLLABLE:
641             return getNonSharableFieldSelColumnValues(alist);
642          case IteratorConstants.SCROLLABLE:
643             return getNonSharableFieldRow(alist);
644          case IteratorConstants.UPDATABLE:
645             return getNonSharableAllCOlumnValues(alist);
646       }
647       throw new DException("DSE3522", new Object JavaDoc[] {new Integer JavaDoc(iteratorType)});
648    }
649
650    public Object JavaDoc getColumnValues(_Reference references) throws DException {
651       _Iterator iter = getIterator(references);
652       return iter != null ? iter.getColumnValues(references) : ( (ColumnDetails) references).run(variableValues);
653    }
654
655    public Object JavaDoc getObject() throws DException {
656       switch (iteratorType) {
657          case IteratorConstants.NONSCROLLABLE:
658             return getSelColumnValues();
659          case IteratorConstants.SCROLLABLE:
660             return getValues(getRow());
661          case IteratorConstants.UPDATABLE:
662             return getAllColumnValues();
663       }
664       throw new DException("DSE3522", new Object JavaDoc[] {new Integer JavaDoc(iteratorType)});
665    }
666
667    private Object JavaDoc getSelColumnValues() throws DException {
668       Object JavaDoc[] row = getRow();
669       int length = selectColumnDetails.length;
670       Object JavaDoc[] result = new Object JavaDoc[length];
671       for (int i = 0; i < length; i++) {
672          try {
673             result[i] = ( (FieldBase) getObject(i + 1, row)).getObject();
674          } catch (NullPointerException JavaDoc ex) {
675             throw ex;
676          }
677       }
678       return result;
679    }
680
681    private Object JavaDoc getObject(int index, Object JavaDoc row) throws DException {
682       try {
683          return ( (Object JavaDoc[]) row)[selectedColumnMapping[index - 1] - 1];
684       } catch (ArrayIndexOutOfBoundsException JavaDoc ex) {
685          for (int i = 0; i < groupMapping.length; i++) {
686          ;//// Removed By Program ** System.out.println("[0] " + groupMapping[i][0].getClass() + " [2] " + groupMapping[i][2] + " COL REFERE[1] " + P.print(groupMapping[i][1]));
687
}
688          throw ex;
689       }
690    }
691
692    private Object JavaDoc[] getValues(Object JavaDoc[] values) throws DException {
693       Object JavaDoc[] newValues = new Object JavaDoc[values.length];
694       for (int i = 0; i < values.length; i++) {
695          newValues[i] = ( (FieldBase) values[i]).getObject();
696       }
697       return newValues;
698    }
699
700    private Object JavaDoc getAllColumnValues() throws DException {
701       try {
702          ArrayList list = new ArrayList();
703          for (int i = 0, length = tableAndIteratorMapping.length; i < length; i++) {
704             Object JavaDoc[] values = (Object JavaDoc[]) ( (_Iterator) tableAndIteratorMapping[i][2]).getColumnValues( (_Reference[]) tableAndIteratorMapping[i][1]);
705             list.addAll(Arrays.asList(getValues(values)));
706          }
707          return list.toArray(new Object JavaDoc[list.size()]);
708       } catch (NullPointerException JavaDoc ex) {
709          throw new DException("DSE3523", null);
710       }
711    }
712
713    private FieldBase[] getFieldAllColumnValues() throws DException {
714       try {
715          ArrayList list = new ArrayList();
716          for (int i = 0, length = tableAndIteratorMapping.length; i < length; i++) {
717             FieldBase[] values = ( (_Iterator) tableAndIteratorMapping[i][2]).fields( (_Reference[]) tableAndIteratorMapping[i][1]);
718             list.addAll(Arrays.asList(values));
719          }
720          return (FieldBase[]) list.toArray(new FieldBase[list.size()]);
721       } catch (NullPointerException JavaDoc ex) {
722          throw new DException("DSE3523", null);
723       }
724    }
725
726    private FieldBase getFieldObject(int index, FieldBase[] row) throws DException {
727       try {
728          return row[selectedColumnMapping[index - 1] - 1];
729       } catch (ArrayIndexOutOfBoundsException JavaDoc ex) {
730          throw ex;
731       }
732    }
733
734    public FieldBase field(_Reference references) throws com.daffodilwoods.database.resource.DException {
735       _Iterator iter = getIterator(references);
736       return iter != null ? iter.field(references) : (FieldBase) ( (ColumnDetails) references).run(variableValues);
737    }
738
739    public FieldBase[] fields(_Reference[] parm1) throws com.daffodilwoods.database.resource.DException {
740       int length = parm1.length;
741       FieldBase[] result = new FieldBase[length];
742       for (int i = 0; i < length; i++) {
743          result[i] = field(parm1[i]);
744       }
745       return result;
746    }
747
748    public _ExecutionPlan getExecutionPlan() throws DException {
749       return new ExecutionPlan("SelectedColumnIterator", null, null, null, null);
750    }
751
752    public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException {
753       int length = 1;
754       ExecutionPlanForBrowser cplans[] = new ExecutionPlanForBrowser[length];
755       cplans[0] = iterator.getExecutionPlanForBrowser();
756       return new ExecutionPlanForBrowser("Select", "SELECT Column Iterator", cplans, null, null, null);
757    }
758
759    public Object JavaDoc[] getFunctionalColumnIteratorMapping() throws DException {
760       return functionalColumns != null ? new Object JavaDoc[] {functionalColumns, this}
761            : null;
762    }
763
764    public Object JavaDoc getSelectColumnValues() throws DException {
765       Object JavaDoc row = getNonSharableFieldSelColumnValues();
766       return row;
767    }
768
769    public void flushInsertedRecord() throws DException {
770    }
771
772    public Object JavaDoc getColumnValueForReference(_Reference reference) throws DException {
773      _Iterator iter = getIterator(reference);
774            /* Done by vibha to solve bug no 12524 */
775       return iter != null ? getIterator(reference).getColumnValues(reference) :
776           ( (ColumnDetails) reference).run(variableValues);
777
778  }
779
780    public FieldBase getFieldValueForReference(_Reference reference) throws DException {
781       return getIterator(reference).field(reference);
782    }
783
784    protected _Iterator getIterator(_Reference reference) throws DException {
785       ColumnDetails column = (ColumnDetails) reference;
786       return getIteratorForGetIterator(column);
787    }
788
789    /**
790     * This method is required to know whether select query supports updatable
791     * type of resultset. Select query of only single table without groupby and
792     * functional columns is candidate of updatable type.
793     * @return is view query updatable
794     * @throws DException
795     */

796    public boolean isUpdatable() throws DException {
797      return false;
798    }
799
800    /**
801     * This method is used to get the data types of columns specified in given
802     * select query.
803     * @return data types of columns specified in given select query.
804     * @throws DException
805     */

806    public int[] getDataTypes() throws DException {
807       int length = selectColumnDetails.length;
808       int[] dataTypes = new int[length];
809       for (int i = 0; i < length; i++) {
810          dataTypes[i] = selectColumnDetails[i].getDatatype();
811       }
812       return dataTypes;
813    }
814
815    public Object JavaDoc getKey() throws com.daffodilwoods.database.resource.DException {
816       return iterator.getKey();
817    }
818
819     public byte[] getByteKey() throws DException {
820       return iterator.getByteKey();
821     }
822
823     public void moveByteKey(byte[] key) throws DException {
824       iterator.moveByteKey(key);
825     }
826
827    public Object JavaDoc getColumnValues(int[] parm1) throws com.daffodilwoods.database.resource.DException {
828       return iterator.getColumnValues(parm1);
829    }
830
831    public _Order getDefaultOrder() throws DException {
832       return iterator.getDefaultOrder();
833    }
834
835    public Object JavaDoc[][] getFunctionalColumnMapping() throws DException {
836       return iterator.getFunctionalColumnMapping();
837    }
838
839    public Object JavaDoc moveToInsertRow() throws DException {
840       throw new UnsupportedOperationException JavaDoc();
841    }
842
843    public _QueryPlan getQueryPlan() throws DException {
844       throw new UnsupportedOperationException JavaDoc();
845    }
846
847    public FieldBase[] fields(int[] columns) throws com.daffodilwoods.database.resource.DException {
848       throw new java.lang.UnsupportedOperationException JavaDoc("Method fields() not yet implemented.");
849    }
850
851    public Object JavaDoc insert(int[] columns, Object JavaDoc[] values) throws DException {
852       throw new UnsupportedOperationException JavaDoc("Method does not return value");
853    }
854
855    public void delete(Object JavaDoc key) throws DException {
856       throw new UnsupportedOperationException JavaDoc("Method does not return value");
857    }
858
859    public Object JavaDoc update(Object JavaDoc key, int[] columns, Object JavaDoc[] values, Object JavaDoc row) throws DException {
860       throw new UnsupportedOperationException JavaDoc("Method does not return value");
861    }
862
863    /* NOT USED */
864    private void initializeRowReader(_ColumnCharacteristics columnCharacteristics, ForeignKeyColumns[] foreignColumns) throws DException {
865       rowReader = new SelectRowReader(columnCharacteristics, foreignColumns);
866    }
867
868
869
870    public String JavaDoc toString() {
871       return "SelectedColumnIterator[" + iterator + "]";
872    }
873
874
875    private _Iterator getIteratorForGetIterator(ColumnDetails column) throws DException {
876       if (column.getTable() != null) {
877          for (int i = 0, length = groupMapping.length; i < length; i++) {
878             TableDetails mappingTable = (TableDetails) groupMapping[i][3];
879             if (mappingTable == column.getTable()) {
880                return (_Iterator) groupMapping[i][0];
881             }
882          }
883          return iterator;
884       }
885
886       for (int i = 0, length = groupMapping.length; i < length; i++) {
887          ColumnDetails mappingColumn = ( (ColumnDetails[]) groupMapping[i][1])[0];
888          if (column.getAppropriateColumn().equalsIgnoreCase(mappingColumn.getAppropriateColumn())) {
889            return (_Iterator) groupMapping[i][0];
890          }
891       }
892       return iterator;
893
894    }
895  private Object JavaDoc[] getNonSharableAllCOlumnValues(ArrayList alist) throws DException {
896             try {
897                for (int i = 0, length = tableAndIteratorMapping.length; i < length; i++) {
898                 Object JavaDoc[] values = (Object JavaDoc[]) ( (_Iterator) tableAndIteratorMapping[i][2]).getColumnValues( (_Reference[]) tableAndIteratorMapping[i][1]);
899                 alist.addAll(Arrays.asList(values));
900                }
901          return (Object JavaDoc[]) alist.toArray(new Object JavaDoc[alist.size()]);
902             } catch (NullPointerException JavaDoc ex) {
903                throw new DException("DSE3523", null);
904             }
905          }
906
907
908
909
910 }
911
Popular Tags