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 java.util.ArrayList;
044:
045: import com.quadcap.util.Debug;
046:
047: /**
048: * Cursor implementation that concatenates multiple cursors together and
049: * makes them appear as a single cursor.
050: *
051: * @author Stan Bailes
052: */
053: public class MultiCursor extends FilterCursor {
054: ArrayList cursors = new ArrayList();
055: int cursorNum = 0;
056:
057: public MultiCursor(Session session) throws SQLException {
058: super (session);
059: }
060:
061: public MultiCursor(Session session, Cursor cursor)
062: throws SQLException {
063: super (session, cursor);
064: appendCursor(session, cursor);
065: }
066:
067: public void appendCursor(Session session, Cursor cursor)
068: throws SQLException {
069: cursors.add(cursor);
070: if (this .cursor == null) {
071: this .cursor = cursor;
072: addColumns(session, cursor);
073: }
074: }
075:
076: public void beforeFirst() throws SQLException {
077: for (int i = 0; i < cursors.size(); i++) {
078: Cursor c = ((Cursor) cursors.get(i));
079: c.beforeFirst();
080: if (i == 0)
081: cursor = c;
082: }
083: cursorNum = 0;
084: }
085:
086: public boolean next() throws SQLException {
087: boolean ret = cursor.next();
088: while (!ret && ++cursorNum < cursors.size()) {
089: cursor = (Cursor) cursors.get(cursorNum);
090: cursor.beforeFirst();
091: ret = cursor.next();
092: }
093: return ret;
094: }
095:
096: public boolean prev() throws SQLException {
097: throw new SQLException("MultiCursor.prev() not implemented");
098: }
099:
100: public void close() throws SQLException {
101: for (int i = 0; i < cursors.size(); i++) {
102: try {
103: ((Cursor) cursors.get(i)).close();
104: } catch (Throwable t) {
105: }
106: }
107: }
108: }
|