KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dml > fetchstatement


1 package com.daffodilwoods.daffodildb.server.sql99.dml;
2
3 import java.util.*;
4
5 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
6 import com.daffodilwoods.daffodildb.server.datasystem.utility.*;
7 import com.daffodilwoods.daffodildb.server.serversystem.*;
8 import com.daffodilwoods.daffodildb.server.sql99.*;
9 import com.daffodilwoods.daffodildb.server.sql99.common.*;
10 import com.daffodilwoods.daffodildb.server.sql99.dml.declarecursor.*;
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.token.*;
14 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
15 import com.daffodilwoods.daffodildb.utils.*;
16 import com.daffodilwoods.daffodildb.utils.field.*;
17 import com.daffodilwoods.database.resource.*;
18 import com.daffodilwoods.daffodildb.utils.FieldUtility;
19
20 /**
21  * This Class is responisble for fetching the records from the query
22  * specification specified in declare cursor statement. It retrives
23  * values from the row depending on fetch orientaion specified by
24  * user in to the fetch target list.
25  * Fetch orientaion may be FIRST, PRIOR, NEXT or LAST or may be
26  * absolute or relative number.
27  *
28  * <p>Title: </p>
29  * <p>Description: </p>
30  * <p>Copyright: Copyright (c) 2003</p>
31  * <p>Company: </p>
32  * @author unascribed
33  * @version 1.0
34  */

