001: package com.quadcap.sql;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.sql.SQLException;
042:
043: import com.quadcap.util.Debug;
044:
045: /**
046: * Base cursor implementation class.
047: *
048: * @author Stan Bailes
049: */
050: abstract public class CursorImpl extends TupleImpl implements Cursor {
051: protected Session session = null;
052: protected Cursor outer;
053:
054: /**
055: * Oh no, you don't...
056: */
057: private CursorImpl() {
058: }
059:
060: /**
061: * Construct a cursor from a session and a name
062: */
063: public CursorImpl(Session session, String name) {
064: super (name);
065: this .session = session;
066: }
067:
068: /**
069: * Construct a cursor from a session, name, and outer cursor
070: */
071: public CursorImpl(Session session, String name, Cursor outer) {
072: super (name);
073: this .session = session;
074: this .outer = outer;
075: }
076:
077: /**
078: * Return the cursor's session
079: */
080: public Session getSession() {
081: return session;
082: }
083:
084: /**
085: * Derived class implements this function to return the current
086: * cursor row. Implementation required.
087: */
088: abstract public Row getRow() throws SQLException;
089:
090: /**
091: * Handle insert
092: */
093: public void insertRow(Row row) throws SQLException {
094: throw new SQLException(
095: "Insert not allowed for this cursor type");
096: }
097:
098: abstract public void updateRow(Row row) throws SQLException;
099:
100: abstract public void deleteRow() throws SQLException;
101:
102: abstract public void beforeFirst() throws SQLException;
103:
104: abstract public void afterLast() throws SQLException;
105:
106: abstract public boolean next() throws SQLException;
107:
108: public boolean prev() throws SQLException {
109: throw new SQLException(
110: "prev() not supported for this cursor type");
111: }
112:
113: abstract public boolean isWritable(int column) throws SQLException;
114:
115: abstract public void close() throws SQLException;
116:
117: public void finalize() throws Throwable {
118: try {
119: close();
120: } catch (Throwable t) {
121: }
122: super .finalize();
123: }
124:
125: public Column getColumn(String columnName) throws SQLException {
126: Column c = super .getColumn(columnName);
127: if (c == null && outer != null) {
128: c = outer.getColumn(columnName);
129: }
130: return c;
131: }
132:
133: public void setOuterCursor(Cursor c) {
134: outer = c;
135: }
136:
137: public Cursor getOuterCursor() {
138: return outer;
139: }
140:
141: /**
142: * Position the cursor to the specified absolute row. The first row
143: * is row '1', and the last row is '-1', as in JDBC.
144: */
145: public boolean absolute(int row) throws SQLException {
146: if (row > 0) {
147: beforeFirst();
148: while (row-- > 0)
149: if (!next())
150: return false;
151: } else {
152: afterLast();
153: while (row++ < 0)
154: if (!prev())
155: return false;
156: }
157: return true;
158: }
159:
160: public void reset(Expression expr, Cursor outer)
161: throws SQLException {
162: }
163:
164: public Table getTable() {
165: return null;
166: }
167:
168: public long getRowId() {
169: return 0;
170: }
171:
172: //#ifdef DEBUG
173: public String toString() {
174: try {
175: StringBuffer sb = new StringBuffer(Table.strip(getClass()
176: .getName()));
177: sb.append(": ");
178: sb.append(getName());
179: if (outer != null) {
180: sb.append(" (outer ");
181: sb.append(outer.toString()); // Table.strip(outer.getClass().getName()));
182: sb.append(")");
183: }
184: sb.append(" {");
185: for (int i = 1; i <= getColumnCount(); i++) {
186: Column c = getColumn(i);
187: if (i > 1)
188: sb.append(',');
189: sb.append(c.getName());
190: }
191: sb.append('}');
192: return sb.toString();
193: } catch (Exception e) {
194: Debug.print(e);
195: return this .getClass().getName();
196: }
197: }
198:
199: static void dumpCursor(String label, Cursor c) throws SQLException {
200: if (label.length() > 0)
201: label = label + " ";
202: int count = 0;
203: c.beforeFirst();
204: while (c.next()) {
205: Debug.println(label + "Row " + (++count) + ": "
206: + c.getRow());
207: }
208: c.beforeFirst();
209: }
210: //#endif
211: }
|