KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > StaticCursor


1 package com.quadcap.sql;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.Externalizable JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.ObjectInput JavaDoc;
44 import java.io.ObjectOutput JavaDoc;
45
46 import java.util.Collections JavaDoc;
47 import java.util.Comparator JavaDoc;
48 import java.util.Vector JavaDoc;
49
50 import java.sql.SQLException JavaDoc;
51
52 import com.quadcap.sql.types.Op;
53 import com.quadcap.sql.types.Value;
54 import com.quadcap.sql.types.ValueException;
55
56 import com.quadcap.util.Debug;
57
58 /**
59  * Cursor implementation of <b>VALUES</b> clause. Contains either a row
60  * or a Vector of rows.
61  *
62  * @author Stan Bailes
63  */

64 public class StaticCursor extends CursorImpl implements Externalizable JavaDoc {
65     Tuple tuple = null;
66     Vector JavaDoc v;
67     Row currentRow = null;
68     int pos = 0;
69
70     /**
71      * Constructor for a static cursor from a tuple and single row
72      */

73     public StaticCursor(Session session, Tuple tuple, Row row) {
74     super(session, "");
75     this.tuple = tuple;
76     this.v = new Vector JavaDoc();
77     v.addElement(row);
78     }
79
80     /**
81      * Constructor for a static cursor from a vector of rows.
82      */

83     public StaticCursor(Session session, Vector JavaDoc v) {
84     super(session, "");
85     this.v = v;
86     }
87
88     /**
89      * Add a row to the cursor's row set.
90      */

91     public void addRow(Row row) {
92     v.addElement(row);
93     }
94
95     /**
96      * Return the current row
97      */

98     public Row getRow() {
99     return currentRow;
100     }
101
102     /**
103      * Static cursors aren't updateable.
104      */

105     public void updateRow(Row row) throws SQLException JavaDoc {
106     throw new SQLException JavaDoc("Not implemented for static cursor", "0A000");
107     }
108
109     /**
110      * Static cursors aren't updateable.
111      */

112     public void deleteRow() throws SQLException JavaDoc {
113     throw new SQLException JavaDoc("Not implemented for static cursor", "0A000");
114     }
115
116     /**
117      * Return the specified column.
118      *
119      * Static cursors may or may not have tuple information. If they
120      * do, use the tuple information directly, otherwise default to the
121      * superclass (TupleImpl) implementation of getColumn.
122      */

123     public Column getColumn(int col) throws SQLException JavaDoc {
124     if (tuple == null) {
125         return super.getColumn(col);
126     }
127     return tuple.getColumn(col);
128     }
129
130     /**
131      * Return the specified column.
132      *
133      * Static cursors may or may not have tuple information. If they
134      * do, use the tuple information directly, otherwise default to the
135      * superclass (TupleImpl) implementation of getColumn.
136      */

137     public Column getColumn(String JavaDoc columnName) throws SQLException JavaDoc {
138     if (tuple == null) {
139         return super.getColumn(columnName);
140     }
141     return tuple.getColumn(columnName);
142     }
143
144     /**
145      * Get the value for the specified column
146      */

147     public Value get(String JavaDoc columnName) throws SQLException JavaDoc {
148     Column col = getColumn(columnName);
149     if (col != null) {
150         return currentRow.item(col.getColumn());
151     }
152     return null;
153     }
154
155     /**
156      * Put a new value into the current row for the specified column
157      * (Who calls this? We're not supposed to be writable!!!)
158      */

159     public void put(String JavaDoc columnName, Value v) throws SQLException JavaDoc {
160     Column col = getColumn(columnName);
161     if (col != null) {
162         currentRow.set(col.getColumn(), v);
163     }
164     }
165
166     /**
167      * Return the column count.
168      */

169     public int getColumnCount() throws SQLException JavaDoc {
170     if (tuple != null) return tuple.getColumnCount();
171     int ret = super.getColumnCount();
172     if (ret > 0) return ret;
173     if (v.size() > 0) {
174         Row row = (Row)v.elementAt(0);
175         return row.size();
176     }
177     throw new SQLException JavaDoc("no columns", "Q0017");
178     }
179
180     /**
181      * Static cursors don't allow column update
182      */

183     public boolean isWritable(int column) { return false; }
184
185     /**
186      * Return the number of rows in this cursor.
187      */

188     public long size() throws SQLException JavaDoc { return v.size(); }
189     
190     /**
191      * Position the cursor to the specified absolute row. The first row
192      * is row '1', and the last row is '-1', as in JDBC.
193      */

194     public boolean absolute(int row) throws SQLException JavaDoc {
195     currentRow = null;
196     if (row == 0) {
197         throw new SQLException JavaDoc("Attempt to position cursor on row zero",
198                    "42000");
199     }
200     if (row < 0) {
201         row += v.size();
202     }
203     if (row <= 0 || row > v.size()) return false;
204     currentRow = (Row)v.elementAt(row-1);
205     return true;
206     }
207
208     /**
209      * Position the cursor before the first row.
210      */

211     public void beforeFirst() throws SQLException JavaDoc {
212         pos = 0;
213     }
214
215     /**
216      * Position the cursor after the last row.
217      */

218     public void afterLast() throws SQLException JavaDoc {
219         pos = v.size();
220     }
221
222     /**
223      * Move the cursor to the next row and return true if the cursor is
224      * positioned on a valid row.
225      */

226     public boolean next() {
227     currentRow = null;
228         if (pos < v.size()) {
229             currentRow = (Row)v.elementAt(pos++);
230         }
231     return currentRow != null;
232     }
233
234     /**
235      * Move the cursor to the previous row and return true if the cursor is
236      * positioned on a valid row.
237      */

238     public boolean prev() {
239     currentRow = null;
240         if (pos > 0 && pos <= v.size()) {
241             currentRow = (Row)v.elementAt(--pos);
242         }
243     return currentRow != null;
244     }
245
246     /**
247      * Read me from a stream
248      */

249     public void readExternal(ObjectInput JavaDoc in)
250     throws IOException JavaDoc, ClassNotFoundException JavaDoc
251     {
252     this.pos = in.readInt();
253     this.v = (Vector JavaDoc)in.readObject();
254     }
255
256     /**
257      * Write me to a stream
258      */

259     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
260     out.writeInt(pos);
261     out.writeObject(v);
262     }
263
264     /**
265      * Close.
266      */

