KickJava   Java API By Example, From Geeks To Geeks.

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


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.datasystem.indexsystem.*;
7 import com.daffodilwoods.daffodildb.server.datasystem.utility._Record;
8 import com.daffodilwoods.daffodildb.server.sql99.common.*;
9 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
10 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*;
11 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
12 import com.daffodilwoods.daffodildb.utils.field.*;
13 import com.daffodilwoods.database.general.*;
14 import com.daffodilwoods.database.resource.*;
15 import com.daffodilwoods.database.sqlinitiator.*;
16 import com.daffodilwoods.database.utility.P;
17
18 /**
19  *
20  * <p>Title: ViewIterator </p>
21  * <p>Description: This Class is responsible for retrieving the records from the
22  * query having view name as table reference. The filter, grouping and order
23  * applied on the view is merged with the query specification defined in the
24  * view definition at plan level. Thus, it just transfers the calls for all the
25  * required functioning to underlying iterator. For retrieval methods, it
26  * maintains the mapping of external columns defined at view level with respect
27  * to columns specified in the view defintion. It maintains the boundary between
28  * the query columns and the view columns.</p>
29  * <p>Copyright: Copyright (c) 2004</p>
30  * <p>Company: </p>
31  * @author not attributable
32  * @version 1.0
33  */

