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.util.Vector;
00047
00048
import java.sql.SQLException;
00049
00050
import com.quadcap.sql.types.Op;
00051
import com.quadcap.sql.types.Type;
00052
import com.quadcap.sql.types.TypeBoolean;
00053
import com.quadcap.sql.types.Value;
00054
import com.quadcap.sql.types.ValueBoolean;
00055
00056
import com.quadcap.util.Debug;
00057
00058
00059
00060
00061
00062
00063 public class QuantifiedCompare extends Expression implements
Externalizable {
00064 Expression e = null;
00065 int op = 0;
00066 int quant = 0;
00067 Expression q = null;
00068 boolean not =
false;
00069 ValueBoolean value = null;
00070
00071 public QuantifiedCompare() {}
00072
00073 public QuantifiedCompare(
Expression e,
int op,
int quant,
Expression q) {
00074
this.e = e;
00075
this.op = op;
00076
this.quant = quant;
00077
this.q = q;
00078 }
00079
00080 public int rank() {
return 1; }
00081
00082 public void invert() {
not = !
not; }
00083
00084 static boolean compare(
int op,
Row a,
Row b)
throws SQLException {
00085
boolean eq =
true;
00086
for (
int i = 1; eq && i <= a.size(); i++) {
00087
Value va = a.item(i);
00088
Value vb = b.item(i);
00089 eq =
Value.boolOp(
op, va, vb);
00090 }
00091
return eq;
00092 }
00093
00094 public Type getType(
Session session,
Cursor cursor) {
00095
return TypeBoolean.typeBoolean;
00096 }
00097
00098 public Value getValue(
Session session,
Cursor cursor)
00099
throws SQLException
00100 {
00101
Cursor qcursor =
q.
getCursor(session, cursor);
00102
try {
00103
Row row =
e.
getValues(session, cursor);
00104
boolean res =
false;
00105
00106
if (
quant ==
Op.ALL) {
00107 res =
true;
00108
while (res && qcursor.
next()) {
00109
Row crow = qcursor.
getRow();
00110 res = compare(
op, row, crow);
00111 }
00112 }
else if (
quant ==
Op.ANY) {
00113
while (!res && qcursor.
next()) {
00114
Row crow = qcursor.
getRow();
00115 res = compare(
op, row, crow);
00116 }
00117 }
else {
00118
throw new SQLException(
"bad quantifier: " +
quant,
"42000");
00119 }
00120
return new ValueBoolean(
not ^ res);
00121 } finally {
00122 qcursor.
close();
00123 }
00124 }
00125
00126 public void visitSubExpressions(
ExpressionVisitor ev) {
00127 ev.
visit(
e);
00128 ev.
visit(
q);
00129 }
00130
00131 public void readExternal(ObjectInput in)
00132
throws IOException, ClassNotFoundException
00133 {
00134
e = (
Expression)in.readObject();
00135
op = in.read();
00136
quant = in.read();
00137
q = (
Expression)in.readObject();
00138
not = in.read() == 1;
00139 }
00140
00141 public void writeExternal(ObjectOutput out)
throws IOException {
00142 out.writeObject(
e);
00143 out.write(
op);
00144 out.write(
quant);
00145 out.writeObject(
q);
00146 out.write(
not ? 1 : 0);
00147 }
00148
00149 public String
toString() {
00150 StringBuffer sb =
new StringBuffer(
e.
toString());
00151 sb.append(
' ');
00152 sb.append(
Op.toString(
op));
00153 sb.append(
' ');
00154 sb.append(
Op.toString(
quant));
00155 sb.append(
' ');
00156 sb.append(
q.
toString());
00157
return sb.toString();
00158 }
00159 }