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 40 public class BetweenExpr extends AbstractAmberExpr { 41 private AmberExpr _expr; 42 43 private AmberExpr _min; 44 private AmberExpr _max; 45 46 private boolean _isNot; 47 48 51 public BetweenExpr(AmberExpr expr, 52 AmberExpr min, 53 AmberExpr max, 54 boolean isNot) 55 { 56 _expr = expr; 57 _min = min; 58 _max = max; 59 60 _isNot = isNot; 61 } 62 63 66 public boolean isBoolean() 67 { 68 return true; 69 } 70 71 74 public Type getType() 75 { 76 return BooleanType.create(); 77 } 78 79 82 public AmberExpr bindSelect(QueryParser parser) 83 { 84 _expr = _expr.bindSelect(parser); 85 _min = _min.bindSelect(parser); 86 _max = _max.bindSelect(parser); 87 88 return this; 89 } 90 91 94 public boolean usesFrom(FromItem from, int type, boolean isNot) 95 { 96 return (_expr.usesFrom(from, type) || 97 _min.usesFrom(from, type) || 98 _max.usesFrom(from, type)); 99 } 100 101 104 public void generateWhere(CharBuffer cb) 105 { 106 generateInternalWhere(cb, true); 107 } 108 109 112 public void generateUpdateWhere(CharBuffer cb) 113 { 114 generateInternalWhere(cb, false); 115 } 116 117 120 private void generateInternalWhere(CharBuffer cb, 121 boolean select) 122 { 123 cb.append('('); 124 125 if (select) 126 _expr.generateWhere(cb); 127 else 128 _expr.generateUpdateWhere(cb); 129 130 if (_isNot) 131 cb.append(" NOT"); 132 133 cb.append(" BETWEEN "); 134 135 if (select) 136 _min.generateWhere(cb); 137 else 138 _min.generateUpdateWhere(cb); 139 140 cb.append(" AND "); 141 142 if (select) 143 _max.generateWhere(cb); 144 else 145 _max.generateUpdateWhere(cb); 146 147 cb.append(')'); 148 } 149 150 153 public void generateHaving(CharBuffer cb) 154 { 155 generateWhere(cb); 156 } 157 158 public String toString() 159 { 160 CharBuffer cb = CharBuffer.allocate(); 161 162 cb.append('('); 163 cb.append(_expr); 164 165 if (_isNot) 166 cb.append(" NOT"); 167 168 cb.append(" BETWEEN "); 169 170 cb.append(_min); 171 172 cb.append(" AND "); 173 174 cb.append(_max); 175 176 cb.append(')'); 177 178 return cb.close(); 179 } 180 } 181 | Popular Tags |