1 28 29 package com.caucho.ejb.ql; 30 31 import com.caucho.amber.field.IdField; 32 import com.caucho.amber.field.KeyManyToOneField; 33 import com.caucho.amber.type.EntityType; 34 import com.caucho.bytecode.JClass; 35 import com.caucho.bytecode.JClassLoader; 36 import com.caucho.config.ConfigException; 37 import com.caucho.ejb.cfg.EjbEntityBean; 38 import com.caucho.util.CharBuffer; 39 import com.caucho.util.L10N; 40 import com.caucho.util.Log; 41 42 import java.util.ArrayList ; 43 import java.util.Collection ; 44 import java.util.Collections ; 45 import java.util.logging.Logger ; 46 47 50 public class Expr { 51 static final Logger log = Log.open(Expr.class); 52 static final L10N L = new L10N(Expr.class); 53 54 protected Query _query; 55 56 private JClass _javaType; 58 59 62 public JClass getJavaType() 63 { 64 return _javaType; 65 } 66 67 70 void setJavaType(JClass javaType) 71 { 72 _javaType = javaType; 73 } 74 75 78 void setJavaType(Class javaType) 79 { 80 setJavaType(JClassLoader.localForName(javaType.getName())); 81 } 82 83 86 String getReturnEJB() 87 { 88 return null; 89 } 90 91 94 boolean isKey() 95 { 96 return getReturnEJB() != null; 97 } 98 99 102 boolean isNumeric() 103 { 104 JClass type = getJavaType(); 105 106 if (type == null) 107 return false; 108 109 String typeName = type.getName(); 110 111 if ("java.lang.Byte".equals(typeName) || 112 "java.lang.Short".equals(typeName) || 113 "java.lang.Integer".equals(typeName) || 114 "java.lang.Long".equals(typeName) || 115 "java.lang.Float".equals(typeName) || 116 "java.lang.Double".equals(typeName)) 117 return true; 118 else if (! type.isPrimitive()) 119 return false; 120 else if (typeName.equals("boolean") || typeName.equals("char")) 121 return false; 122 else 123 return true; 124 } 125 126 129 boolean isInteger() 130 { 131 JClass type = getJavaType(); 132 String typeName = type.getName(); 133 134 return ("java.lang.Byte".equals(typeName) || 135 "java.lang.Short".equals(typeName) || 136 "java.lang.Integer".equals(typeName) || 137 "java.lang.Long".equals(typeName) || 138 "byte".equals(typeName) || 139 "short".equals(typeName) || 140 "int".equals(typeName) || 141 "long".equals(typeName)); 142 } 143 144 147 static boolean isInteger(JClass type) 148 { 149 String typeName = type.getName(); 150 151 return ("java.lang.Byte".equals(typeName) || 152 "java.lang.Short".equals(typeName) || 153 "java.lang.Integer".equals(typeName) || 154 "java.lang.Long".equals(typeName) || 155 "byte".equals(typeName) || 156 "short".equals(typeName) || 157 "int".equals(typeName) || 158 "long".equals(typeName)); 159 } 160 161 int getComponentCount() 162 { 163 return 1; 164 } 165 166 169 boolean isString() 170 { 171 JClass type = getJavaType(); 172 String typeName = type.getName(); 173 174 return ("java.lang.String".equals(typeName) || 175 "char".equals(typeName) || 176 "java.lang.Character".equals(typeName)); 177 } 178 179 182 boolean isBoolean() 183 { 184 JClass type = getJavaType(); 185 String typeName = type.getName(); 186 187 return ("boolean".equals(typeName) || 188 "java.lang.Boolean".equals(typeName)); 189 } 190 191 194 boolean isDate() 195 { 196 JClass type = getJavaType(); 197 String typeName = type.getName(); 198 199 return ("java.util.Date".equals(typeName) || 200 "java.sql.Timestamp".equals(typeName) || 201 "java.sql.Date".equals(typeName) || 202 "java.sql.Time".equals(typeName)); 203 } 204 205 208 boolean canCoerce() 209 { 210 return false; 211 } 212 213 216 boolean isCollection() 217 { 218 JClass type = getJavaType(); 219 220 return type.isAssignableTo(Collection .class); 221 } 222 223 226 boolean isExternal() 227 { 228 return false; 229 } 230 231 234 EjbEntityBean getItemBean() 235 { 236 return null; 237 } 238 239 242 Expr newField(String field) 243 throws ConfigException 244 { 245 throw error(L.l("`{0}' can't have field `{1}'. Only path expressions referring to a single bean have fields.", this, field)); 246 } 247 248 251 FieldExpr newReference(String field) 252 throws ConfigException 253 { 254 throw error(L.l("`{0}' can't have reference `{1}'", this, field)); 255 } 256 257 260 void evalTypes() 261 throws ConfigException 262 { 263 if (getJavaType() == null) 264 throw error(L.l("'{0}' has no type.", this) + getClass()); 265 } 266 267 270 void generateSelect(CharBuffer cb) 271 { 272 generateWhere(cb); 273 } 274 275 278 String getSelectTable(CharBuffer cb) 279 throws ConfigException 280 { 281 throw new IllegalStateException (L.l("`{0}' can't be used in a SELECT expression", this)); 282 } 283 284 287 void generateWhere(CharBuffer cb) 288 { 289 throw new IllegalStateException (L.l("{0}: '{1}' can't be used in a WHERE expression", getClass().getName(), this)); 290 } 291 292 295 void generateWhereSubExpr(CharBuffer cb) 296 { 297 generateWhere(cb); 298 } 299 300 303 void generateComponent(CharBuffer cb, int i) 304 { 305 if (i == 0) 306 generateWhereSubExpr(cb); 307 else 308 throw new IllegalStateException (L.l("`{0}' can't be used in a WHERE multi-component", this)); 309 } 310 311 protected String keyComponent(EntityType type, int index) 312 { 313 ArrayList <String > names = new ArrayList <String >(); 314 315 addKeys(names, type, ""); 316 317 Collections.sort(names); 318 319 return names.get(index); 320 } 321 322 protected void addKeys(ArrayList <String > names, 323 EntityType type, 324 String prefix) 325 { 326 for (IdField key : type.getId().getKeys()) { 327 if (key instanceof KeyManyToOneField) { 328 KeyManyToOneField manyToOne = (KeyManyToOneField) key; 329 330 addKeys(names, manyToOne.getEntityType(), 331 prefix + key.getName() + "."); 332 } 333 else 334 names.add(prefix + key.getName()); 335 } 336 } 337 338 341 ConfigException error(String msg) 342 { 343 if (_query != null) 344 return _query.error(msg); 345 else 346 return new ConfigException(msg); 347 } 348 349 352 ConfigException error(Query query, String msg) 353 { 354 return query.error(msg); 355 } 356 } 357 | Popular Tags |