00001
package com.quadcap.sql;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.Externalizable;
00042
import java.io.IOException;
00043
import java.io.ObjectInput;
00044
import java.io.ObjectOutput;
00045
00046
import java.sql.SQLException;
00047
00048
import com.quadcap.sql.types.Op;
00049
import com.quadcap.sql.types.Type;
00050
import com.quadcap.sql.types.TypeBoolean;
00051
import com.quadcap.sql.types.Value;
00052
import com.quadcap.sql.types.ValueBoolean;
00053
00054
00055
00056
00057
00058
00059 public class TernaryExpression extends Expression implements
Externalizable {
00060 Expression e = null;
00061 Expression f = null;
00062 Expression g = null;
00063 int op = 0;
00064 boolean not =
false;
00065 ValueBoolean value = null;
00066
00067 public TernaryExpression() {}
00068
00069 public TernaryExpression(
int op,
Expression e,
Expression f,
Expression g) {
00070
this.op = op;
00071
this.e = e;
00072
this.f = f;
00073
this.g = g;
00074 }
00075
00076
00077
00078 public int rank() {
return 0; }
00079
00080 public Type getType(
Session session,
Cursor cursor) {
00081
return TypeBoolean.typeBoolean;
00082 }
00083
00084 public Value getValue(
Session session,
Cursor cursor)
throws SQLException {
00085
if (
op !=
Op.BETWEEN) {
00086
throw new SQLException(
"Bad op: " +
Op.toString(
op),
"42000");
00087 }
00088
Value ev =
e.
getValue(session, cursor);
00089
Value fv =
f.
getValue(session, cursor);
00090
Value gv =
g.
getValue(session, cursor);
00091
Value v =
Value.binop(
Op.AND,
00092
Value.binop(
Op.LE, fv, ev),
00093
Value.binop(
Op.LE, ev, gv));
00094
if (
not) {
00095 v = v.
unop(
Op.NOT);
00096 }
00097
return v;
00098 }
00099
00100 public void invert() {
not = !
not; }
00101
00102 public void visitSubExpressions(
ExpressionVisitor ev) {
00103 ev.
visit(
e);
00104 ev.
visit(
f);
00105 ev.
visit(
g);
00106 }
00107
00108 public void readExternal(ObjectInput in)
00109
throws IOException, ClassNotFoundException
00110 {
00111
e = (
Expression)in.readObject();
00112
f = (
Expression)in.readObject();
00113
g = (
Expression)in.readObject();
00114
op = in.readInt();
00115
not = (in.read() == 1);
00116 }
00117
00118 public void writeExternal(ObjectOutput out)
throws IOException {
00119 out.writeObject(
e);
00120 out.writeObject(
f);
00121 out.writeObject(
g);
00122 out.writeInt(
op);
00123 out.write(
not ? 1 : 0);
00124 }
00125
00126 public String
toString() {
00127 StringBuffer sb =
new StringBuffer();
00128
if (
not) sb.append(
"NOT ");
00129 sb.append(
"(");
00130 sb.append(
e.
toString());
00131 sb.append(
" BETWEEN ");
00132 sb.append(
f.
toString());
00133 sb.append(
" AND " );
00134 sb.append(
g.
toString());
00135 sb.append(
")");
00136
return sb.toString();
00137 }
00138 }