KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dml > declarecursor > MemoryTableIterator


1 package com.daffodilwoods.daffodildb.server.sql99.dml.declarecursor;
2
3 import com.daffodilwoods.database.resource.*;
4 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
5 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.TableCharacteristics;
6 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
7 import com.daffodilwoods.daffodildb.server.datasystem.utility.*;
8 import java.util.*;
9 import com.daffodilwoods.daffodildb.utils.field.FieldBase;
10 import com.daffodilwoods.daffodildb.utils.BufferRange;
11
12 /**
13  *
14  * <p>Title: MemoryTableIterator</p>
15  * <p>Description: Objective of MemoryTableIterator is to access the
16  * records of memory table .When more than one user works on the table
17  * then each one is provided with the iterator .Every user can access
18  * the records of the memory table through their respective iterators.
19  */

20 public class MemoryTableIterator implements _TableIterator,_TableOperations{
21
22     /**
23    CurrentKey maintains the current pointer of memoryTableIterator on
24    memory Table.
25    */

26    private Object JavaDoc currentKey ;
27
28     /**
29      * table on which the iterator navigates to perform insert,update,delete
30      * and retrieve operations.
31      */

32
33    private MemoryTable memoryTable;
34
35     /**
36      * status = -1 ::::::: Invalid state before First
37      * status = 0 ::::::: Valid state
38      * status = 1 ::::::: Invalid state after Last
39      */

40
41   private int status;
42     /**
43      * Constructs the MemoryTableIterator with the instance of MemoryTable
44      * @param memoryTable0 Is the table on which the iterator navigates
45      * to perform insert,update,delete and retrieve operations.
46      */

47     public MemoryTableIterator(MemoryTable memoryTable0) throws DException {
48         memoryTable = memoryTable0;
49         status = -1;
50     }
51
52     /**
53      * Sets the pointer to the first record in the table and returns true
54      * if record exists in the table otherwise returns false
55      * @return true if first record exists in the table otherwise
56      * return false.
57      */

58
59     public boolean first() throws DException {
60         Object JavaDoc key = memoryTable.getFirstKey();
61         boolean flag = (currentKey = key) != null;
62         status = flag ? 0 : -1;
63         return flag;
64     }
65
66     /**
67      * Sets the pointer to the last record in the table and returns true
68      * if record exists in the table otherwise returns false
69      * @return true if last record exists in the table otherwise
70      * return false.
71      */

72
73     public boolean last() throws DException {
74         Object JavaDoc key = memoryTable.getLastKey();
75         boolean flag = (currentKey = key) != null;
76         status = flag ? 0 : 1;
77         return flag;
78     }
79
80     /**
81      * Sets the pointer to the next record of the current pointer
82      * and returns true if next record exists in the table otherwise
83      * returns false
84      * @return true if next record of currentKey exists in the table.
85      * otherwise returns false
86      */

87     public boolean next() throws DException {
88         if(status == 1)
89             return false;
90         if(status == -1)
91             return first();
92         while(true){
93             try{
94                 currentKey = new Integer JavaDoc(currentKey.hashCode()+1);
95                 memoryTable.verifyValidity(currentKey);
96                 status = 0;
97                 return true;
98             }catch(DException e){
99                 if(e.getDseCode().equalsIgnoreCase("DSE2002")){
100                     status = 1;
101                     return false;
102                 }
103                 else if(!e.getDseCode().equalsIgnoreCase("DSE2003"))
104                     throw e;
105             }
106         }
107     }
108
109     /**
110      * Sets the pointer to the previous record of the current pointer
111      * and returns true if previous record exists in the table otherwise
112      * returns false
113      * @return true if previous record of currentKey exists in the table.
114      * otherwise returns false
115      */

116
117     public boolean previous() throws DException {
118         if(status == -1)
119             return false;
120         if(status == 1)
121             return last();
122         while(true){
123             try{
124                 currentKey = new Integer JavaDoc(currentKey.hashCode()-1);
125                 memoryTable.verifyValidity(currentKey);
126                 status = 0;
127                 return true;
128             }catch(DException e){
129                 if(e.getDseCode().equalsIgnoreCase("DSE2002")){
130                     status = -1;
131                     return false;
132                 }
133                 else if(!e.getDseCode().equalsIgnoreCase("DSE2003"))
134                     throw e;
135             }
136         }
137     }
138
139     /**
140      * returns currentKey of iterator
141      * @return currentKey of iterator
142      */

143
144     public Object JavaDoc getKey()throws DException{
145         return currentKey;
146     }
147
148     /**
149      * Inserts record in memory table and updates its currentKey and status
150      *
151      * @param values Is an array values to be inserted in the table as a record.
152      */

153
154     public void insert(Object JavaDoc values)throws DException{
155         currentKey = memoryTable.insert(values);
156         status = 0;
157     }
158
159     /**
160      * updates the record in memory table at the currentKey
161      * @param values values with which the record at the currentKey
162      * is to be updated.
163      */

164     public void update(Object JavaDoc values)throws DException{
165             currentKey = memoryTable.update(currentKey,values);
166             status = 0;
167     }
168
169     /**
170      * deltes the record in memory table at the currentKey
171      */

172     public void delete() throws DException{
173             memoryTable.delete(currentKey);
174     }
175
176
177     /**
178      * Forms an array of values and invokes the update method with these values.
179      * @param columns specify the columns whose values are to be updated
180      * @param values specify the values with which those columns are to be updated.
181      * @throws DException if the number of values is not equal to the number of columns
182      */

183     public void update(int[] columns, Object JavaDoc[] values) throws DException {
184                if((columns.length != values.length))
185                    throw new DataException("DSE754",null);
186                Object JavaDoc oldVal[] = (Object JavaDoc[])getColumnValues();
187                for (int j = 0; j < columns.length ; j++)
188                    oldVal[columns[j]]=values[j];
189                currentKey = memoryTable.update(currentKey,oldVal);
190                status =0;
191     }
192
193     /**
194      * moves the pointer of iterator on the given key.
195      * @param key the position to which the pointer is to be moved.
196      */

197
198     public void move(Object JavaDoc key) throws DException {
199         currentKey = key;
200         status = 0;
201     }
202
203
204
205     public void show() throws DException {
206         if(first()){
207             do{
208             }while(next());
209         }
210     }
211
212     /**
213      * returns record at the current position from the memoryTable
214      * @return record at the current position from the memoryTable
215      */

216
217     public _Record getRecord() throws DException {
218         Record record = new Record((TableCharacteristics)memoryTable.getTableCharacteristics(),null);
219         record.setObject((Object JavaDoc[])getColumnValues());
220         return record;
221     }
222
223     public String JavaDoc toString() {
224         return "";
225     }
226
227     /**
228      * returns the value of the given column at the current position
229      * from the memory table.
230      * @param column specifies the column whose value is to be retreived.
231      * @return the value of the given column at the current position
232      * from the memory table.
233      * @throws DException if iterator is not at valid key
234      */

235
236
237
238     public Object JavaDoc getColumnValues(int parm1) throws com.daffodilwoods.database.resource.DException {
239         if(status != 0)
240             throw new DException("DSE2019",new Object JavaDoc[] {new Integer JavaDoc(status)});
241         Object JavaDoc[] values = (Object JavaDoc[])memoryTable.getObjectAtKey(currentKey);
242         return values[parm1];
243     }
244
245     /**
246      * returns the values at the current position
247      * from the memory table.
248      * @return the values at the current position from the memory table.
249      * @throws DException if iterator is not at valid key
250      */

251
252
253
254     public Object JavaDoc getColumnValues() throws com.daffodilwoods.database.resource.DException {
255         if(status != 0)
256             throw new DException("DSE2019",new Object JavaDoc[] {new Integer JavaDoc(status)});
257         Object JavaDoc obj = memoryTable.getObjectAtKey(currentKey);
258         Object JavaDoc[] row = (Object JavaDoc[]) obj;
259         Object JavaDoc[] clonedRow = new Object JavaDoc[row.length];
260         System.arraycopy(row,0,clonedRow,0,row.length);
261         return clonedRow;
262     }
263
264
265
266     /**
267      * returns the values of the given columns at the current position
268      * from the memory table.
269      * @param columns specifies the columns whose values is to be retreived.
270      * @return the values of the given columns at the current position
271      * from the memory table.
272      * @throws DException if iterator is not at valid key
273      */

274
275     public Object JavaDoc getColumnValues(int[] columns) throws com.daffodilwoods.database.resource.DException {
276         if(status != 0)
277             throw new DException("DSE2019",new Object JavaDoc[] {new Integer JavaDoc(status)});
278         Object JavaDoc[] oldVal = (Object JavaDoc[])memoryTable.getObjectAtKey(currentKey);
279         Object JavaDoc[] newVal = new Object JavaDoc[columns.length];
280         for (int i = 0; i < columns.length; i++)
281             newVal[i] = oldVal[columns[i]];
282         return newVal;
283     }
284     public byte[] getByteKey() throws DException{
285       throw new java.lang.UnsupportedOperationException JavaDoc(
286         "Method getByteKey() not yet implemented.");
287     }
288     public void moveByteKey(byte[] key) throws DException{
289       throw new java.lang.UnsupportedOperationException JavaDoc(
290         "Method moveByteKey() not yet implemented.");
291         }
292
293         public void deleteBlobClobRecord(_DatabaseUser user) throws DException {
294           throw new UnsupportedOperationException JavaDoc(
295               "method deleteBlobClobRecord not supported ");
296
297         }
298
299 }
300
Popular Tags