267     public void close() {
268         tuple = null;
269         currentRow = null;
270         v = null;
271     }
272
273     public int[] getSortColumns() {
274     return new int[0];
275     }
276
277     public Comparator JavaDoc getComparator() {
278     return new Comparator JavaDoc() {
279         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
280         try {
281             Row r1 = (Row)o1;
282             Row r2 = (Row)o2;
283             int[] keys = getSortColumns();
284             for (int i = 0; i < keys.length; i++) {
285             int col = keys[i];
286             Value v1 = r1.item(col);
287             Value v2 = r2.item(col);
288             if (Value.boolOp(Op.LT, v1, v2)) return -1;
289             if (Value.boolOp(Op.GT, v1, v2)) return 1;
290             }
291         } catch (ValueException e) {
292             Debug.print(e);
293             throw new DbRuntimeException(e);
294         } catch (SQLException JavaDoc e) {
295             Debug.print(e);
296             throw new DbRuntimeException(e);
297         } catch (Throwable JavaDoc e) {
298             Debug.print(e);
299             throw new DbRuntimeException(e);
300         }
301         return 0;
302         }
303     };
304     }
305
306     public void sort() throws ValueException, SQLException JavaDoc {
307     try {
308         Collections.sort(v, getComparator());
309     } catch (DbRuntimeException e) {
310         Throwable JavaDoc ex = e.getException();
311         if (ex instanceof ValueException) {
312         throw (ValueException)ex;
313         } else if (ex instanceof SQLException JavaDoc) {
314         throw (SQLException JavaDoc)ex;
315         } else {
316         throw e;
317         }
318     }
319     }
320
321     public void updateRow(int pos, Row row) {
322     v.setElementAt(row, pos);
323     }
324
325     public void reset() {
326     pos = 0;
327     }
328 }
329
Popular Tags