Quadcap Embeddable Database

com/quadcap/sql/QuantifiedCompare.java

Go to the documentation of this file.
00001 package com.quadcap.sql; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 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 * Expression implemented quantified comparisons: <b>ALL</b>, <b>ANY</b>. 00060 * 00061 * @author Stan Bailes 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 }