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.util.Vector;
042:
043: import java.sql.SQLException;
044:
045: import com.quadcap.sql.types.Value;
046: import com.quadcap.sql.types.ValueBoolean;
047: import com.quadcap.sql.types.ValueUnknown;
048:
049: import com.quadcap.util.Debug;
050:
051: /**
052: * Cursor for <b>WHERE</b> predicates.
053: *
054: * @author Stan Bailes
055: */
056: public class PredicateCursor extends FilterCursor {
057: Expression predicate = null;
058: Row row = null;
059:
060: /**
061: * A PredicateCursor is a kind of FilterCursor which only returns
062: * rows which match a predicate
063: */
064: public PredicateCursor(Session session, Cursor cursor,
065: Expression predicate) throws SQLException {
066: super (session, cursor);
067: this .predicate = predicate;
068: }
069:
070: /**
071: * Return the current cursor row
072: */
073: public Row getRow() throws SQLException {
074: return row;
075: }
076:
077: /**
078: * Cursor.next() Advance the cursor and return 'true' if the cursor
079: * advances past a valid item. This item becomes the current row.
080: */
081: public boolean next() throws SQLException {
082: row = null;
083: while (row == null && cursor.next()) {
084: Value val = predicate.getValue(session, cursor);
085: //Debug.println("[" + predicate + "] on " + cursor.getRow() + " " +
086: // (outer == null ? "{}" : outer.getRow().toString()) + " = " + val);
087: if (Value.isTrue(val)) {
088: row = cursor.getRow();
089: }
090: }
091: return row != null;
092: }
093:
094: /**
095: * Cursor.next() Retreat the cursor and return 'true' if the cursor
096: * moves past a valid item. This item becomes the current row.
097: */
098: public boolean prev() throws SQLException {
099: row = null;
100: while (row == null && cursor.prev()) {
101: Value val = predicate.getValue(session, cursor);
102: if (Value.isTrue(val)) {
103: row = cursor.getRow();
104: }
105: }
106: return row != null;
107: }
108:
109: /**
110: * Position the cursor to the specified absolute row. The first row
111: * is row '1', and the last row is '-1', as in JDBC.
112: */
113: public boolean absolute(int row) throws SQLException {
114: if (row > 0) {
115: beforeFirst();
116: while (row-- > 0)
117: if (!next())
118: return false;
119: } else {
120: afterLast();
121: while (row++ < 0)
122: if (!prev())
123: return false;
124: }
125: return true;
126: }
127:
128: //#ifdef DEBUG
129: public String toString() {
130: return "PredicateCursor: " + super .toString();
131: }
132: //#endif
133:
134: }
|