35 public class fetchstatement implements com.daffodilwoods.daffodildb.utils.parser.StatementExecuter,
36     SQLdatastatement, _Executer {
37    public fetchtargetlist _fetchtargetlist0;
38    public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439221;
39    public cursorname _cursorname2;
40    public OptfetchorientationSRESERVEDWORD1206543922
41        _OptOptfetchorientationSRESERVEDWORD12065439223;
42    public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439224;
43
44    private _ServerSession ss;
45    private String JavaDoc fetchOrientation = null;
46    private Object JavaDoc[] targetSpecArray = null;
47    private _Cursor cursor = null;
48    private int jumpValue = 0;
49    private int columnCount = 0;
50    private boolean isForAlias;
51
52    /**
53     *This method determines the specified fetch orientation & jump-value in
54     * case of absolute or relative fetch orientation.
55     * @param object
56     * @return
57     * This Method thrwos exception if cursor is in close state, fetch
58     * orientation is specified as NEXT for Non-crollable cursor and degree
59     * of target specification mismatches that of the degree of Table specified
60     * in cursor specification of declare cursor statement.
61     * @throws DException
62     */

63    public Object JavaDoc run(Object JavaDoc object) throws DException {
64       _StatementExecutionContext sec = (_StatementExecutionContext) object;
65       ss = sec.getServerSession();
66       boolean svsPresence = false;
67       cursor = (_Cursor) ss.getCursor( (String JavaDoc) _cursorname2.run(object));
68       if (!cursor.getCursorState()) {
69          throw new DException("DSE8051",new Object JavaDoc[]{cursor.getCursorName()} );
70       }
71       if (targetSpecArray == null) {
72          throw new DException("DSE1307", null);
73       }
74       if (_OptOptfetchorientationSRESERVEDWORD12065439223 == null) {
75          fetchOrientation = SqlKeywords.NEXT;
76       } else {
77          Object JavaDoc returnValue = _OptOptfetchorientationSRESERVEDWORD12065439223.run(
78              object);
79          if (returnValue instanceof String JavaDoc) {
80             fetchOrientation = (String JavaDoc) returnValue;
81          } else {
82             svsPresence = true;
83             fetchOrientation = ( (Object JavaDoc[]) returnValue)[0].toString();
84             jumpValue = ( (Object JavaDoc[]) returnValue)[1].hashCode();
85          }
86       }
87       if (!fetchOrientation.equalsIgnoreCase(SqlKeywords.NEXT) &&
88           !cursor.getCursorScrollability()) {
89          throw new DException("DSE1186", new Object JavaDoc[] {cursor.getCursorName()});
90       }
91       if ( ( (cursorspecification) cursor.getCursorSpecification()).
92           _queryexpression2.getColumnDetails().length !=
93           (columnCount = targetSpecArray.length)) {
94          throw new DException("DSE1185", null);
95       }
96       return this;
97    }
98
99    /**
100     * This method actually positions the cursor at the specified fetch orientation.
101     * NEXT fetch orientation - Determines the first record if the records are
102     * fetching from cursor first time otherwise next record.
103     * PRIOR fetch orientation - Determines the previous record of iterator for
104     * scrollable cursor.
105     * LAST fetch orientation - Determines the last record of iterator for
106     * scrollable cursor.
107     * FIRST fetch orientation - Determines the first record of iterator set for
108     * cursor.
109     * ABSOLUTE fetch orientation -Checks the Jump value ( i.e. let n ) of
110     * iterator, if n > 0, returns the nth record from the first record, if n < 0,
111     * returns the nth record from the last record, and the current record if n=0.
112     * RELATIVE fetch orientation -Checks the Jump value ( i.e. let n ) of
113     * iterator, if n > 0, returns the nth record from the current record moving
114     * towards last record, if n < 0, returns the nth record from the last record
115     * moving towards first record, and the current record if n=0.
116     * @param vv
117     * @return
118     * @throws DException If the cursor in in close state.
119     */

120    public Object JavaDoc execute(_VariableValues vv) throws DException {
121       _Record record = null;
122       if (cursor.getCursorState() == false) {
123          throw new DException("DSE8051", new Object JavaDoc[] {cursor.getCursorName()});
124       }
125
126       _Iterator iterator = cursor.getIterator();
127       if (fetchOrientation.equalsIgnoreCase(SqlKeywords.NEXT)) {
128          if (cursor.isFetchStatementFired() == false) {
129             if (iterator.first()) {
130                record = iterator.getRecord();
131             }
132          } else {
133             if (iterator.next()) {
134                record = iterator.getRecord();
135             }
136          }
137       } else if (fetchOrientation.equalsIgnoreCase(SqlKeywords.PRIOR) &&
138                  cursor.getCursorScrollability()) {
139          if (iterator.previous()) {
140             record = iterator.getRecord();
141          }
142       } else if (fetchOrientation.equalsIgnoreCase(SqlKeywords.FIRST)) {
143          if (iterator.first()) {
144             record = iterator.getRecord();
145          }
146       } else if (fetchOrientation.equalsIgnoreCase(SqlKeywords.LAST) &&
147                  cursor.getCursorScrollability()) {
148          if (iterator.last()) {
149             record = iterator.getRecord();
150          }
151       } else if (fetchOrientation.equalsIgnoreCase(SqlKeywords.ABSOLUTE) &&
152                  cursor.getCursorScrollability()) {
153          if (jumpValue > 0) {
154             iterator.first();
155             boolean flag = true;
156             for (int i = 0; i < jumpValue && flag; i++) {
157                flag = iterator.next();
158             }
159             if (flag) {
160                record = iterator.getRecord();
161             }
162          } else
163          if (jumpValue < 0) {
164             iterator.last();
165             boolean flag = true;
166             for (int i = 0; i < jumpValue && flag; i++) {
167                flag = iterator.previous();
168             }
169             if (flag) {
170                record = iterator.getRecord();
171             }
172          } else {
173             record = iterator.getRecord();
174          }
175       } else if (fetchOrientation.equalsIgnoreCase(SqlKeywords.RELATIVE) &&
176                  cursor.getCursorScrollability()) {
177          if (jumpValue > 0) {
178             boolean flag = true;
179             for (int i = 0; i < jumpValue && flag; i++) {
180                flag = iterator.next();
181             }
182             if (flag) {
183                record = iterator.getRecord();
184             }
185          } else
186          if (jumpValue < 0) {
187             boolean flag = true;
188             for (int i = 0; i < jumpValue && flag; i++) {
189                flag = iterator.previous();
190             }
191             if (flag) {
192                record = iterator.getRecord();
193             }
194          } else {
195             record = iterator.getRecord();
196             cursor.setIteratorState(true);
197          }
198       }
199       cursor.setFetchStatementFired(true);
200       if (isForAlias) {
201          if (record != null) {
202             cursor.setIteratorState(true);
203          } else {
204             cursor.setIteratorState(false);
205          }
206       } else if (record != null) {
207          Object JavaDoc[] recordValues = record.getObject();
208          Object JavaDoc[][] referenceNvalue = vv.getReferenceAndValuePair(); // to correct it.
209

210          _ColumnCharacteristics cc = ( (_SelectIterator) iterator).
211              getColumnCharacteristics();
212          Object JavaDoc[] recordValue = (Object JavaDoc[]) ( (_SelectIterator) iterator).getSelectColumnValues();
213
214          _Reference[] referencesNew = new _Reference[columnCount];
215          Object JavaDoc[] valuesForReferences = new Object JavaDoc[columnCount];
216          String JavaDoc tmp = null;
217          for (int i = 0; i < targetSpecArray.length; i++) {
218             tmp = ( (_Reference) targetSpecArray[i]).getColumn();
219             referencesNew[i] = getCorrespondingReference(referenceNvalue, tmp);
220          }
221
222          for (int j = 1; j <= columnCount; j++) {
223             Object JavaDoc obj = recordValue[j - 1];
224             valuesForReferences[j - 1] = obj;
225          }
226          makeProvisionForAllReferences(referencesNew, valuesForReferences, vv);
227          vv.setConditionVariableValue(referencesNew, valuesForReferences, 0);
228          cursor.setIteratorState(true);
229       } else {
230          /** @todo what to do when there is no record from iterator */
231          cursor.setIteratorState(false);
232       }
233       return null;
234    }
235
236    /**
237     * Returns the reference of particular value form the reference and object
238     * array value pair.
239     * @param referenceNvalue
240     * @param name
241     * @return
242     * @throws DException
243     */

244    private _Reference getCorrespondingReference(Object JavaDoc[][] referenceNvalue,
245                                                 String JavaDoc name) throws DException {
246       _Reference tmpReference = null;
247       for (int i = 0; i < columnCount; i++) {
248          tmpReference = (_Reference) referenceNvalue[i][0];
249          if (name.equalsIgnoreCase(tmpReference.getColumn())) {
250             return tmpReference;
251          }
252       }
253       throw new DException("DSE_FOR_REFERENCE_NOT_FOUND", (Object JavaDoc[])null);
254    }
255
256    public Object JavaDoc execute(Object JavaDoc[] parm1) throws DException {
257       /**@todo Implement this com.daffodilwoods.daffodildb.server.sql99._Executer method*/
258       throw new java.lang.UnsupportedOperationException JavaDoc(
259           "Method execute() with Object Array Not supported by Cursors");
260    }
261
262    public String JavaDoc toString() {
263       StringBuffer JavaDoc clause = new StringBuffer JavaDoc();
264       clause.append(" ");
265       clause.append(_SRESERVEDWORD12065439224.toString());
266       if (_OptOptfetchorientationSRESERVEDWORD12065439223 != null) {
267          clause.append(" ");
268          clause.append(_OptOptfetchorientationSRESERVEDWORD12065439223.toString());
269       }
270       clause.append(" ");
271       clause.append(_cursorname2.toString());
272       clause.append(" ");
273       clause.append(_SRESERVEDWORD12065439221.toString());
274       clause.append(" ");
275       clause.append(_fetchtargetlist0.toString());
276       return clause.toString().trim();
277    }
278
279    public Object JavaDoc[] getParameters(Object JavaDoc parm1) throws DException {
280       return null;
281    }
282
283    public Object JavaDoc executeForFresh(Object JavaDoc[] obj) throws DException {
284       throw new java.lang.UnsupportedOperationException JavaDoc(
285           "Method executeForFresh not yet implemented.");
286    }
287
288    /**
289     *
290     * @param serverSession
291     * @return
292     * @throws DException
293     */

294    public _Reference[] checkSemantic(_ServerSession serverSession) throws
295        DException {
296       ArrayList list = new ArrayList();
297       _Reference[] parm1 = _fetchtargetlist0.getReferences(serverSession);
298       if (parm1 != null) {
299          list.addAll(Arrays.asList(parm1));
300          targetSpecArray = parm1;
301       }
302       _Reference[] parm2 = _cursorname2.getReferences(new TableDetails[0]);
303       if (parm2 != null) {
304          list.addAll(Arrays.asList(parm2));
305       }
306       return GeneralPurposeStaticClass.changeReferences(targetSpecArray);
307    }
308
309    public _Reference[] getReferences() throws DException {
310       return null;
311    }
312
313    /**
314     * Sets references and the values to instance of _VariableValues.
315     * @param references
316     * @param values
317     * @param vv
318     * @throws DException
319     */

320    private void makeProvisionForAllReferences(_Reference[] references, Object JavaDoc[] values, _VariableValues vv) throws DException {
321       Object JavaDoc[][] afterExecuteRefValuePair = vv.getReferenceAndValuePair();
322       for (int j = 0; j < references.length; j++) {
323          for (int i = 0; i < afterExecuteRefValuePair.length; i++) {
324             _Reference vvRef = (_Reference) afterExecuteRefValuePair[i][0];
325             if (vvRef.getColumn().equalsIgnoreCase(references[j].getColumn())) {
326                values[i] = FieldUtility.convertToAppropriateType( (FieldBase) values[i], vvRef.getDatatype(), vvRef.getSize(), null);
327                vv.setConditionVariableValue(
328                    new _Reference[] {vvRef}
329                    , new Object JavaDoc[] {values[i]}
330                    , 1);
331             }
332          }
333       }
334    }
335
336    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
337       return this;
338    }
339
340    public void setForAlias(boolean isForAlias0) throws DException {
341       isForAlias = isForAlias0;
342    }
343 }
344
345
Popular Tags