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.table.ForeignColumn; 34 import com.caucho.util.CharBuffer; 35 36 import java.util.ArrayList ; 37 38 41 public class UnaryExpr extends AbstractAmberExpr { 42 private AmberExpr _expr; 43 private int _token; 44 45 48 public UnaryExpr(int token, AmberExpr expr) 49 { 50 _token = token; 51 _expr = expr; 52 } 53 54 57 public boolean isBoolean() 58 { 59 return (_token == QueryParser.NOT || 60 _token == QueryParser.NULL || 61 _token == QueryParser.NOT_NULL); 62 } 63 64 67 public Class getJavaType() 68 { 69 switch (_token) { 70 case QueryParser.NOT: 71 case QueryParser.NULL: 72 return boolean.class; 73 default: 74 return double.class; 75 } 76 } 77 78 81 public AmberExpr bindSelect(QueryParser parser) 82 { 83 _expr = _expr.bindSelect(parser); 84 85 return this; 86 } 87 88 91 public boolean usesFrom(FromItem from, int type, boolean isNot) 92 { 93 switch (_token) { 94 case '-': 95 case '+': 96 case QueryParser.NULL: 97 case QueryParser.NOT_NULL: 98 return _expr.usesFrom(from, type); 99 100 case QueryParser.NOT: 101 return _expr.usesFrom(from, type, ! isNot); 102 103 default: 104 return false; 105 } 106 } 107 108 111 public void generateWhere(CharBuffer cb) 112 { 113 generateInternalWhere(cb, true); 114 } 115 116 119 public void generateUpdateWhere(CharBuffer cb) 120 { 121 generateInternalWhere(cb, false); 122 } 123 124 127 public void generateHaving(CharBuffer cb) 128 { 129 generateWhere(cb); 130 } 131 132 public String toString() 133 { 134 String str = null; 135 136 switch (_token) { 137 case '-': 138 str = "-"; 139 break; 140 case '+': 141 str = "+"; 142 break; 143 case QueryParser.NOT: 144 str = "not"; 145 break; 146 case QueryParser.NULL: 147 return _expr + " is null"; 148 case QueryParser.NOT_NULL: 149 return _expr + " is not null"; 150 } 151 152 return str + " " + _expr; 153 } 154 155 158 private void generateInternalWhere(CharBuffer cb, 159 boolean select) 160 { 161 switch (_token) { 162 case '-': 163 cb.append(" -"); 164 break; 165 case '+': 166 cb.append(" +"); 167 break; 168 case QueryParser.NOT: 169 cb.append("NOT "); 170 break; 171 case QueryParser.NULL: 172 case QueryParser.NOT_NULL: 173 if (_expr instanceof ManyToOneExpr) { 174 ManyToOneExpr path = (ManyToOneExpr) _expr; 175 cb.append('('); 176 177 ArrayList <ForeignColumn> keys = path.getLinkColumns().getColumns(); 178 for (int i = 0; i < keys.size(); i++) { 179 if (i != 0) 180 cb.append(" and "); 181 182 cb.append(path.getFromItem().getName()); 183 cb.append("."); 184 cb.append(keys.get(i).getName()); 185 186 if (_token == QueryParser.NULL) 187 cb.append(" is null"); 188 else 189 cb.append(" is not null"); 190 } 191 cb.append(')'); 192 } 193 else { 194 195 if (select) 196 _expr.generateWhere(cb); 197 else 198 _expr.generateUpdateWhere(cb); 199 200 if (_token == QueryParser.NULL) 201 cb.append(" is null"); 202 else 203 cb.append(" is not null"); 204 } 205 return; 206 } 207 208 if (select) 209 _expr.generateWhere(cb); 210 else 211 _expr.generateUpdateWhere(cb); 212 } 213 } 214 | Popular Tags |