KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > client > RecordSet


1 package com.daffodilwoods.daffodildb.client;
2
3 import com.daffodilwoods.daffodildb.server.sql99.dql.resultsetmetadata.
4
    _SelectColumnCharacteristics;
5
6 import com.daffodilwoods.daffodildb.server.sql99.dql.resultsetmetadata.
7
    _RowReader;
8
9 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.
10
    _SelectIterator;
11
12 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table.
13
    SelectIterator;
14
15 import java.sql.SQLException JavaDoc;
16 import com.daffodilwoods.database.resource.DException;
17 import com.daffodilwoods.daffodildb.server.datadictionarysystem.
18
    _ColumnCharacteristics;
19
20 import java.util.*;
21 import com.daffodilwoods.daffodildb.server.sql99.common.*;
22
23 public class RecordSet
24     implements _RecordSetBuffer {
25
26   /* the number of records to be fetched in one call*/
27
28   private int fetchSize = 50;
29   /* type of records */
30
31   private _RowReader rowReader;
32   /* used for retriving records */
33
34   _SelectIterator selectIterator;
35
36   private _ColumnCharacteristics columnCharacteristics;
37
38   private _SelectColumnCharacteristics scc;
39
40   private _RecordSetBufferIterator recordSetBufferIterator;
41
42   ArrayList recordList;
43   boolean topFetched; // stores whether the top is already fetched
44
boolean bottomFetched; // stores whether the bottom is already fetched
45

46   private int fetchDirection = 0; // forward -1 backward;
47
private int maxRows = 0; // all rows;
48
private int maxFieldSize = 0; // no data truncation;
49
private boolean lastFetchedDirection; // stores the direction in which fetched the records last time
50
private int noOfRecordMoved;
51   private boolean afterLast;
52
53   boolean isUpdateable;
54   private String JavaDoc query = "";
55
56   public RecordSet() {
57     recordList = new ArrayList(fetchSize);
58   }
59
60   public void setSelectIterator(_SelectIterator selectIterator0) throws
61       DException {
62     this.selectIterator = selectIterator0;
63     rowReader = selectIterator.getRowReader();
64     columnCharacteristics = selectIterator.getColumnCharacteristics();
65     scc = selectIterator.getSelectColumnCharacteristics();
66   }
67
68   public _RecordSetBufferIterator getIterator() throws SQLException JavaDoc {
69     if (recordSetBufferIterator == null) {
70       recordSetBufferIterator = new RecordSetBufferIterator();
71       recordSetBufferIterator.setRecordSetBuffer(this);
72     }
73     return recordSetBufferIterator;
74   }
75
76   public void addDataOperationListener(DataOperationListener listener) {
77     throw new java.lang.UnsupportedOperationException JavaDoc(
78         "Method addDataOperationListener() not yet implemented.");
79   }
80
81   public void removeDataOperationListener(DataOperationListener listener) {
82     throw new java.lang.UnsupportedOperationException JavaDoc(
83         "Method removeDataOperationListener() not yet implemented.");
84   }
85
86   public void loadRecordForKey(_Record record, Object JavaDoc key) throws SQLException JavaDoc {
87     if (key == null)
88       throw new SQLException JavaDoc(new DException("DSE550", null).getMessage());
89     int index = key.hashCode();
90     if (index < 0 && index > recordList.size())
91       throw new SQLException JavaDoc(new DException("DSE552", new Object JavaDoc[] {key}).
92                              getMessage());
93     Object JavaDoc row = recordList.get(index);
94     JDBCRecord drecord = (JDBCRecord) record;
95     drecord.setBuffer(this);
96     drecord.setValues(row);
97     drecord.setKey(key);
98   }
99
100   public void loadRecordForIdentity(_Record record, Object JavaDoc identity) throws
101       SQLException JavaDoc {
102     throw new java.lang.UnsupportedOperationException JavaDoc(
103         "Method loadRecordForIdentity() not yet implemented.");
104   }
105
106   public Object JavaDoc getKeyForIdentity(Object JavaDoc identity) {
107     throw new java.lang.UnsupportedOperationException JavaDoc(
108         "Method getKeyForIdentity() not yet implemented.");
109   }
110
111   public Object JavaDoc getIdentityForKey(Object JavaDoc key) {
112     throw new java.lang.UnsupportedOperationException JavaDoc(
113         "Method getIdentityForKey() not yet implemented.");
114   }
115
116   public String JavaDoc getTable() throws SQLException JavaDoc {
117     return "";
118   }
119
120   public Object JavaDoc getTopKey() throws SQLException JavaDoc {
121     try {
122       int size = recordList.size();
123       if (topFetched)
124         return size == 0 ? null : new Integer JavaDoc(0);
125       selectIterator.beforeFirst();
126       int noOfRecordsToBeFetched = maxRows != 0 && fetchSize > maxRows
127                                    ? maxRows : fetchSize;
128       Object JavaDoc[] fetchedResult = selectIterator.fetchForward(noOfRecordsToBeFetched);
129       topFetched = true;
130       int numberOfRecordsFetched = fetchedResult == null ? 0 :
131           fetchedResult.length;
132       lastFetchedDirection = true;
133       if (numberOfRecordsFetched == 0) {
134         bottomFetched = true;
135         afterLast = true;
136         return null;
137       }
138       noOfRecordMoved = numberOfRecordsFetched;
139       bottomFetched = maxRows != 0 && maxRows < fetchSize
140                       ? numberOfRecordsFetched <= maxRows
141                       : numberOfRecordsFetched < fetchSize;
142       afterLast = bottomFetched && numberOfRecordsFetched < fetchSize;
143       recordList.clear();
144       recordList.addAll(Arrays.asList(fetchedResult));
145       return new Integer JavaDoc(0);
146     }
147     catch (DException ex) {
148       throw ex.getSqlException(null);
149     }
150   }
151
152   public Object JavaDoc getBottomKey() throws SQLException JavaDoc {
153     try {
154       int size = recordList.size();
155       if (bottomFetched)
156         return size == 0 ? null : new Integer JavaDoc(size - 1);
157       int rowCount = selectIterator.getRowCount();
158       if(rowCount < maxRows || maxRows == 0 ){
159         selectIterator.afterLast();
160       }else{
161         selectIterator.beforeFirst();
162         selectIterator.move(maxRows+1);
163       }
164       Object JavaDoc[] fetchedResult = selectIterator.fetchBackward(fetchSize);
165       bottomFetched = true;
166       afterLast = false;
167       int numberOfRecordsFetched = fetchedResult == null ? 0 :
168           fetchedResult.length;
169       lastFetchedDirection = false;
170       if (numberOfRecordsFetched == 0) {
171         topFetched = true;
172         return size == 0 ? null : new Integer JavaDoc(size - 1);
173       }
174
175       topFetched = numberOfRecordsFetched < fetchSize;
176       noOfRecordMoved = maxRows;
177       recordList.clear();
178       for (int i = numberOfRecordsFetched - 1; i >= 0; i--)
179         recordList.add(fetchedResult[i]);
180       return new Integer JavaDoc(numberOfRecordsFetched - 1);
181     }
182     catch (DException ex) {
183       throw ex.getSqlException(null);
184     }
185   }
186
187   public Object JavaDoc getNextKey(Object JavaDoc key) throws SQLException JavaDoc {
188     try {
189       if (key == null)
190         throw new DException("DSE550", null);
191       int index = key.hashCode();
192       if (index == -1)
193         throw new DException("DSE552", new Object JavaDoc[] {key});
194       int size = recordList.size();
195       if (bottomFetched || size - 1 > index)
196         return size - 1 == index ? null : new Integer JavaDoc(index + 1);
197       int noOfRecordsLeft = maxRows - noOfRecordMoved;
198       if(maxRows !=0 && noOfRecordsLeft == 0){
199         bottomFetched = true;
200         afterLast = true;
201         return null;
202       }
203       if (!lastFetchedDirection)
204         selectIterator.move(topFetched ? recordList.size() :
205                             recordList.size() - 1);
206       lastFetchedDirection = true;
207       int noOfRecordsToBeFetched = maxRows != 0 && fetchSize > noOfRecordsLeft
208                                    ? noOfRecordsLeft : fetchSize;
209       Object JavaDoc[] fetchedResult = selectIterator.fetchForward(noOfRecordsToBeFetched);
210
211       int numberOfRecordsFetched = fetchedResult == null ? 0 :
212           fetchedResult.length;
213       if (numberOfRecordsFetched == 0) {
214         bottomFetched = true;
215         afterLast = true;
216         return null;
217       }
218       topFetched = false;
219       noOfRecordMoved+= numberOfRecordsFetched;
220       bottomFetched = maxRows != 0 && noOfRecordsLeft <= fetchSize
221                       ? numberOfRecordsFetched <= noOfRecordsToBeFetched
222                       : numberOfRecordsFetched < fetchSize;
223       afterLast = bottomFetched && numberOfRecordsFetched < noOfRecordsLeft;
224       recordList.clear();
225       recordList.addAll(Arrays.asList(fetchedResult));
226       return new Integer JavaDoc(0);
227     }
228     catch (DException ex) {
229       throw ex.getSqlException(null);
230     }
231   }
232
233   public Object JavaDoc getPreviousKey(Object JavaDoc key) throws SQLException JavaDoc {
234     try {
235       if (key == null)
236         throw new DException("DSE550", null);
237       int index = key.hashCode();
238       if (index == -1)
239         throw new DException("DSE552", new Object JavaDoc[] {key});
240       int size = recordList.size();
241       if (topFetched || index != 0)
242         return index == 0 ? null : new Integer JavaDoc(index - 1);
243       if (lastFetchedDirection)
244         selectIterator.move(bottomFetched ? (maxRows == 0 || afterLast ? -recordList.size() : -recordList.size() + 1) :
245                             -recordList.size() + 1);
246       lastFetchedDirection = false;
247       afterLast = false;
248       Object JavaDoc[] fetchedResult = selectIterator.fetchBackward(fetchSize);
249       int numberOfRecordsFetched = fetchedResult == null ? 0 :
250           fetchedResult.length;
251       if (numberOfRecordsFetched == 0) {
252         topFetched = true;
253         return null;
254       }
255       bottomFetched = false;
256
257       topFetched = numberOfRecordsFetched < fetchSize;
258       noOfRecordMoved-= size;
259       recordList.clear();
260       for (int i = numberOfRecordsFetched - 1; i >= 0; i--)
261         recordList.add(fetchedResult[i]);
262       return new Integer JavaDoc(numberOfRecordsFetched - 1);
263     }
264     catch (DException ex) {
265       throw ex.getSqlException(null);
266     }
267   }
268
269   public int getDistanceBetweenKeys(Object JavaDoc key1, Object JavaDoc key2) throws
270       SQLException JavaDoc {
271     throw new java.lang.UnsupportedOperationException JavaDoc(
272         "Method getDistanceBetweenKeys() not yet implemented.");
273   }
274
275   public Object JavaDoc getKeyAtDistance(Object JavaDoc key, int distance) throws SQLException JavaDoc {
276     throw new java.lang.UnsupportedOperationException JavaDoc(
277         "Method getKeyAtDistance() not yet implemented.");
278   }
279
280   public Object JavaDoc insertInitiate() throws DException {
281     throw new java.lang.UnsupportedOperationException JavaDoc(
282         "Method insertInitiate() not yet implemented.");
283   }
284
285   public Object JavaDoc updateInitiate(Object JavaDoc currentKey, String JavaDoc columnName,
286                                Object JavaDoc value) throws SQLException JavaDoc {
287     throw new java.lang.UnsupportedOperationException JavaDoc(
288         "Method updateInitiate() not yet implemented.");
289   }
290
291   public Object JavaDoc deleteInitiate(Object JavaDoc deleteRecord) throws DException {
292     throw new java.lang.UnsupportedOperationException JavaDoc(
293         "Method deleteInitiate() not yet implemented.");
294   }
295
296   public Object JavaDoc deleteRow(_Record deleteRecord) throws DException {
297       throw new java.lang.UnsupportedOperationException JavaDoc(
298           "Method deleteInitiate() not yet implemented.");
299   }
300
301   public Object JavaDoc[] getParameters() {
302     throw new java.lang.UnsupportedOperationException JavaDoc(
303         "Method getParameters() not yet implemented.");
304   }
305
306   public Object JavaDoc locateNearestKey(Object JavaDoc key) throws SQLException JavaDoc {
307     throw new java.lang.UnsupportedOperationException JavaDoc(
308         "Method locateNearestKey() not yet implemented.");
309   }
310
311   public _SelectColumnCharacteristics getColumnCharacteristics() {
312     return scc;
313   }
314
315   public Object JavaDoc seek(String JavaDoc clause) throws SQLException JavaDoc {
316     throw new java.lang.UnsupportedOperationException JavaDoc(
317         "Method seek() not yet implemented.");
318   }
319
320   public void setFetchDirection(int direction) {
321     fetchDirection = direction;
322   }
323
324   public void setFetchSize(int fetchSize0) {
325     fetchSize = fetchSize0;
326   }
327
328   public void setMaxRows(int maxRows0) {
329     maxRows = maxRows0;
330   }
331
332   public void setMaxFieldSize(int maxSize) {
333     maxFieldSize = maxSize;
334   }
335
336   public String JavaDoc getQuery() {
337      return query;
338   }
339
340   public void setQuery(String JavaDoc query0) {
341      query = query0;
342   }
343
344   public void updateRow(_Record record) throws DException, SQLException JavaDoc {
345     throw new java.lang.UnsupportedOperationException JavaDoc(
346         "Method updateRow() not yet implemented.");
347   }
348
349   public _RowReader getRowReader() throws SQLException JavaDoc {
350     return rowReader;
351   }
352
353   public Object JavaDoc insertInitiate(String JavaDoc[] columnNames, Object JavaDoc[] values) throws
354       DException {
355     throw new java.lang.UnsupportedOperationException JavaDoc(
356         "Method insertInitiate() not yet implemented.");
357   }
358
359   public Object JavaDoc updateInitiate(Object JavaDoc currentKey, String JavaDoc[] columnNames,
360                                Object JavaDoc[] values) throws SQLException JavaDoc {
361     throw new java.lang.UnsupportedOperationException JavaDoc(
362         "Method updateInitiate() not yet implemented.");
363   }
364
365   public int getRowCount() throws SQLException JavaDoc {
366       try {
367         return selectIterator.getRowCount();
368       }
369       catch (DException ex) {
370         throw ex.getSqlException(null);
371       }
372   }
373
374   public Object JavaDoc convertIntoParameter() throws SQLException JavaDoc, DException {
375     throw new java.lang.UnsupportedOperationException JavaDoc(
376         "Method convertIntoParameter() not yet implemented.");
377   }
378
379   public boolean isBottom(Object JavaDoc key) throws SQLException JavaDoc {
380     try {
381       if (key == null)
382         throw new DException("DSE550", null);
383       int index = key.hashCode();
384       if (index == -1)
385         throw new DException("DSE552", new Object JavaDoc[] {key});
386       int size = recordList.size();
387       if (bottomFetched || size - 1 > index)
388         return size - 1 == index;
389       int noOfRecordsLeft = maxRows - noOfRecordMoved;
390       if(maxRows !=0 && noOfRecordsLeft == 0){
391         bottomFetched = true;
392         afterLast = true;
393         return true;
394       }
395       if (!lastFetchedDirection)
396         selectIterator.move(topFetched ? recordList.size() :
397                             recordList.size() - 1);
398       int noOfRecordsToBeFetched = maxRows != 0 && fetchSize > noOfRecordsLeft
399                                    ? noOfRecordsLeft : fetchSize;
400       Object JavaDoc[] fetchedResult = selectIterator.fetchForward(noOfRecordsToBeFetched);
401       int numberOfRecordsFetched = fetchedResult == null ? 0 :
402           fetchedResult.length;
403       if (numberOfRecordsFetched == 0) {
404         bottomFetched = true;
405         afterLast = true;
406         return true;
407       }
408       topFetched = false;
409       lastFetchedDirection = true;
410       Object JavaDoc lastValue = recordList.get(index);
411       noOfRecordMoved+= numberOfRecordsFetched;
412       bottomFetched = maxRows != 0 && noOfRecordsToBeFetched < fetchSize
413                       ? numberOfRecordsFetched <= noOfRecordsToBeFetched
414                       : numberOfRecordsFetched < fetchSize;
415       afterLast = bottomFetched && numberOfRecordsFetched < noOfRecordsLeft;
416       recordList.clear();
417       recordList.add(lastValue);
418       recordList.addAll(Arrays.asList(fetchedResult));
419       recordSetBufferIterator.moveToKey(new Integer JavaDoc(0));
420       return false;
421     }
422     catch (DException ex) {
423       throw ex.getSqlException(null);
424     }
425   }
426
427   public boolean isTop(Object JavaDoc key) throws SQLException JavaDoc {
428     try {
429       if (key == null)
430         throw new DException("DSE550", null);
431       int index = key.hashCode();
432       if (index == -1)
433         throw new DException("DSE552", new Object JavaDoc[] {key});
434       int size = recordList.size();
435       if (topFetched || index != 0)
436         return index == 0;
437       if (lastFetchedDirection)
438         selectIterator.move(bottomFetched ? (maxRows == 0 || afterLast ? -recordList.size() : -recordList.size() + 1) :
439                             -recordList.size() + 1);
440       Object JavaDoc[] fetchedResult = selectIterator.fetchBackward(fetchSize);
441       int numberOfRecordsFetched = fetchedResult == null ? 0 :
442           fetchedResult.length;
443       if (numberOfRecordsFetched == 0) {
444         topFetched = true;
445         return true;
446       }
447       bottomFetched = false;
448       lastFetchedDirection = false;
449       topFetched = numberOfRecordsFetched < fetchSize;
450       noOfRecordMoved-= (size -1);
451       Object JavaDoc lastValue = recordList.get(0);
452       recordList.clear();
453       for (int i = numberOfRecordsFetched - 1; i >= 0; i--)
454         recordList.add(fetchedResult[i]);
455       recordList.add(lastValue);
456       recordSetBufferIterator.moveToKey(new Integer JavaDoc(numberOfRecordsFetched));
457       return false;
458     }
459     catch (DException ex) {
460       throw ex.getSqlException(null);
461     }
462   }
463
464   public void setUpdateMode(boolean immediate) {
465     throw new java.lang.UnsupportedOperationException JavaDoc(
466         "Method setUpdateMode() not yet implemented.");
467   }
468
469   public _Record getRecordInstance() {
470     return new JDBCRecord(isUpdateable);
471   }
472
473   public _Record getInsertedRecord() throws DException {
474     throw new java.lang.UnsupportedOperationException JavaDoc(
475         "Method getInsertedRecord() not yet implemented.");
476   }
477
478   public Object JavaDoc insertRow() throws DException {
479     throw new java.lang.UnsupportedOperationException JavaDoc(
480         "Method insertRow() not yet implemented.");
481   }
482
483   public _ExecutionPlan getExecutionPlan() throws DException {
484     return selectIterator.getExecutionPlan();
485   }
486
487   public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException {
488     return selectIterator.getExecutionPlanForBrowser();
489   }
490
491   public _QueryPlan getQueryPlan() throws DException {
492     return selectIterator.getQueryPlan();
493   }
494
495   public _Record getDummyRecord() throws SQLException JavaDoc {
496     /**@todo Implement this com.daffodilwoods.daffodildb.client._RecordSetBuffer method*/
497     throw new java.lang.UnsupportedOperationException JavaDoc(
498         "Method getDummyRecord() not yet implemented.");
499   }
500
501   public void flushRecords(Object JavaDoc parm1, boolean parm2) throws com.
502
      daffodilwoods.database.resource.DException {
503     /**@todo Implement this com.daffodilwoods.daffodildb.client._RecordSetBuffer method*/
504     throw new java.lang.UnsupportedOperationException JavaDoc(
505         "Method flushRecords() not yet implemented.");
506   }
507
508   public void setAutoFlush(boolean flushData0) throws DException {
509     throw new java.lang.UnsupportedOperationException JavaDoc(
510         "Method not yet implemented.");
511   }
512
513   public void setBufferSize(int bufferSize0) throws DException {
514     throw new java.lang.UnsupportedOperationException JavaDoc(
515         "Method not yet implemented.");
516   }
517
518   void decrementNumberOfRecodsMoved(){
519     if(maxRows >0 ){
520       noOfRecordMoved--;
521       bottomFetched = false;
522     }
523   }
524
525   void updateBottomFetched(){
526       if (maxRows == 0 || maxRows > noOfRecordMoved) {
527         bottomFetched = false;
528         afterLast = false;
529       }
530   }
531 }
532
Popular Tags