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.io.Externalizable;
042: import java.io.IOException;
043: import java.io.ObjectInput;
044: import java.io.ObjectOutput;
045:
046: import java.util.Vector;
047:
048: import java.sql.SQLException;
049:
050: import com.quadcap.sql.types.Op;
051: import com.quadcap.sql.types.Type;
052: import com.quadcap.sql.types.TypeBoolean;
053: import com.quadcap.sql.types.Value;
054: import com.quadcap.sql.types.ValueBoolean;
055:
056: import com.quadcap.util.Debug;
057:
058: /**
059: * Expression implemented quantified comparisons: <b>ALL</b>, <b>ANY</b>.
060: *
061: * @author Stan Bailes
062: */
063: public class QuantifiedCompare extends Expression implements
064: Externalizable {
065: Expression e = null;
066: int op = 0;
067: int quant = 0;
068: Expression q = null;
069: boolean not = false;
070: ValueBoolean value = null;
071:
072: public QuantifiedCompare() {
073: }
074:
075: public QuantifiedCompare(Expression e, int op, int quant,
076: Expression q) {
077: this .e = e;
078: this .op = op;
079: this .quant = quant;
080: this .q = q;
081: }
082:
083: public int rank() {
084: return 1;
085: }
086:
087: public void invert() {
088: not = !not;
089: }
090:
091: static boolean compare(int op, Row a, Row b) throws SQLException {
092: boolean eq = true;
093: for (int i = 1; eq && i <= a.size(); i++) {
094: Value va = a.item(i);
095: Value vb = b.item(i);
096: eq = Value.boolOp(op, va, vb);
097: }
098: return eq;
099: }
100:
101: public Type getType(Session session, Cursor cursor) {
102: return TypeBoolean.typeBoolean;
103: }
104:
105: public Value getValue(Session session, Cursor cursor)
106: throws SQLException {
107: Cursor qcursor = q.getCursor(session, cursor);
108: try {
109: Row row = e.getValues(session, cursor);
110: boolean res = false;
111:
112: if (quant == Op.ALL) {
113: res = true;
114: while (res && qcursor.next()) {
115: Row crow = qcursor.getRow();
116: res = compare(op, row, crow);
117: }
118: } else if (quant == Op.ANY) {
119: while (!res && qcursor.next()) {
120: Row crow = qcursor.getRow();
121: res = compare(op, row, crow);
122: }
123: } else {
124: throw new SQLException("bad quantifier: " + quant,
125: "42000");
126: }
127: return new ValueBoolean(not ^ res);
128: } finally {
129: qcursor.close();
130: }
131: }
132:
133: public void visitSubExpressions(ExpressionVisitor ev) {
134: ev.visit(e);
135: ev.visit(q);
136: }
137:
138: public void readExternal(ObjectInput in) throws IOException,
139: ClassNotFoundException {
140: e = (Expression) in.readObject();
141: op = in.read();
142: quant = in.read();
143: q = (Expression) in.readObject();
144: not = in.read() == 1;
145: }
146:
147: public void writeExternal(ObjectOutput out) throws IOException {
148: out.writeObject(e);
149: out.write(op);
150: out.write(quant);
151: out.writeObject(q);
152: out.write(not ? 1 : 0);
153: }
154:
155: public String toString() {
156: StringBuffer sb = new StringBuffer(e.toString());
157: sb.append(' ');
158: sb.append(Op.toString(op));
159: sb.append(' ');
160: sb.append(Op.toString(quant));
161: sb.append(' ');
162: sb.append(q.toString());
163: return sb.toString();
164: }
165: }
|