| 1 package com.quadcap.sql; 2 3 40 41 import java.util.Vector ; 42 43 import java.sql.SQLException ; 44 45 import com.quadcap.sql.types.Op; 46 47 import com.quadcap.util.Debug; 48 49 54 public class Analyze { 55 Session session; 56 Expression expr; 57 BinaryExpression b = null; 58 NameExpression n = null; 59 ValueExpression v = null; 60 String prefix = ""; 62 64 public Analyze(Session session, Expression expr) { 65 this.session = session; 66 this.expr = expr; 67 if (expr instanceof BinaryExpression) b = (BinaryExpression)expr; 68 if (expr instanceof NameExpression) n = (NameExpression)expr; 69 if (expr instanceof ValueExpression) v = (ValueExpression)expr; 70 } 71 72 public void getConjunctives(Vector v) { 73 if (b != null) { 74 if (b.not) { 75 v.addElement(expr); 77 } else { 78 if (b.op == Op.AND) { 79 analyze(b.e).getConjunctives(v); 80 analyze(b.f).getConjunctives(v); 81 } else { 82 v.addElement(expr); 83 } 84 } 85 } else { 86 v.addElement(expr); 87 } 88 } 89 90 public boolean isConstant() { 91 return expr instanceof ValueExpression; 92 } 93 94 public boolean isName() { 95 return n != null; 96 } 97 98 public boolean refersTo(Tuple table) throws SQLException { 99 boolean ret = false; 100 if (n != null) { 101 if (table.getColumn(n.name) != null) ret = true; 102 } 103 return ret; 104 } 105 106 public Analyze analyze(Expression e) { 107 Analyze z = new Analyze(session, e); 108 z.prefix = prefix + " "; 110 return z; 112 } 113 114 public boolean isConstantCompareToTable(Tuple table) throws SQLException { 115 boolean ret = false; 116 if (b != null) { 117 Analyze be = analyze(b.e); 118 Analyze bf = analyze(b.f); 119 if (be.isConstant()) { 120 if (bf.refersTo(table)) { 121 ret = true; 122 } 123 } else if (bf.isConstant()) { 124 if (be.refersTo(table)) { 125 ret = true; 126 } 127 } 128 } 129 return ret; 132 } 133 134 public boolean isTermOrConstant(Tuple table) throws SQLException { 135 boolean ret = false; 136 if (n != null) { 137 ret = refersTo(table); 138 } else if (b != null) { 139 Analyze be = analyze(b.e); 140 Analyze bf = analyze(b.f); 141 ret = be.isTermOrConstant(table) && bf.isTermOrConstant(table); 142 } else if (v != null) { 143 ret = true; 144 } 145 return ret; 146 } 147 148 public int[][] getJoinColumns(Cursor ca, Cursor cb) throws SQLException { 149 Vector vec = new Vector (); 150 Vector aCols = new Vector (); 151 Vector bCols = new Vector (); 152 int cnt = 0; 153 int[][] ret = null; 154 getConjunctives(vec); 155 for (int i = 0; i < vec.size(); i++) { 156 Expression ei = (Expression)vec.elementAt(i); 157 if (analyze(ei).isJoinColumn(ca, aCols, cb, bCols)) { 158 cnt++; 159 } 160 } 161 if (cnt > 0) { 162 ret = new int[2][cnt]; 163 for (int i = 0; i < cnt; i++) { 164 ret[0][i] = ((Column)aCols.elementAt(i)).getColumn(); 165 ret[1][i] = ((Column)bCols.elementAt(i)).getColumn(); 166 } 167 } 168 return ret; 169 } 170 171 boolean isJoinColumn(Cursor ca, Vector aCols, Cursor cb, Vector bCols) 172 throws SQLException 173 { 174 if (b == null) return false; 175 if (b.op != Op.EQ) return false; 176 if (!(b.e instanceof NameExpression)) return false; 177 String ename = ((NameExpression)b.e).name; 178 if (!(b.f instanceof NameExpression)) return false; 179 String fname = ((NameExpression)b.f).name; 180 Column cea = ca.getColumn(ename); 181 if (cea != null) { 182 Column cfb = cb.getColumn(fname); 183 if (cfb != null) { 184 aCols.addElement(cea); 185 bCols.addElement(cfb); 186 return true; 187 } 188 } 189 Column ceb = cb.getColumn(ename); 190 if (ceb != null) { 191 Column cfa = ca.getColumn(fname); 192 if (cfa != null) { 193 aCols.addElement(cfa); 194 bCols.addElement(ceb); 195 return true; 196 } 197 } 198 return false; 199 } 200 201 public Expression factorTable(Tuple table) throws SQLException { 202 Expression ret = null; 203 if (table != null) { 204 Vector vec = new Vector (); 205 getConjunctives(vec); 206 for (int i = 0; i < vec.size(); i++) { 207 Expression ei = (Expression)vec.elementAt(i); 208 if (analyze(ei).isTermOrConstant(table)) { 209 if (ret == null) { 210 ret = ei; 211 } else { 212 ret = new BinaryExpression(Op.AND, ret, ei); 213 } 214 } 215 } 216 } 217 return ret; 221 222 } 223 224 public boolean isJoinExpression(Tuple c) throws SQLException { 225 boolean ret = false; 226 if (isConstant()) { 227 ret = true; 228 } else if (n != null) { 229 if (c.getColumn(n.name) != null) ret = true; 230 } else if (b != null) { 231 if (analyze(b.e).isJoinExpression(c) && 232 analyze(b.f).isJoinExpression(c)) ret = true; 233 } 234 return ret; 235 } 236 237 public Expression factorJoinExpression(Tuple c) throws SQLException { 238 Expression ret = null; 239 Vector vec = new Vector (); 240 getConjunctives(vec); 241 for (int i = 0; i < vec.size(); i++) { 242 Expression ei = (Expression)vec.elementAt(i); 243 if (analyze(ei).isJoinExpression(c)) { 244 if (ret == null) { 245 ret = ei; 246 } else { 247 ret = new BinaryExpression(Op.AND, ret, ei); 248 } 249 } 250 } 251 return ret; 252 } 253 254 public String toString() { 256 return prefix + "Analyze[" + expr + "]"; 257 } 258 } 260 | Popular Tags |