KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.SQLException JavaDoc;
47
48 import com.quadcap.sql.types.Op;
49 import com.quadcap.sql.types.Type;
50 import com.quadcap.sql.types.TypeBoolean;
51 import com.quadcap.sql.types.Value;
52 import com.quadcap.sql.types.ValueBoolean;
53
54 /**
55  * The only ternary op in SQL is <b>BETWEEN</b>
56  *
57  * @author Stan Bailes
58  */

59 public class TernaryExpression extends Expression implements Externalizable JavaDoc {
60     Expression e = null;
61     Expression f = null;
62     Expression g = null;
63     int op = 0;
64     boolean not = false;
65     ValueBoolean value = null;
66
67     public TernaryExpression() {}
68
69     public TernaryExpression(int op, Expression e, Expression f, Expression g) {
70     this.op = op;
71     this.e = e;
72     this.f = f;
73     this.g = g;
74     }
75
76     // The only ternary expression we have to deal with is "between",
77
// so we know it's a scalar
78
public int rank() { return 0; }
79
80     public Type getType(Session session, Cursor cursor) {
81         return TypeBoolean.typeBoolean;
82     }
83
84     public Value getValue(Session session, Cursor cursor) throws SQLException JavaDoc {
85     if (op != Op.BETWEEN) {
86         throw new SQLException JavaDoc("Bad op: " + Op.toString(op), "42000");
87     }
88     Value ev = e.getValue(session, cursor);
89     Value fv = f.getValue(session, cursor);
90     Value gv = g.getValue(session, cursor);
91     Value v = Value.binop(Op.AND,
92                Value.binop(Op.LE, fv, ev),
93                Value.binop(Op.LE, ev, gv));
94     if (not) {
95         v = v.unop(Op.NOT);
96     }
97     return v;
98     }
99
100     public void invert() { not = !not; }
101
102     public void visitSubExpressions(ExpressionVisitor ev) {
103     ev.visit(e);
104     ev.visit(f);
105     ev.visit(g);
106     }
107
108     public void readExternal(ObjectInput JavaDoc in)
109     throws IOException JavaDoc, ClassNotFoundException JavaDoc
110     {
111     e = (Expression)in.readObject();
112     f = (Expression)in.readObject();
113     g = (Expression)in.readObject();
114     op = in.readInt();
115     not = (in.read() == 1);
116     }
117     
118     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
119     out.writeObject(e);
120     out.writeObject(f);
121     out.writeObject(g);
122     out.writeInt(op);
123     out.write(not ? 1 : 0);
124     }
125
126     public String JavaDoc toString() {
127     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
128     if (not) sb.append("NOT ");
129     sb.append("(");
130     sb.append(e.toString());
131     sb.append(" BETWEEN ");
132     sb.append(f.toString());
133     sb.append(" AND " );
134     sb.append(g.toString());
135     sb.append(")");
136     return sb.toString();
137     }
138 }
139
Popular Tags