KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Externalizable JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.ObjectInput JavaDoc;
44 import java.io.ObjectOutput JavaDoc;
45
46 import java.util.Vector JavaDoc;
47
48 import java.sql.SQLException JavaDoc;
49
50 import com.quadcap.sql.types.Op;
51 import com.quadcap.sql.types.Type;
52 import com.quadcap.sql.types.TypeBoolean;
53 import com.quadcap.sql.types.Value;
54 import com.quadcap.sql.types.ValueBoolean;
55
56 import com.quadcap.util.Debug;
57
58 /**
59  * Expression implemented quantified comparisons: <b>ALL</b>, <b>ANY</b>.
60  *
61  * @author Stan Bailes
62  */

63 public class QuantifiedCompare extends Expression implements Externalizable JavaDoc {
64     Expression e = null;
65     int op = 0;
66     int quant = 0;
67     Expression q = null;
68     boolean not = false;
69     ValueBoolean value = null;
70
71     public QuantifiedCompare() {}
72     
73     public QuantifiedCompare(Expression e, int op, int quant, Expression q) {
74     this.e = e;
75     this.op = op;
76     this.quant = quant;
77     this.q = q;
78     }
79
80     public int rank() { return 1; }
81
82     public void invert() { not = !not; }
83
84     static boolean compare(int op, Row a, Row b) throws SQLException JavaDoc {
85         boolean eq = true;
86     for (int i = 1; eq && i <= a.size(); i++) {
87         Value va = a.item(i);
88         Value vb = b.item(i);
89         eq = Value.boolOp(op, va, vb);
90         }
91         return eq;
92     }
93
94     public Type getType(Session session, Cursor cursor) {
95         return TypeBoolean.typeBoolean;
96     }
97
98     public Value getValue(Session session, Cursor cursor)
99         throws SQLException JavaDoc
100     {
101     Cursor qcursor = q.getCursor(session, cursor);
102         try {
103             Row row = e.getValues(session, cursor);
104             boolean res = false;
105
106             if (quant == Op.ALL) {
107                 res = true;
108                 while (res && qcursor.next()) {
109                     Row crow = qcursor.getRow();
110                     res = compare(op, row, crow);
111                 }
112             } else if (quant == Op.ANY) {
113                 while (!res && qcursor.next()) {
114                     Row crow = qcursor.getRow();
115                     res = compare(op, row, crow);
116                 }
117             } else {
118                 throw new SQLException JavaDoc("bad quantifier: " + quant, "42000");
119             }
120             return new ValueBoolean(not ^ res);
121         } finally {
122             qcursor.close();
123         }
124     }
125
126     public void visitSubExpressions(ExpressionVisitor ev) {
127     ev.visit(e);
128     ev.visit(q);
129     }
130
131     public void readExternal(ObjectInput JavaDoc in)
132     throws IOException JavaDoc, ClassNotFoundException JavaDoc
133     {
134     e = (Expression)in.readObject();
135     op = in.read();
136     quant = in.read();
137     q = (Expression)in.readObject();
138     not = in.read() == 1;
139     }
140     
141     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
142     out.writeObject(e);
143     out.write(op);
144     out.write(quant);
145     out.writeObject(q);
146     out.write(not ? 1 : 0);
147     }
148
149     public String JavaDoc toString() {
150         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(e.toString());
151         sb.append(' ');
152         sb.append(Op.toString(op));
153         sb.append(' ');
154         sb.append(Op.toString(quant));
155         sb.append(' ');
156         sb.append(q.toString());
157         return sb.toString();
158     }
159 }
160
Popular Tags