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.sql.SQLException;
047:
048: import com.quadcap.sql.types.Op;
049: import com.quadcap.sql.types.Type;
050: import com.quadcap.sql.types.TypeBoolean;
051: import com.quadcap.sql.types.Value;
052: import com.quadcap.sql.types.ValueBoolean;
053:
054: /**
055: * The only ternary op in SQL is <b>BETWEEN</b>
056: *
057: * @author Stan Bailes
058: */
059: public class TernaryExpression extends Expression implements
060: Externalizable {
061: Expression e = null;
062: Expression f = null;
063: Expression g = null;
064: int op = 0;
065: boolean not = false;
066: ValueBoolean value = null;
067:
068: public TernaryExpression() {
069: }
070:
071: public TernaryExpression(int op, Expression e, Expression f,
072: Expression g) {
073: this .op = op;
074: this .e = e;
075: this .f = f;
076: this .g = g;
077: }
078:
079: // The only ternary expression we have to deal with is "between",
080: // so we know it's a scalar
081: public int rank() {
082: return 0;
083: }
084:
085: public Type getType(Session session, Cursor cursor) {
086: return TypeBoolean.typeBoolean;
087: }
088:
089: public Value getValue(Session session, Cursor cursor)
090: throws SQLException {
091: if (op != Op.BETWEEN) {
092: throw new SQLException("Bad op: " + Op.toString(op),
093: "42000");
094: }
095: Value ev = e.getValue(session, cursor);
096: Value fv = f.getValue(session, cursor);
097: Value gv = g.getValue(session, cursor);
098: Value v = Value.binop(Op.AND, Value.binop(Op.LE, fv, ev), Value
099: .binop(Op.LE, ev, gv));
100: if (not) {
101: v = v.unop(Op.NOT);
102: }
103: return v;
104: }
105:
106: public void invert() {
107: not = !not;
108: }
109:
110: public void visitSubExpressions(ExpressionVisitor ev) {
111: ev.visit(e);
112: ev.visit(f);
113: ev.visit(g);
114: }
115:
116: public void readExternal(ObjectInput in) throws IOException,
117: ClassNotFoundException {
118: e = (Expression) in.readObject();
119: f = (Expression) in.readObject();
120: g = (Expression) in.readObject();
121: op = in.readInt();
122: not = (in.read() == 1);
123: }
124:
125: public void writeExternal(ObjectOutput out) throws IOException {
126: out.writeObject(e);
127: out.writeObject(f);
128: out.writeObject(g);
129: out.writeInt(op);
130: out.write(not ? 1 : 0);
131: }
132:
133: public String toString() {
134: StringBuffer sb = new StringBuffer();
135: if (not)
136: sb.append("NOT ");
137: sb.append("(");
138: sb.append(e.toString());
139: sb.append(" BETWEEN ");
140: sb.append(f.toString());
141: sb.append(" AND ");
142: sb.append(g.toString());
143: sb.append(")");
144: return sb.toString();
145: }
146: }
|