Quadcap Embeddable Database

com/quadcap/sql/TernaryExpression.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.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 * The only ternary op in SQL is <b>BETWEEN</b> 00056 * 00057 * @author Stan Bailes 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 // The only ternary expression we have to deal with is "between", 00077 // so we know it's a scalar 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 }