1 package com.quadcap.sql; 2 3 40 41 import java.io.Externalizable ; 42 import java.io.IOException ; 43 import java.io.ObjectInput ; 44 import java.io.ObjectOutput ; 45 46 import java.sql.SQLException ; 47 48 import com.quadcap.sql.index.BCursor; 49 import com.quadcap.sql.index.Btree; 50 51 import com.quadcap.sql.types.Op; 52 import com.quadcap.sql.types.Type; 53 import com.quadcap.sql.types.TypeBoolean; 54 import com.quadcap.sql.types.Value; 55 import com.quadcap.sql.types.ValueBoolean; 56 57 import com.quadcap.util.Debug; 58 59 64 public class UnaryExpression extends Expression implements Externalizable { 65 Expression e = null; 66 int op = -1; 67 boolean not = false; 68 Value value = null; 69 70 public UnaryExpression() {} 71 72 public UnaryExpression(int op, Expression e) { 73 this.op = op; 74 this.e = e; 75 } 76 77 public void invert() { 78 not = !not; 79 } 80 81 public int rank() { return 0; } 82 83 public Value uniqueValue(Session session, Cursor cursor) 84 throws SQLException , IOException 85 { 86 Cursor c = e.getCursor(session, cursor); 87 try { 88 Btree index = session.makeTempTree(); 89 try { 90 BCursor bc = index.getCursor(true); 91 try { 92 while (c.next()) { 93 Row row = c.getRow(); 94 byte[] key = Key.makeKey(null, row, null, 0, false); 95 if (bc.seek(key)) { 96 return ValueBoolean.trueBoolean; 97 } else { 98 bc.insert(key, key.length, key, 0, 1); 99 } 100 } 101 return ValueBoolean.falseBoolean; 102 } finally { 103 bc.release(); 104 } 105 } finally { 106 try { 107 index.free(); 108 } finally { 109 session.getDatabase().releaseTempFile(); 110 } 111 } 112 113 } finally { 114 c.close(); 115 } 116 } 117 118 public Value existsValue(Session session, Cursor cursor) 119 throws SQLException 120 { 121 Cursor c = e.getCursor(session, cursor); 122 try { 123 Value ret = new ValueBoolean(c.next()); 124 if (not) ret = ret.unop(Op.NOT); 125 return ret; 128 } finally { 129 c.close(); 130 } 131 } 132 133 public Type getType(Session session, Cursor cursor) throws SQLException { 134 switch (op) { 135 case Op.EXISTS: 136 case Op.UNIQUE: 137 return TypeBoolean.typeBoolean; 138 default: 139 return e.getType(session, cursor); 140 } 141 } 142 143 public Value getValue(Session session, Cursor cursor) throws SQLException { 144 switch (op) { 145 case Op.EXISTS: 146 return existsValue(session, cursor); 147 case Op.UNIQUE: 148 try { 149 return uniqueValue(session, cursor); 150 } catch (IOException ex) { 151 throw DbException.wrapThrowable(ex); 152 } 153 default: 154 Value v = e.getValue(session, cursor); 155 value = v.unop(op); 156 if (not) { 157 value = value.unop(Op.NOT); 158 } 159 return value; 160 } 161 } 162 163 public void visitSubExpressions(ExpressionVisitor ev) { 164 ev.visit(e); 165 } 166 167 public String toString() { 168 String n = not ? "NOT " : ""; 169 return n + Op.toString(op) + " " + e.toString(); 170 } 171 172 public void readExternal(ObjectInput in) 173 throws IOException , ClassNotFoundException 174 { 175 e = (Expression)in.readObject(); 176 op = in.readInt(); 177 not = in.read() == 1; 178 value = (Value)in.readObject(); 179 } 180 181 public void writeExternal(ObjectOutput out) throws IOException { 182 out.writeObject(e); 183 out.writeInt(op); 184 out.write(not ? 1 : 0); 185 out.writeObject(value); 186 } 187 } 188 | Popular Tags |