1 28 29 package com.caucho.amber.expr; 30 31 import com.caucho.amber.query.FromItem; 32 import com.caucho.amber.query.QueryParser; 33 import com.caucho.amber.type.BooleanType; 34 import com.caucho.amber.type.Type; 35 import com.caucho.util.CharBuffer; 36 37 import java.util.ArrayList ; 38 39 42 public class InExpr extends AbstractAmberExpr { 43 private AmberExpr _expr; 44 45 private ArrayList <AmberExpr> _values; 46 private boolean _isNot; 47 48 51 public InExpr(AmberExpr expr, 52 ArrayList <AmberExpr> values, 53 boolean isNot) 54 { 55 _expr = expr; 56 _values = values; 57 _isNot = isNot; 58 } 59 60 63 public boolean isBoolean() 64 { 65 return true; 66 } 67 68 71 public Type getType() 72 { 73 return BooleanType.create(); 74 } 75 76 79 public AmberExpr bindSelect(QueryParser parser) 80 { 81 _expr = _expr.bindSelect(parser); 82 83 for (int i = 0; i < _values.size(); i++) { 84 _values.set(i, _values.get(i).bindSelect(parser)); 85 } 86 87 return this; 88 } 89 90 93 public boolean usesFrom(FromItem from, int type, boolean isNot) 94 { 95 if (_expr.usesFrom(from, type)) 96 return true; 97 98 for (int i = 0; i < _values.size(); i++) { 99 if (_values.get(i).usesFrom(from, type)) 100 return true; 101 } 102 103 return false; 104 } 105 106 109 public AmberExpr replaceJoin(JoinExpr join) 110 { 111 _expr = _expr.replaceJoin(join); 112 113 for (int i = 0; i < _values.size(); i++) { 114 _values.set(i, _values.get(i).replaceJoin(join)); 115 } 116 117 return this; 118 } 119 120 123 public void generateWhere(CharBuffer cb) 124 { 125 generateInternalWhere(cb, true); 126 } 127 128 131 public void generateUpdateWhere(CharBuffer cb) 132 { 133 generateInternalWhere(cb, false); 134 } 135 136 139 public void generateHaving(CharBuffer cb) 140 { 141 generateWhere(cb); 142 } 143 144 public String toString() 145 { 146 CharBuffer cb = new CharBuffer(); 147 148 cb.append('('); 149 cb.append(_expr); 150 151 if (_isNot) 152 cb.append(" NOT"); 153 154 cb.append(" IN ("); 155 156 for (int i = 0; i < _values.size(); i++) { 157 if (i != 0) 158 cb.append(','); 159 160 cb.append(_values.get(i)); 161 } 162 163 cb.append("))"); 164 165 return cb.toString(); 166 } 167 168 171 private void generateInternalWhere(CharBuffer cb, 172 boolean select) 173 { 174 cb.append('('); 175 176 if (select) 177 _expr.generateWhere(cb); 178 else 179 _expr.generateUpdateWhere(cb); 180 181 if (_isNot) 182 cb.append(" NOT"); 183 184 cb.append(" IN ("); 185 186 for (int i = 0; i < _values.size(); i++) { 187 if (i != 0) 188 cb.append(','); 189 190 if (select) 191 _values.get(i).generateWhere(cb); 192 else 193 _values.get(i).generateUpdateWhere(cb); 194 } 195 196 cb.append("))"); 197 } 198 } 199 | Popular Tags |