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 OrExpr extends AbstractAmberExpr { 42 private ArrayList <AmberExpr> _components = new ArrayList <AmberExpr>(); 43 44 47 public boolean isBoolean() 48 { 49 return true; 50 } 51 52 55 public void add(AmberExpr expr) 56 throws QueryParseException 57 { 58 _components.add(expr.createBoolean()); 59 } 60 61 64 public AmberExpr bindSelect(QueryParser parser) 65 { 66 for (int i = 0; i < _components.size(); i++) { 67 AmberExpr expr = _components.get(i); 68 69 expr = expr.bindSelect(parser); 70 71 _components.set(i, expr); 72 } 73 74 return this; 75 } 76 77 80 public boolean usesFrom(FromItem from, int type, boolean isNot) 81 { 82 if (type == IS_INNER_JOIN) { 83 for (int i = 0; i < _components.size(); i++) { 85 AmberExpr expr = _components.get(i); 86 87 if (! isNot && ! expr.usesFrom(from, type, isNot)) 88 return false; 89 else if (isNot && expr.usesFrom(from, type, isNot)) 90 return true; 91 } 92 93 return false; 94 } 95 else { 96 for (int i = 0; i < _components.size(); i++) { 97 AmberExpr expr = _components.get(i); 98 99 if (expr.usesFrom(from, type)) 100 return true; 101 } 102 103 return false; 104 } 105 } 106 107 110 public AmberExpr replaceJoin(JoinExpr join) 111 { 112 for (int i = 0; i < _components.size(); i++) { 113 AmberExpr expr = _components.get(i); 114 115 expr = expr.replaceJoin(join); 116 117 _components.set(i, expr); 118 } 119 120 return this; 121 } 122 123 126 AmberExpr getSingle() 127 { 128 if (_components.size() == 0) 129 return null; 130 else if (_components.size() == 1) 131 return _components.get(0); 132 else 133 return this; 134 } 135 136 139 public void generateWhere(CharBuffer cb) 140 { 141 generateInternalWhere(cb, true); 142 } 143 144 147 public void generateUpdateWhere(CharBuffer cb) 148 { 149 generateInternalWhere(cb, false); 150 } 151 152 155 public void generateHaving(CharBuffer cb) 156 { 157 generateWhere(cb); 158 } 159 160 163 public void generateJoin(CharBuffer cb) 164 { 165 cb.append('('); 166 167 for (int i = 0; i < _components.size(); i++) { 168 if (i != 0) 169 cb.append(" OR "); 170 171 AmberExpr expr = _components.get(i); 172 173 expr.generateJoin(cb); 174 } 175 176 cb.append(')'); 177 } 178 179 public String toString() 180 { 181 if (_components.size() == 1) { 182 return _components.get(0).toString(); 183 } 184 else { 185 CharBuffer cb = new CharBuffer(); 186 187 cb.append('('); 188 189 for (int i = 0; i < _components.size(); i++) { 190 if (i != 0) 191 cb.append(" OR "); 192 193 AmberExpr expr = _components.get(i); 194 195 cb.append(expr); 196 } 197 198 cb.append(')'); 199 200 return cb.toString(); 201 } 202 } 203 204 207 private void generateInternalWhere(CharBuffer cb, 208 boolean select) 209 { 210 cb.append('('); 211 212 for (int i = 0; i < _components.size(); i++) { 213 if (i != 0) 214 cb.append(" OR "); 215 216 AmberExpr expr = _components.get(i); 217 218 if (select) 219 expr.generateWhere(cb); 220 else 221 expr.generateUpdateWhere(cb); 222 } 223 224 cb.append(')'); 225 } 226 } 227 | Popular Tags |