KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.SQLException JavaDoc;
42
43 import com.quadcap.util.Debug;
44
45 /**
46  * Base cursor implementation class.
47  *
48  * @author Stan Bailes
49  */

50 abstract public class CursorImpl extends TupleImpl implements Cursor {
51     protected Session session = null;
52     protected Cursor outer;
53
54     /**
55      * Oh no, you don't...
56      */

57     private CursorImpl() {}
58
59     /**
60      * Construct a cursor from a session and a name
61      */

62     public CursorImpl(Session session, String JavaDoc name) {
63     super(name);
64     this.session = session;
65     }
66
67     /**
68      * Construct a cursor from a session, name, and outer cursor
69      */

70     public CursorImpl(Session session, String JavaDoc name, Cursor outer) {
71     super(name);
72     this.session = session;
73     this.outer = outer;
74     }
75
76     /**
77      * Return the cursor's session
78      */

79     public Session getSession() { return session; }
80
81     /**
82      * Derived class implements this function to return the current
83      * cursor row. Implementation required.
84      */

85     abstract public Row getRow() throws SQLException JavaDoc;
86
87     /**
88      * Handle insert
89      */

90     public void insertRow(Row row) throws SQLException JavaDoc {
91     throw new SQLException JavaDoc("Insert not allowed for this cursor type");
92     }
93     abstract public void updateRow(Row row) throws SQLException JavaDoc;
94     abstract public void deleteRow() throws SQLException JavaDoc;
95
96     abstract public void beforeFirst() throws SQLException JavaDoc;
97     abstract public void afterLast() throws SQLException JavaDoc;
98     abstract public boolean next() throws SQLException JavaDoc;
99
100     public boolean prev() throws SQLException JavaDoc {
101         throw new SQLException JavaDoc("prev() not supported for this cursor type");
102     }
103
104     abstract public boolean isWritable(int column) throws SQLException JavaDoc;
105     abstract public void close() throws SQLException JavaDoc;
106
107     public void finalize() throws Throwable JavaDoc {
108         try {
109             close();
110         } catch (Throwable JavaDoc t) {
111         }
112         super.finalize();
113     }
114     
115     public Column getColumn(String JavaDoc columnName) throws SQLException JavaDoc {
116     Column c = super.getColumn(columnName);
117     if (c == null && outer != null) {
118         c = outer.getColumn(columnName);
119     }
120     return c;
121     }
122
123     public void setOuterCursor(Cursor c) {
124     outer = c;
125     }
126
127     public Cursor getOuterCursor() {
128     return outer;
129     }
130
131     /**
132      * Position the cursor to the specified absolute row. The first row
133      * is row '1', and the last row is '-1', as in JDBC.
134      */

135     public boolean absolute(int row) throws SQLException JavaDoc {
136         if (row > 0) {
137             beforeFirst();
138             while (row-- > 0) if (!next()) return false;
139         } else {
140             afterLast();
141             while (row++ < 0) if (!prev()) return false;
142         }
143         return true;
144     }
145
146     public void reset(Expression expr, Cursor outer) throws SQLException JavaDoc {}
147     
148     public Table getTable() { return null; }
149     public long getRowId() { return 0; }
150
151     //#ifdef DEBUG
152
public String JavaDoc toString() {
153     try {
154         StringBuffer JavaDoc sb =
155                 new StringBuffer JavaDoc(Table.strip(getClass().getName()));
156         sb.append(": ");
157         sb.append(getName());
158         if (outer != null) {
159         sb.append(" (outer ");
160         sb.append(outer.toString()); // Table.strip(outer.getClass().getName()));
161
sb.append(")");
162         }
163             sb.append(" {");
164         for (int i = 1; i <= getColumnCount(); i++) {
165         Column c = getColumn(i);
166                 if (i > 1) sb.append(',');
167                 sb.append(c.getName());
168             }
169             sb.append('}');
170         return sb.toString();
171     } catch (Exception JavaDoc e) {
172         Debug.print(e);
173         return this.getClass().getName();
174     }
175     }
176
177     static void dumpCursor(String JavaDoc label, Cursor c) throws SQLException JavaDoc {
178         if (label.length() > 0) label = label + " ";
179         int count = 0;
180         c.beforeFirst();
181         while (c.next()) {
182             Debug.println(label + "Row " + (++count) + ": " + c.getRow());
183         }
184         c.beforeFirst();
185     }
186     //#endif
187
}
188
Popular Tags