KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.SQLException JavaDoc;
47
48 import com.quadcap.sql.io.ObjectInputStream;
49 import com.quadcap.sql.io.ObjectOutputStream;
50
51 import com.quadcap.sql.file.SubPageManager;
52 import com.quadcap.sql.index.BCursor;
53
54 import com.quadcap.util.Debug;
55
56 /**
57  * Cursor that implements the <b>DISTINCT</b> modifier by creating
58  * temporary table, where the entire row is used as the index key.
59  *
60  * @author Stan Bailes
61  */

62 public abstract class BC_Cursor extends CursorImpl {
63     protected BCursor bc = null;
64     protected LazyRow row = null;
65     protected boolean rowValid = false;
66     protected long rowId = 0;
67
68     /**
69      * Constructor on session and cursor
70      */

71     public BC_Cursor(Session session, String JavaDoc name, Cursor outer)
72         throws SQLException JavaDoc
73     {
74     super(session, name, outer);
75     }
76
77     /**
78      * Cursor.getRow(): return the current table row, fetching it if necessary
79      * based on the rowId
80      */

81     public Row getRow() throws SQLException JavaDoc {
82         Row ret = null;
83         if (rowId != 0) {
84             if (!rowValid) {
85                 try {
86                     fetchCurrentRow();
87                 } catch (IOException JavaDoc ex) {
88                     throw DbException.wrapThrowable(ex);
89                 }
90             }
91             if (rowValid) {
92                 ret = row;
93             }
94         }
95         return row;
96     }
97
98     /**
99      * Make the row too, while you're at it...
100      */

101     public abstract void checkCursor() throws IOException JavaDoc, SQLException JavaDoc;
102
103     public abstract long getCurrentRowId() throws IOException JavaDoc, SQLException JavaDoc;
104
105     public abstract void fetchCurrentRow() throws IOException JavaDoc, SQLException JavaDoc;
106     
107     public boolean isWritable(int column) { return false; }
108
109     public boolean absolute(int x) throws SQLException JavaDoc {
110     try {
111             rowValid = false;
112             rowId = 0;
113             checkCursor();
114             boolean ret = bc.absolute(x);
115             if (ret) {
116                 rowId = getCurrentRowId();
117             }
118             return ret;
119     } catch (IOException JavaDoc e) {
120         throw DbException.wrapThrowable(e);
121     }
122     }
123
124     public void beforeFirst() throws SQLException JavaDoc {
125     try {
126             rowValid = false;
127             rowId = 0;
128             checkCursor();
129             bc.beforeFirst();
130     } catch (IOException JavaDoc e) {
131         throw DbException.wrapThrowable(e);
132     }
133     }
134
135     public void afterLast() throws SQLException JavaDoc {
136     try {
137             rowValid = false;
138             rowId = 0;
139             checkCursor();
140             bc.afterLast();
141     } catch (IOException JavaDoc e) {
142         throw DbException.wrapThrowable(e);
143     }
144     }
145
146     public boolean next() throws SQLException JavaDoc {
147     try {
148             rowValid = false;
149             rowId = 0;
150             checkCursor();
151             boolean ret = bc.next();
152             if (ret) {
153                 rowId = getCurrentRowId();
154             }
155             return ret;
156     } catch (IOException JavaDoc e) {
157         throw DbException.wrapThrowable(e);
158     }
159     }
160
161     public boolean prev() throws SQLException JavaDoc {
162     try {
163             rowValid = false;
164             rowId = 0;
165             checkCursor();
166             boolean ret = bc.prev();
167             if (ret) rowId = getCurrentRowId();
168             return ret;
169     } catch (IOException JavaDoc e) {
170         throw DbException.wrapThrowable(e);
171     }
172     }
173
174     /**
175      * On close, we release the resources.
176      */

177     public void close() throws SQLException JavaDoc {
178     try {
179             if (bc != null) bc.release();
180         } finally {
181             bc = null;
182             row = null;
183         }
184     }
185
186     /**
187      * Return the row id
188      */

189     public long getRowId() {
190         return rowId;
191     }
192
193     /**
194      * Return the size of the index backing this cursor.
195      */

196     public long size() throws SQLException JavaDoc {
197         try {
198             return bc.size();
199         } catch (IOException JavaDoc e) {
200             throw DbException.wrapThrowable(e);
201         }
202     }
203     
204     public void updateRow(Row row) throws SQLException JavaDoc {
205         throw new SQLException JavaDoc("Cursor not updateable");
206     }
207
208     public void deleteRow() throws SQLException JavaDoc {
209         throw new SQLException JavaDoc("Cursor not updateable");
210     }
211 }
212
Popular Tags