34 public class ViewIterator extends BaseSingleIterator {
35
36   /**
37    * Mapping between the query columns and the view query columns.
38    */

39   private Object JavaDoc[][] mappingOfColAndCD;
40   /**
41    * View from which record are retrieved.
42    */

43   private TableDetails tableDetails;
44   /**
45    * Variable representing the key column information of the view
46    */

47   private _KeyColumnInformation[] keyColumnInfo;
48   /**
49    * Order by columns applied on the view
50    */

51   private _Order defaultOrder;
52
53    public ViewIterator(_Iterator iterator0, Object JavaDoc[][] mappingOfColAndCD0, TableDetails tableDetails0) {
54       super(iterator0);
55       mappingOfColAndCD = mappingOfColAndCD0;
56       tableDetails = tableDetails0;
57    }
58
59    /**
60     * This method is used to retrieve the values of passed references.
61     * @param reference reference for which values are to be retrived.
62     * Reference may be column or parameter.
63     * @return NonShared FieldBases denoting the value of References. Non Shared
64     * FieldBases are those for which BufferRange is not shared with some other
65     * FieldBase.
66     * @throws DException
67     */

68    public Object JavaDoc getColumnValues(_Reference[] reference) throws DException {
69       return getUnderLyingReferencesValues(reference);
70    }
71
72    /**
73     * This method is used to retrieve the values of passed references.
74     * @param reference reference for which values are to be retrived.
75     * Reference may be column or parameter.
76     * @return NonShared FieldBases denoting the value of References. Non Shared
77     * FieldBases are those for which BufferRange is not shared with some other
78     * FieldBase.
79     * @throws DException
80     */

81    private Object JavaDoc[] getUnderLyingReferencesValues(_Reference[] reference) throws DException {
82       int length = reference.length;
83       Object JavaDoc[] values = new Object JavaDoc[length];
84       for (int j = 0; j < length; j++) {
85          values[j] = getColumnValues(reference[j]);
86       }
87       return values;
88    }
89
90    /**
91     * This method is used to retrieve the value of passed reference.
92     * It checks the passed column, if the column corresponds to view, values
93     * are retrived from underlying iterator, otherwise, call is transfered to
94     * upper level iterator that has mapping of functional columns with respect
95     * to resultant values.
96     * @param reference reference for which value is to be retrived.
97     * Reference may be column or parameter.
98     * @return NonShared FieldBase denoting the value of Reference. Non Shared
99     * FieldBases are those for which BufferRange is not shared with some other
100     * FieldBase.
101     * @throws DException
102     */

103    public Object JavaDoc getColumnValues(_Reference reference) throws DException {
104       _Reference underLyingReference = getUnderLyingReference(reference);
105       Object JavaDoc obj = underLyingReference == null ? ( (_SelectQueryIterator) iterator).getColumnValueForReference(reference) : iterator.getColumnValues(underLyingReference);
106       return obj;
107    }
108
109    /**
110     * This method is required to get the name of the column corresponding to
111     * table on which view is created.
112     * @param reference name of the column at view level
113     * @return name of the column at table level
114     * @throws DException
115     */

116    private _Reference getUnderLyingReference(_Reference reference) throws DException {
117      if(mappingOfColAndCD !=null)
118      for (int i = 0, length1 = mappingOfColAndCD.length; i < length1; i++) {
119             if ( reference == mappingOfColAndCD[i][0] )
120                 return (_Reference)mappingOfColAndCD[i][1];
121              if( reference.getReferenceType() ==SimpleConstants.COLUMNDETAIL ){
122                 if(((ColumnDetails)reference).isSame((_Reference)mappingOfColAndCD[i][0])) {
123                    return (_Reference) mappingOfColAndCD[i][1];
124                 }
125              }
126         }
127         return null;
128     }
129
130     /**
131      * This method return the shared FieldBase for the passed reference. By Shared
132      * FieldBase we mean, BufferRange of FieldBase is shared with other FieldBase
133      * objects.
134      * @param reference reference for which value is to be retrived
135      * @return shared field base correspondition to passed column reference
136      * @throws DException
137      */

138    public FieldBase field(_Reference reference) throws com.daffodilwoods.database.resource.DException {
139       _Reference underLyingReference = getUnderLyingReference(reference);
140       return underLyingReference == null ? ( (_SelectQueryIterator) iterator).getFieldValueForReference(reference) : iterator.field(underLyingReference);
141    }
142
143    /**
144     * This method return the shared FieldBases for the passed references. By Shared
145     * FieldBase we mean, BufferRange of FieldBase is shared with other FieldBase
146     * objects.
147     * @param reference reference for which values are to be retrived
148     * @return shared field base correspondition to passed column reference
149     * @throws DException
150     */

151    public FieldBase[] fields(_Reference[] reference) throws com.daffodilwoods.database.resource.DException {
152       int length = reference.length;
153       FieldBase[] values = new FieldBase[length];
154       for (int j = 0; j < length; j++) {
155          values[j] = field(reference[j]);
156       }
157       return values;
158    }
159
160    /**
161     * This method is required to get the view name.
162     * @return name of the view
163     * @throws DException
164     */

165    public TableDetails[] getTableDetails() throws DException {
166       return new TableDetails[] {tableDetails};
167    }
168
169    /**
170     * This method is required to get the base level iterator.
171     * If the passed column is function column, this iterator is returned,
172     * otherwise, call is transferred to underlying table iterator.
173     * @param column for which itertor is to be retrieved
174     * @return base iterator for passed column
175     * @throws DException
176     */

177    public _Iterator getBaseIterator(ColumnDetails column) throws DException {
178       if (column.getTableDetails() != null) {
179          return this;
180       }
181       return iterator.getBaseIterator(column);
182    }
183
184    /**
185     * This method is required to get the key column information of view.
186     * The key column of the view will be same as that of the table. Key
187     * columns for view are returned after being clonned.
188     * @return key columns of view
189     * @throws DException
190     */

191    public _KeyColumnInformation[] getKeyColumnInformations() throws DException {
192       if (keyColumnInfo == null) {
193          keyColumnInfo = iterator.getKeyColumnInformations();
194          keyColumnInfo = getClone(keyColumnInfo);
195       }
196       return keyColumnInfo;
197    }
198
199    /**
200     * This method is required to get clonned key column information of the view.
201     * @param keys key column information of main table
202     * @return clonned key column information of view
203     * @throws DException
204     */

205    private _KeyColumnInformation[] getClone(_KeyColumnInformation[] keys) throws DException {
206       int length = keys.length;
207       _KeyColumnInformation[] result = new _KeyColumnInformation[length];
208       for (int i = 0; i < length; i++) {
209          ColumnDetails column = keys[i].getColumnDetails();
210          ColumnDetails cd = null;
211          try {
212             cd = (ColumnDetails) column.clone();
213             cd.setTableDetails(tableDetails);
214             addInMapping(column, cd);
215          } catch (CloneNotSupportedException JavaDoc ex) {
216             throw new DException("DSE0", new Object JavaDoc[] {"Clone Not Supported"});
217          }
218          result[i] = new TableKeyColumnInformation(cd, keys[i].getOrderSpecification());
219       }
220       return result;
221    }
222
223    /**
224     * This method is required to maintain mapping of columns across the view
225     * boundary.
226     * @param cd column of main table
227     * @param clonedCD column of view
228     * @throws DException
229     */

230    private void addInMapping(ColumnDetails cd, ColumnDetails clonedCD) throws DException {
231       if (mappingOfColAndCD == null) {
232          mappingOfColAndCD = new Object JavaDoc[1][2];
233          mappingOfColAndCD[0][0] = clonedCD;
234          mappingOfColAndCD[0][1] = cd;
235          return;
236       }
237       int length = mappingOfColAndCD.length;
238       Object JavaDoc[][] newMapping = new Object JavaDoc[length + 1][2];
239       Object JavaDoc found = null;
240       for (int i = 0; i < length; i++) {
241          ColumnDetails virtualColumn = (ColumnDetails) mappingOfColAndCD[i][0];
242          if (cd.getTable() == virtualColumn.getTable() && cd.getAppropriateColumn().equalsIgnoreCase(virtualColumn.getAppropriateColumn())) {
243             found = mappingOfColAndCD[i][1];
244          }
245          newMapping[i][0] = mappingOfColAndCD[i][0];
246          newMapping[i][1] = mappingOfColAndCD[i][1];
247       }
248       newMapping[length][0] = clonedCD;
249       newMapping[length][1] = found == null ? cd : found;
250       mappingOfColAndCD = newMapping;
251    }
252
253    /**
254     * This method is required to get merged mapping of functional columns
255     * defined on main table and view.
256     * @return merged functional column mapping
257     * @throws DException
258     */

259    public Object JavaDoc[][] getFunctionalColumnMapping() throws DException {
260      Object JavaDoc[][] mapping = iterator.getFunctionalColumnMapping();
261      Object JavaDoc[] funcIterator = ( (_FunctionalMapping) iterator).getFunctionalColumnIteratorMapping();
262      if (mapping == null) {
263         return funcIterator == null ? null : getMappingOfFunctionalColumn(funcIterator);
264      }
265      int length = mapping.length;
266      if (funcIterator != null) {
267         Object JavaDoc[][] newMapping = new Object JavaDoc[length + 1][3];
268         newMapping[0] = getViewEntry(funcIterator);
269         System.arraycopy(mapping, 0, newMapping, 1, length);
270         return newMapping;
271      }
272      return mapping;
273   }
274
275   /**
276    * This method is required to get the mapping of functional columns mapping
277    * having name of view, fucntional column alias name and iterator having the
278    * result of the corresponding fucnctional column.
279    * @param funcIterator iterator having the value of given functional columns
280    * @return functional column mapping of view
281    * @throws DException
282    */

283   private Object JavaDoc[][] getMappingOfFunctionalColumn(Object JavaDoc[] funcIterator) throws DException {
284      Object JavaDoc[][] newMaping = new Object JavaDoc[1][3];
285      newMaping[0] = getViewEntry(funcIterator);
286      return newMaping;
287   }
288
289   /**
290    * This method is required to set the name of the view into the column
291    * mapping maintained. This is done to solve the table level functional
292    * columns at the view level.
293    * @param funcIterator mapping of functional column and iterator
294    * @return mapping merged with view name
295    * @throws DException
296    */

297   private Object JavaDoc[] getViewEntry(Object JavaDoc[] funcIterator) throws DException {
298      Object JavaDoc[] newEntry = new Object JavaDoc[3];
299      newEntry[0] = tableDetails; // View TableDetails
300
newEntry[1] = funcIterator[0]; // functionalCOlumns
301
newEntry[2] = funcIterator[1]; // Iterator
302
return newEntry;
303   }
304
305   /**
306    * This method is required to get the columns involved in the order by clause.
307    * Special handling has been done for rowid column.
308    * @return order by columns
309    * @throws com.daffodilwoods.database.resource.DException
310    */

311   public _Order getDefaultOrder() throws com.daffodilwoods.database.resource.DException {
312       if (defaultOrder == null) {
313          defaultOrder = iterator.getDefaultOrder();
314          defaultOrder= getClonedOrder(defaultOrder);
315       }
316       return defaultOrder;
317    }
318
319    /**
320     * This method is required to make a special provision for ROWID column.
321     * For rowId column, clonned column is returned. For rowId column, name of
322     * the view is set into rowId column of main table to istinguish with that
323     * of the view.
324     * @param cd total columns given in order by clause
325     * @throws DException
326     */

327    private void changeAndAddInMapping(ColumnDetails[] cd) throws DException {
328
329      /*Done by vibha on 9-10-2004 to solve bug no 12333 . check is added for mappingOfColAndCD in null */
330      ArrayList list = new ArrayList();
331      list = mappingOfColAndCD == null ? new ArrayList() :
332          new ArrayList(Arrays.asList(mappingOfColAndCD));
333       try {
334          for (int i = 0; i < cd.length; i++) {
335             if (cd[i].getAppropriateColumn().equalsIgnoreCase(SystemFields.systemFields[SystemFields.rowId])) {
336                ColumnDetails tempCD = (ColumnDetails) cd[i].clone();
337                list.add(new Object JavaDoc[] {cd[i], tempCD});
338             }
339          }
340       } catch (CloneNotSupportedException JavaDoc ex) {
341          throw new DException("", new Object JavaDoc[] {"Clone Not Supported"});
342       }
343       mappingOfColAndCD = (Object JavaDoc[][]) list.toArray(new Object JavaDoc[0][]);
344    }
345
346    /**
347     * This method is responsible to display the executionPlan of a Select Query.
348     * @return _ExecutionPlan
349     * @throws DException
350     */

351    public _ExecutionPlan getExecutionPlan() throws com.daffodilwoods.database.resource.DException {
352        return new ExecutionPlan("ViewIterator",new _ExecutionPlan[]{iterator.getExecutionPlan()},null,null,null);
353    }
354
355    /**
356     * This method is responsible to display the iterators hierarchy of a Select Query.
357     * @return ExecutionPlanForBrowser
358     * @throws DException
359     */

360    public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException {
361        return iterator.getExecutionPlanForBrowser();
362    }
363
364    /**
365     * The following method transfer the call to the underlying iterator.
366     * Since, the where clause condition and order by applied on the view
367     * definition has been already applied at the plan level which merges
368     * query expression defined in view definition and view specification.
369     * Hence, all the following methods just transfer the call to underlying
370     * iterator.
371     */

372
373    public boolean first() throws DException {
374        return iterator.first();
375    }
376
377    public boolean last() throws DException {
378        return iterator.last();
379    }
380
381    public boolean next() throws DException {
382        return iterator.next();
383    }
384
385    public boolean previous() throws DException {
386        return iterator.previous();
387    }
388
389    public Object JavaDoc getKey() throws DException {
390        return iterator.getKey();
391    }
392    public byte[] getByteKey() throws DException {
393        return iterator.getByteKey();
394    }
395
396    public void move(Object JavaDoc key) throws DException {
397        iterator.move(key);
398    }
399    public void moveByteKey(byte[] key) throws DException {
400        iterator.moveByteKey(key);
401    }
402
403    public Object JavaDoc getColumnValues() throws DException {
404       return iterator.getColumnValues();
405    }
406
407    public Object JavaDoc getColumnValues(int[] columns) throws DException {
408        return iterator.getColumnValues(columns);
409    }
410
411    public _Record getRecord() throws DException {
412        return iterator.getRecord();
413    }
414
415    public boolean seek(Object JavaDoc indexKey) throws DException {
416       return iterator.seek(indexKey);
417    }
418
419    public boolean seekFromTop(_IndexPredicate[] condition) throws DException {
420        return iterator.seekFromTop(condition);
421    }
422
423    public boolean seekFromBottom(_IndexPredicate[] condition) throws DException {
424        return iterator.seekFromBottom(condition);
425    }
426
427
428 /*Method written by Sandeep to solve problem in MemoryIndexIterator and NPE in TempIndexDatabase*/
429    private _Order getClonedOrder(_Order defaultOrder) throws DException {
430      ColumnDetails[] cds = defaultOrder.getColumnDetails();
431      ColumnDetails[] clonedCds = new ColumnDetails[cds.length];
432      for (int i = 0; i < cds.length; i++) {
433        try {
434          clonedCds[i] = (ColumnDetails) cds[i].clone();
435          clonedCds[i].setTableDetails(tableDetails);
436          addInMapping(cds[i], clonedCds[i]);
437        }
438        catch (CloneNotSupportedException JavaDoc ex) {
439          throw new DException("DSE0", new Object JavaDoc[] {"Clone Not Supported"});
440        }
441      }
442      return new SelectOrder(clonedCds, defaultOrder.getOrderOfColumns(),defaultOrder.getComparator());
443    }
444   public String JavaDoc toString() {
445        String JavaDoc str = "VIEWITERATOR";
446        return str += "[" + iterator + "]";
447    }
448 }
449
Popular Tags