KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table;
2
3 import com.daffodilwoods.daffodildb.client.*;
4 import com.daffodilwoods.daffodildb.server.datasystem.utility._Record;
5 import com.daffodilwoods.daffodildb.server.sql99.common.*;
6 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*;
7 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
8 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
9 import com.daffodilwoods.database.resource.*;
10 import com.daffodilwoods.database.sqlinitiator.*;
11 import com.daffodilwoods.database.utility.P;
12 import com.daffodilwoods.daffodildb.utils.field.FieldLong;
13 import com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs;
14 import com.daffodilwoods.daffodildb.utils.BufferRange;
15
16 /**
17  *
18  * <p>Title: CounterIterator </p>
19      * <p>Description: This Class is responsible to enhance the ROWNUM functionality.
20  * The ROWNUM is used to add a column in the table that maintains the number of
21  * the current row in the complete result set of the underlying iterator. </p>
22  * <p>Copyright: Copyright (c) 2004</p>
23  * <p>Company: </p>
24  * @author not attributable
25  * @version 1.0
26  *
27  * @updated Manoj Kr.
28  * @date Nov. 29 , 2004
29  * @reason To maintain state of iterator
30  * @version 1.1
31  */

32 public class CounterIterator
33     extends BaseSingleIterator {
34
35   /**
36    * Underlying iterator on which ROWNUM is to be applied.
37    */

38   _Iterator baseIterator;
39   /**
40    * Column reference corresponding to ROWNUM column.
41    */

42   _Reference[] references;
43   /**
44    * Variable representing the current row number .
45    */

46   long counter;
47
48   /**
49     * Variable representing current state of iterator.
50    */

51
52    private int state ;
53
54
55
56   public CounterIterator(_Iterator baseIterator0, _Reference[] references0) {
57     super(baseIterator0);
58     baseIterator = baseIterator0;
59     references = references0;
60     state = INVALIDSTATE;
61
62   }
63
64   /**
65    * This method is used to retrieve the first record from the iterator.
66    * If underlying iterator has record, it set the row count to 1, 0 otherwise.
67    * @return true if reord found, false otherwise.
68    * @throws DException
69    */

70   public boolean first() throws DException {
71     boolean flag = baseIterator.first();
72     state = flag ? VALIDSTATE : AFTERLAST;
73     counter = flag ? 1 : 0;
74     return flag;
75   }
76
77   /**
78    * This method is used to retrieve the last record from the iterator.
79    * It navigates the underlying iterator in the forward direction to get the
80    * total number of rows in the iterator.
81    * If underlying iterator has record, row count is 0 otherwise rowcount is
82    * equal to total number of records in the underlying iterator.
83    *
84    * While navigating the base iterator, it's state is AFTERLAST after the last
85    * row, hence, previous of base iterator is called to align the underlying
86    * iterator.
87    * @return true if reord found, false otherwise.
88    * @throws DException
89    */

90   public boolean last() throws DException {
91     boolean flag = baseIterator.first();
92     counter = 0;
93     if (flag) {
94       do {
95         counter++;
96       }
97       while (flag = baseIterator.next());
98       baseIterator.previous();
99     }
100     state = flag ? VALIDSTATE : AFTERLAST ;
101
102     return counter > 0;
103   }
104
105   /**
106    * This method is used to retrieve the next record from the iterator and to
107    * update the rowcount (increments the rowcount by 1).
108    * @return true if record found, false otherwise.
109    * @throws DException
110    */

111   public boolean next() throws DException {
112     switch (state) {
113        case INVALIDSTATE:
114          throw new DException("DSE4116", null);
115        case BEFOREFIRST:
116          return first();
117        case AFTERLAST:
118          return false;
119      }
120
121     boolean flag = baseIterator.next();
122     state = flag ? VALIDSTATE : AFTERLAST;
123     counter = flag ? counter + 1 : counter;
124     return flag;
125   }
126
127
128   /**
129        * This method is used to retrieve the previous record from the iterator and to
130    * update the rowcount (decrements the rowcount by 1).
131    * @return true if record found, false otherwise.
132    * @throws DException
133    */

134   public boolean previous() throws DException {
135     boolean flag ;
136     switch (state) {
137      case INVALIDSTATE:
138        throw new DException("DSE4117", null);
139      case BEFOREFIRST:
140        return false;
141      case AFTERLAST :
142        flag = baseIterator.previous();
143        state = flag ? VALIDSTATE : BEFOREFIRST ; // IS NODATA
144
return flag;
145    }
146
147     flag = baseIterator.previous();
148
149     state = flag ? VALIDSTATE : BEFOREFIRST ;
150
151     counter = flag ? counter - 1 : counter;
152     return flag;
153   }
154
155
156   /**
157    * This method is used to move the iterator at the passed key.
158    * It navigates the underlying iterator from the first record till the
159    * key matches from the passed key. If key is not found, underlying iterator
160    * is moved to the passed key.
161    * @param key key where to move the iterator
162    * @throws DException
163    */

164   public void move(Object JavaDoc key) throws DException {
165
166     Object JavaDoc oldKey = baseIterator.getKey();
167     int tempCounter = 0;
168     boolean match = false;
169     if (baseIterator.first()) {
170       do {
171         if (baseIterator.getKey().equals(key)) {
172           match = true;
173         }
174         tempCounter++;
175       }
176       while (!match && baseIterator.next());
177     }
178     if (match) {
179       counter = tempCounter;
180     }
181     else {
182       baseIterator.move(oldKey);
183       state = VALIDSTATE;
184     }
185   }
186
187   /**
188    * This method is used to retrieve the values of passed references.
189    * @param refs reference for which values are to be retrived.
190    * Reference may be column or parameter.
191    * @return NonShared FieldBases denoting the value of References. Non Shared
192    * FieldBases are those for which BufferRange is not shared with some other
193    * FieldBase.
194    * @throws DException
195    */

196   public Object JavaDoc getColumnValues(_Reference[] refs) throws DException {
197     if (state != VALIDSTATE)
198     throw new DException("DSE2019", new Object JavaDoc[] {new Integer JavaDoc(state)});
199
200     Object JavaDoc[] values = new Object JavaDoc[references.length];
201     for (int i = 0; i < refs.length; i++) {
202       values[i] = getColumnValues(refs[i]);
203     }
204     return values;
205   }
206
207   /**
208    * This method is used to retrieve the value of passed reference.
209    * It checks passed column, if the column corresponds to ROWNUM column,
210    * value from the maintained rowcount is returned, otherwise, call is
211    * transfered to underlying iterator to retrieve the passed column values.
212    * @param ref reference for which value is to be retrived.
213    * Reference may be column or parameter.
214    * @return NonShared FieldBase denoting the value of Reference. Non Shared
215    * FieldBases are those for which BufferRange is not shared with some other
216    * FieldBase.
217    * @throws DException
218    */

219   public Object JavaDoc getColumnValues(_Reference ref) throws DException {
220     boolean matched = false;
221     if (state != VALIDSTATE)
222          throw new DException("DSE2019", new Object JavaDoc[] {new Integer JavaDoc(state)});
223
224
225     for (int i = 0; i < references.length && !matched; i++) {
226       matched = ref == references[i];
227     }
228     return matched
229         ?
230         new FieldLong(new BufferRange(CCzufDpowfsufs.getBytes(counter)),
231                       Datatypes.LONG) : baseIterator.getColumnValues(ref);
232
233   }
234
235   /**
236    * This method is responsible to display the executionPlan of a Select Query.
237    * @return _ExecutionPlan
238    * @throws DException
239    */

240   public _ExecutionPlan getExecutionPlan() throws DException {
241     _ExecutionPlan plan = baseIterator.getExecutionPlan();
242     _ExecutionPlan cplans[] = plan == null ? null : new _ExecutionPlan[] {
243         plan};
244     return new ExecutionPlan("CounterIterator", cplans, null, null, null);
245   }
246
247   /**
248    * This method is responsible to display the iterators hierarchy of a Select Query.
249    * @return ExecutionPlanForBrowser
250    * @throws DException
251    */

252   public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException {
253     int length = 1;
254     ExecutionPlanForBrowser cplans[] = new ExecutionPlanForBrowser[length];
255     cplans[0] = baseIterator.getExecutionPlanForBrowser();
256     return new ExecutionPlanForBrowser("ROWNUM", "CounterIterator", cplans, null, null, null);
257   }
258
259   /**
260    * The following methods do not have the special fuctioning for adding the
261        * additional ROWNUM column, they transfer the call to the underlying iterator.
262    */

263
264   public Object JavaDoc getKey() throws DException {
265     return baseIterator.getKey();
266   }
267
268   public _OrderCount getOrderCounts() throws DException {
269     return baseIterator.getOrderCounts();
270   }
271
272   public TableDetails[] getTableDetails() throws DException {
273     return baseIterator.getTableDetails();
274   }
275
276   public _Iterator getBaseIterator(ColumnDetails column) throws DException {
277     return baseIterator.getBaseIterator(column);
278   }
279
280   public void setConditionVariableValue(_Reference[] references,
281                                         Object JavaDoc[] values, int priority) throws
282       DException {
283     if (underlyingRef != null) {
284       references = GeneralPurposeStaticClass.getJointReferences(references,
285           underlyingRef);
286       values = GeneralPurposeStaticClass.getJointValues(this, values,
287           underlyingRef.length);
288     }
289     baseIterator.setConditionVariableValue(references, values, priority);
290   }
291
292   public _KeyColumnInformation[] getKeyColumnInformations() throws DException {
293     return baseIterator.getKeyColumnInformations();
294   }
295
296   public void setKeyCount(Object JavaDoc[][] tableAndKeyCount) throws DException {
297     baseIterator.setKeyCount(tableAndKeyCount);
298   }
299
300   public Object JavaDoc[] getUniqueColumnReference() throws DException {
301     return baseIterator.getUniqueColumnReference();
302   }
303
304   public _Order getDefaultOrder() throws DException {
305     return baseIterator.getDefaultOrder();
306   }
307
308   public Object JavaDoc getColumnValues() throws DException {
309 /* if (state != VALIDSTATE)
310       throw new DException("DSE2019", new Object[] {new Integer(state)});
311  */

312     /**@todo Implement this com.daffodilwoods.daffodildb.server.sql99.dql.interfaces._Iterator method*/
313     throw new java.lang.UnsupportedOperationException JavaDoc(
314         "Method getColumnValues() not yet implemented.");
315   }
316
317   public Object JavaDoc getColumnValues(int[] columns) throws DException {
318     /**@todo Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._TableIterator method*/
319     throw new java.lang.UnsupportedOperationException JavaDoc(
320         "Method getColumnValues() not yet implemented.");
321   }
322
323   public _Record getRecord() throws DException {
324     /**@todo Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._TableIterator method*/
325     throw new java.lang.UnsupportedOperationException JavaDoc(
326         "Method getRecord() not yet implemented.");
327   }
328
329   public boolean seek(Object JavaDoc indexKey) throws DException {
330     /**@todo Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._IndexIterator method*/
331     throw new java.lang.UnsupportedOperationException JavaDoc(
332         "Method seek() not yet implemented.");
333   }
334
335   public boolean seekFromTop(_IndexPredicate[] condition) throws DException {
336     /**@todo Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._IndexIterator method*/
337     throw new java.lang.UnsupportedOperationException JavaDoc(
338         "Method seekFromTop() not yet implemented.");
339   }
340
341   public boolean seekFromTopRelative(Object JavaDoc indexKey) throws DException {
342     /**@todo Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._IndexIterator method*/
343     throw new java.lang.UnsupportedOperationException JavaDoc(
344         "Method seekFromTopRelative() not yet implemented.");
345   }
346
347   public boolean seekFromBottom(_IndexPredicate[] condition) throws DException {
348     /**@todo Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._IndexIterator method*/
349     throw new java.lang.UnsupportedOperationException JavaDoc(
350         "Method seekFromBottom() not yet implemented.");
351   }
352
353   public boolean seekFromBottomRelative(Object JavaDoc indexKey) throws DException {
354     /**@todo Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._IndexIterator method*/
355     throw new java.lang.UnsupportedOperationException JavaDoc(
356         "Method seekFromBottomRelative() not yet implemented.");
357   }
358
359   public Object JavaDoc getUpdatedEffect(_Reference[] references, Object JavaDoc[] values) throws
360       DException {
361     throw new UnsupportedOperationException JavaDoc("getUpdatedEffect() Not Supported");
362   }
363
364
365 }
366
Popular Tags