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.util.Vector ; 47 48 import java.sql.SQLException ; 49 50 import antlr.RecognitionException; 51 52 import com.quadcap.sql.types.Value; 53 54 import com.quadcap.util.Debug; 55 56 62 public class VectorExpression extends TableExpression 63 implements Externalizable 64 { 65 Vector expressions = new Vector (); 66 int rank = 1; 67 68 public VectorExpression() {} 69 70 public void addElement(Expression expression) throws RecognitionException { 71 if (expressions.size() == 0) { 72 rank = expression.rank() + 1; 74 } else { 75 if (expression.rank() != rank-1) { 76 throw new RecognitionException( 77 "Mismatched ranks in table expression"); 78 } 79 } 80 expressions.addElement(expression); 81 } 82 83 public int rank() { return rank; } 84 85 public boolean isUpdatable() { return false; } 86 87 public void getBaseTables(Vector v) {} 88 89 public int size() { return expressions.size(); } 90 91 public final Expression get(int i) { 92 return (Expression)expressions.get(i); 93 } 94 95 public Row getValues(Session session, Cursor cursor) throws SQLException { 96 if (rank == 1) { 97 Row values = new Row(); 98 for (int i = 0; i < expressions.size(); i++) { 99 Expression e = (Expression)expressions.elementAt(i); 100 values.addElement(e.getValue(session, cursor)); 101 } 102 return values; 103 } else if (rank == 2) { 104 Expression e = (Expression)expressions.elementAt(0); 105 return e.getValues(session, cursor); 106 } 107 return null; 108 } 109 110 public Cursor getCursor(Session session, Cursor cursor) 111 throws SQLException 112 { 113 if (rank == 1) { 114 Vector v = new Vector (); 115 v.addElement(getValues(session, cursor)); 116 return new StaticCursor(session, v); 117 } else if (rank == 2) { 118 Vector cursorVec = new Vector (); 119 for (int i = 0; i < expressions.size(); i++) { 120 Expression e = (Expression)expressions.elementAt(i); 121 cursorVec.addElement(e.getValues(session, cursor)); 122 } 123 return new StaticCursor(session, cursorVec); 124 } else { 125 return null; 126 } 127 } 128 129 public void invert() { 130 throw new RuntimeException ("Can't invert vector expression"); 131 } 132 133 public void visitSubExpressions(ExpressionVisitor ev) { 134 for (int i = 0; i < expressions.size(); i++) { 135 Expression ex = (Expression)expressions.elementAt(i); 136 ev.visit(ex); 137 } 138 } 139 140 public String toString() { 141 StringBuffer sb = new StringBuffer (); 142 for (int i = 0; i < expressions.size(); i++) { 143 if (i > 0) sb.append(", "); 144 sb.append(expressions.elementAt(i).toString()); 145 } 146 return sb.toString(); 147 } 148 149 public void readExternal(ObjectInput in) 150 throws IOException , ClassNotFoundException 151 { 152 expressions = (Vector )in.readObject(); 153 } 154 155 public void writeExternal(ObjectOutput out) throws IOException { 156 out.writeObject(expressions); 157 } 158 159 163 public Value getParameter(Session session, int pos) { 164 try { 165 if (rank == 2) { 166 VectorExpression v = (VectorExpression)expressions.get(0); 167 Expression e = v.get(pos-1); 168 return e.getValue(session, null); 169 } 170 } catch (Throwable t) {} 171 return null; 172 } 173 174 public String name() { return "[]"; } 176 } 178 | Popular Tags |