1 28 29 package com.caucho.amber.expr; 30 31 import com.caucho.amber.query.FromItem; 32 import com.caucho.amber.query.QueryParseException; 33 import com.caucho.amber.query.QueryParser; 34 import com.caucho.util.CharBuffer; 35 36 import java.util.ArrayList ; 37 38 41 public class AndExpr extends AbstractAmberExpr { 42 private ArrayList <AmberExpr> _components = new ArrayList <AmberExpr>(); 43 44 47 public static AmberExpr create(AmberExpr left, AmberExpr right) 48 throws QueryParseException 49 { 50 if (left == null && right == null) 51 return null; 52 else if (left == null) 53 return right.createBoolean(); 54 else if (right == null) 55 return left.createBoolean(); 56 else if (left instanceof AndExpr) { 57 AndExpr and = (AndExpr) left; 58 and.add(right.createBoolean()); 59 return and; 60 } 61 else if (right instanceof AndExpr) { 62 AndExpr and = (AndExpr) right; 63 and.add(left.createBoolean()); 64 return and; 65 } 66 else { 67 AndExpr and = new AndExpr(); 68 and.add(left.createBoolean()); 69 and.add(right.createBoolean()); 70 71 return and; 72 } 73 } 74 77 public boolean isBoolean() 78 { 79 return true; 80 } 81 82 85 public void add(AmberExpr expr) 86 { 87 _components.add(expr); 88 } 89 90 93 public ArrayList <AmberExpr> getComponents() 94 { 95 return _components; 96 } 97 98 101 public AmberExpr bindSelect(QueryParser parser) 102 { 103 for (int i = 0; i < _components.size(); i++) { 104 AmberExpr expr = _components.get(i); 105 106 expr = expr.bindSelect(parser); 107 108 _components.set(i, expr); 109 } 110 111 return this; 112 } 113 114 117 public AmberExpr getSingle() 118 { 119 if (_components.size() == 0) 120 return null; 121 else if (_components.size() == 1) 122 return _components.get(0); 123 else 124 return this; 125 } 126 127 128 131 public boolean usesFrom(FromItem from, int type, boolean isNot) 132 { 133 if (type == IS_INNER_JOIN) { 134 for (int i = 0; i < _components.size(); i++) { 136 AmberExpr expr = _components.get(i); 137 138 if (! isNot && expr.usesFrom(from, type, isNot)) 139 return true; 140 else if (isNot && ! expr.usesFrom(from, type, isNot)) 141 return false; 142 } 143 144 return false; 145 } 146 else { 147 for (int i = 0; i < _components.size(); i++) { 148 AmberExpr expr = _components.get(i); 149 150 if (expr.usesFrom(from, type)) 151 return true; 152 } 153 154 return false; 155 } 156 } 157 158 161 @Override 162 public boolean exists(FromItem from) 163 { 164 for (int i = 0; i < _components.size(); i++) { 165 if (_components.get(i).exists(from)) 166 return true; 167 } 168 169 return false; 170 } 171 172 175 public AmberExpr replaceJoin(JoinExpr join) 176 { 177 for (int i = 0; i < _components.size(); i++) { 178 AmberExpr expr = _components.get(i); 179 180 expr = expr.replaceJoin(join); 181 182 _components.set(i, expr); 183 } 184 185 return this; 186 } 187 188 191 public void generateWhere(CharBuffer cb) 192 { 193 generateInternalWhere(cb, true); 194 } 195 196 199 public void generateUpdateWhere(CharBuffer cb) 200 { 201 generateInternalWhere(cb, false); 202 } 203 204 207 public void generateHaving(CharBuffer cb) 208 { 209 generateWhere(cb); 210 } 211 212 215 public void generateJoin(CharBuffer cb) 216 { 217 cb.append('('); 218 219 for (int i = 0; i < _components.size(); i++) { 220 if (i != 0) 221 cb.append(" and "); 222 223 AmberExpr expr = _components.get(i); 224 225 expr.generateJoin(cb); 226 } 227 228 cb.append(')'); 229 } 230 231 234 private void generateInternalWhere(CharBuffer cb, 235 boolean select) 236 { 237 cb.append('('); 238 239 for (int i = 0; i < _components.size(); i++) { 240 if (i != 0) 241 cb.append(" and "); 242 243 AmberExpr expr = _components.get(i); 244 245 if (select) 246 expr.generateWhere(cb); 247 else 248 expr.generateUpdateWhere(cb); 249 } 250 251 cb.append(')'); 252 } 253 } 254 | Popular Tags |