1 2 12 package com.versant.core.jdbc.sql.exp; 13 14 import com.versant.core.util.CharBuf; 15 import com.versant.core.jdbc.sql.SqlDriver; 16 import com.versant.core.jdo.query.ParamNode; 17 import com.versant.core.common.Debug; 18 19 import java.util.Map ; 20 import java.util.HashMap ; 21 22 import com.versant.core.common.BindingSupportImpl; 23 24 27 public class SqlExp { 28 29 30 public static final int NO = 0; 31 32 public static final int YES = 1; 33 34 public static final int YES_DISTINCT = 2; 35 36 public static final int YES_DISTINCT_NOT = 3; 37 38 42 public SqlExp next; 43 46 public SqlExp childList; 47 48 private int preFirstCharIndex; 49 private int lastCharIndex; 50 51 public SqlExp() { 52 } 53 54 public SqlExp createInstance() { 55 return new SqlExp(); 56 } 57 58 63 public static SqlExp createClone(SqlExp sqlExp) { 64 return createClone(sqlExp, new HashMap ()); 65 } 66 67 74 public static SqlExp createClone(SqlExp inst, Map cloneMap) { 75 if (inst == null) return null; 76 if (cloneMap.containsKey(inst)) { 77 return (SqlExp) cloneMap.get(inst); 78 } else { 79 cloneMap.put(inst, inst.createInstance()); 80 } 81 return inst.getClone((SqlExp) cloneMap.get(inst), cloneMap); 82 } 83 84 92 public SqlExp getClone(SqlExp clone, Map cloneMap) { 93 if (next != null) clone.next = createClone(next, cloneMap); 94 if (childList != null) clone.childList = createClone(childList, cloneMap); 95 clone.preFirstCharIndex = preFirstCharIndex; 96 clone.lastCharIndex = lastCharIndex; 97 return clone; 98 } 99 100 public SqlExp(SqlExp childList) { 101 this.childList = childList; 102 } 103 104 public String toString() { 105 String n = getClass().getName(); 106 int i = n.lastIndexOf('.'); 107 if (i >= 0) n = n.substring(i + 1); 108 return n + "@" + Integer.toHexString(System.identityHashCode(this)); 109 } 110 111 114 public void dump(String indent) { 115 Debug.OUT.println(indent + this); 116 if (childList != null) childList.dumpList(indent + " "); 117 } 118 119 122 public void dumpList(String indent) { 123 dump(indent); 124 for (SqlExp e = next; e != null; e = e.next) e.dump(indent); 125 } 126 127 130 public int createAlias(int index) { 131 for (SqlExp e = childList; e != null; e = e.next) { 132 index = e.createAlias(index); 133 } 134 return index; 135 } 136 137 141 public void replaceSelectExpRef(SelectExp old, SelectExp nw) { 142 for (SqlExp e = childList; e != null; e = e.next) { 143 e.replaceSelectExpRef(old, nw); 144 } 145 } 146 147 152 public SqlExp normalize(SqlDriver driver, SelectExp sel, boolean convertExists) { 153 SqlExp p = null; 154 for (SqlExp e = childList; e != null; e = e.next) { 155 SqlExp r = e.normalize(driver, sel, convertExists); 156 if (r == null) { 157 p = e; 158 } else { 159 if (p == null) 160 childList = r; 161 else 162 p.next = r; 163 r.next = e.next; 164 } 165 } 166 return null; 167 } 168 169 176 public final void appendSQL(SqlDriver driver, CharBuf s, 177 SqlExp leftSibling) { 178 preFirstCharIndex = s.size(); 179 appendSQLImp(driver, s, leftSibling); 180 lastCharIndex = s.size(); 181 } 182 183 191 protected void appendSQLImp(SqlDriver driver, CharBuf s, 192 SqlExp leftSibling) { 193 } 194 195 199 public boolean requiresParensInAnd() { 200 return false; 201 } 202 203 207 public boolean requiresParensInMultiply() { 208 return false; 209 } 210 211 215 public final int getPreFirstCharIndex() { 216 return preFirstCharIndex; 217 } 218 219 223 public int getFirstCharIndex() { 224 throw BindingSupportImpl.getInstance().internal("getFirstCharIndex called on " + 225 this); 226 } 227 228 232 public final int getLastCharIndex() { 233 return lastCharIndex; 234 } 235 236 240 public boolean isNegative() { 241 return false; 242 } 243 244 247 public int getJdbcType() { 248 return 0; 249 } 250 251 254 public int getJavaTypeCode() { 255 return 0; 256 } 257 258 262 public int getClassIndex() { 263 return -1; 264 } 265 266 274 public int getConvertToJoin() { 275 return NO; 276 } 277 278 281 public void append(SqlExp extra) { 282 if (childList == null) { 283 childList = extra; 284 } else { 285 SqlExp e; 286 for (e = childList; e.next != null; e = e.next) ; 287 e.next = extra; 288 } 289 } 290 291 297 public static SqlExp appendWithAnd(SqlExp base, SqlExp e) { 298 if (e == null) return base; 299 if (base == null) { 300 if (e.next == null) { 301 base = e; 302 } else { 303 base = new AndExp(e); 304 } 305 } else if (base instanceof AndExp) { 306 base.append(e); 307 } else { 308 base.next = e; 309 base = new AndExp(base); 310 } 311 return base; 312 } 313 314 320 public void setOuter(boolean on) { 321 } 322 323 330 public SelectExp getSingleSelectExp(SelectExp exclude) { 331 return null; 332 } 333 334 340 public static SqlExp createBinaryOpExp(SqlExp left, int op, SqlExp right) { 341 if (left.next == null && right.next == null) { 342 BinaryOpExp ans = new BinaryOpExp(left, op, right); 343 if (right instanceof ParamExp) { 344 ParamExp p = (ParamExp)right; 345 p.usage.expList = ans; 346 if (left instanceof ColumnExp) p.usage.expCount = 1; 347 } 348 return ans; 349 } else { 350 SqlExp l = left.next; 351 SqlExp r = right.next; 352 SqlExp list = new BinaryOpExp(left, op, right); 353 AndExp ans = new AndExp(list); 354 ParamExp pe; 355 if (right instanceof ParamExp) { 356 pe = (ParamExp)right; 357 pe.usage.expList = list; 358 } else { 359 pe = null; 360 } 361 boolean colExp = left instanceof ColumnExp; 362 int c = 1; 363 for (; l != null && r != null; c++) { 364 left = l; 365 right = r; 366 l = left.next; 367 r = right.next; 368 BinaryOpExp e = new BinaryOpExp(left, op, right); 369 list = list.next = e; 370 } 371 if (l != null || r != null) { 372 throw BindingSupportImpl.getInstance().internal( 373 "left and right lists have different length"); 374 } 375 if (pe != null && colExp) pe.usage.expCount = c; 376 return ans; 377 } 378 } 379 380 } 381 | Popular Tags |