1 29 30 package com.caucho.ejb.gen; 31 32 import com.caucho.amber.field.IdField; 33 import com.caucho.amber.type.EntityType; 34 import com.caucho.bytecode.JClass; 35 import com.caucho.bytecode.JField; 36 import com.caucho.bytecode.JMethod; 37 import com.caucho.bytecode.JType; 38 import com.caucho.config.ConfigException; 39 import com.caucho.ejb.cfg.EjbConfig; 40 import com.caucho.ejb.cfg.EjbEntityBean; 41 import com.caucho.ejb.ql.EjbSelectQuery; 42 import com.caucho.java.JavaWriter; 43 import com.caucho.java.gen.BaseMethod; 44 import com.caucho.util.L10N; 45 46 import java.io.IOException ; 47 import java.util.ArrayList ; 48 import java.util.Collections ; 49 50 53 abstract public class AbstractQueryMethod extends BaseMethod { 54 private static final L10N L = new L10N(AbstractQueryMethod.class); 55 56 private JMethod _method; 57 private EjbEntityBean _bean; 58 private EjbSelectQuery _query; 59 private boolean _queryLoadsBean = true; 60 61 protected AbstractQueryMethod(EjbEntityBean bean, 62 JMethod method, 63 EjbSelectQuery query) 64 throws ConfigException 65 { 66 super(method); 67 68 _bean = bean; 69 _query = query; 70 _method = method; 71 72 77 } 78 79 82 public void setQueryLoadsBean(boolean queryLoadsBean) 83 { 84 _queryLoadsBean = queryLoadsBean; 85 } 86 87 90 public JClass []getParameterTypes() 91 { 92 return _method.getParameterTypes(); 93 } 94 95 98 public JClass getReturnType() 99 { 100 return _method.getReturnType(); 101 } 102 103 protected String generateBeanId() 104 { 105 return "bean.__caucho_getPrimaryKey()"; 106 } 107 108 void generatePrepareQuery(JavaWriter out, String []args) 109 throws IOException 110 { 111 out.println("com.caucho.amber.AmberQuery query;"); 112 113 out.print("query = trans.getAmberConnection().prepareQuery(\""); 114 out.print(_query.toAmberQuery(args)); 115 out.println("\");"); 116 117 int len = args.length; 118 119 if (_query.getMaxArg() < len) 120 len = _query.getMaxArg(); 121 122 if (len > 0 || _query.getThisExpr() != null) 123 out.println("int index = 1;"); 124 125 JClass []paramTypes = getParameterTypes(); 126 127 for (int i = 0; i < len; i++) { 128 generateSetParameter(out, paramTypes[i], args[i]); 129 } 130 131 if (_query.getThisExpr() != null) 132 generateSetThis(out, _bean, "query"); 133 134 if (_query.getOffsetValue() > 0) 135 out.println("query.setFirstResult(" + _query.getOffsetValue() + ");"); 136 else if (_query.getOffsetArg() > 0) 137 out.println("query.setFirstResult(" + args[_query.getOffsetArg() - 1] + ");"); 138 139 if (_query.getLimitValue() > 0) 140 out.println("query.setMaxResults(" + _query.getLimitValue() + ");"); 141 else if (_query.getLimitArg() > 0) 142 out.println("query.setMaxResults(" + args[_query.getLimitArg() - 1] + ");"); 143 144 if (! _queryLoadsBean) 145 out.println("query.setLoadOnQuery(false);"); 146 } 147 148 public static void generateSetThis(JavaWriter out, 149 EjbEntityBean bean, 150 String query) 151 throws IOException 152 { 153 EntityType amberType = bean.getEntityType(); 154 155 ArrayList <IdField> keys = new ArrayList <IdField>(); 156 keys.addAll(amberType.getId().getKeys()); 157 Collections.sort(keys, new IdFieldCompare()); 158 159 for (IdField field : keys) { 160 field.generateSet(out, query + "", "index", "super"); 161 } 162 } 163 164 public void generateSetParameter(JavaWriter out, JClass type, String arg) 165 throws IOException 166 { 167 generateSetParameter(out, _bean.getConfig(), type, "query", arg); 168 } 169 170 public static void generateSetParameter(JavaWriter out, 171 EjbConfig config, 172 JType type, 173 String query, 174 String arg) 175 throws IOException 176 { 177 if (type.getName().equals("boolean")) { 178 180 out.println(query + ".setBoolean(index++, " + arg + ");"); 181 } 182 else if (type.getName().equals("byte")) { 183 185 out.println(query + ".setInt(index++, " + arg + ");"); 186 } 187 else if (type.getName().equals("short")) { 188 190 out.println(query + ".setInt(index++, " + arg + ");"); 191 } 192 else if (type.getName().equals("int")) { 193 195 out.println(query + ".setInt(index++, " + arg + ");"); 196 } 197 else if (type.getName().equals("long")) { 198 200 out.println(query + ".setLong(index++, " + arg + ");"); 201 } 202 else if (type.getName().equals("char")) { 203 205 out.println(query + ".setString(index++, String.valueOf(" + arg + "));"); 206 } 207 else if (type.getName().equals("float")) { 208 210 out.println(query + ".setFloat(index++, " + arg + ");"); 211 } 212 else if (type.getName().equals("double")) { 213 215 out.println(query + ".setDouble(index++, " + arg + ");"); 216 } 217 else if (type.isAssignableTo(java.sql.Timestamp .class)) { 218 out.println(query + ".setTimestamp(index++, " + arg + ");"); 219 } 220 else if (type.isAssignableTo(java.sql.Date .class)) 221 out.println(query + ".setDate(index++, " + arg + ");"); 222 else if (type.isAssignableTo(java.sql.Time .class)) 223 out.println(query + ".setTime(index++, " + arg + ");"); 224 else if (type.isAssignableTo(java.util.Date .class)) { 225 out.println("{"); 226 out.println(" java.util.Date _caucho_tmp_date = " + arg + ";"); 227 out.println(" if (_caucho_tmp_date == null)"); 228 out.println(" " + query + ".setNull(index++, java.sql.Types.TIMESTAMP);"); 229 out.println(" else"); 230 out.println(" " + query + ".setTimestamp(index++, new java.sql.Timestamp(_caucho_tmp_date.getTime()));"); 231 out.println("}"); 232 } 233 else if (type.getName().equals("java.lang.Boolean")) { 234 out.println("if (" + arg + " == null)"); 235 out.println(" " + query + ".setNull(index++, java.sql.Types.BIT);"); 236 out.println("else"); 237 out.println(" " + query + ".setBoolean(index++, " + arg + ".booleanValue());"); 238 } 239 else if (type.getName().equals("java.lang.Character")) { 240 out.println("if (" + arg + " == null)"); 241 out.println(" " + query + ".setNull(index++, java.sql.Types.VARCHAR);"); 242 out.println("else"); 243 out.println(" " + query + ".setString(index++, " + arg + ".toString());"); 244 } 245 else if (type.getName().equals("java.lang.String")) { 246 out.println(" " + query + ".setString(index++, " + arg + ");"); 247 } 248 else if (type.getName().equals("java.lang.Byte")) { 249 out.println("if (" + arg + " == null)"); 250 out.println(" " + query + ".setNull(index++, java.sql.Types.TINYINT);"); 251 out.println("else"); 252 out.println(" " + query + ".setInt(index++, " + arg + ".byteValue());"); 253 } 254 else if (type.getName().equals("java.lang.Short")) { 255 out.println("if (" + arg + " == null)"); 256 out.println(" " + query + ".setNull(index++, java.sql.Types.SMALLINT);"); 257 out.println("else"); 258 out.println(" " + query + ".setInt(index++, " + arg + ".shortValue());"); 259 } 260 else if (type.getName().equals("java.lang.Integer")) { 261 out.println("if (" + arg + " == null)"); 262 out.println(" " + query + ".setNull(index++, java.sql.Types.INTEGER);"); 263 out.println("else"); 264 out.println(" " + query + ".setInt(index++, " + arg + ".intValue());"); 265 } 266 else if (type.getName().equals("java.lang.Long")) { 267 out.println("if (" + arg + " == null)"); 268 out.println(" " + query + ".setNull(index++, java.sql.Types.BIGINT);"); 269 out.println("else"); 270 out.println(" " + query + ".setLong(index++, " + arg + ".longValue());"); 271 } 272 else if (type.getName().equals("java.lang.Float")) { 273 out.println("if (" + arg + " == null)"); 274 out.println(" " + query + ".setNull(index++, java.sql.Types.REAL);"); 275 out.println("else"); 276 out.println(" " + query + ".setDouble(index++, " + arg + ".floatValue());"); 277 } 278 else if (type.getName().equals("java.lang.Double")) { 279 out.println("if (" + arg + " == null)"); 280 out.println(" " + query + ".setNull(index++, java.sql.Types.DOUBLE);"); 281 out.println("else"); 282 out.println(" " + query + ".setDouble(index++, " + arg + ".doubleValue());"); 283 } 284 else if (type.getName().equals("java.math.BigDecimal")) { 285 out.println("if (" + arg + " == null)"); 286 out.println(" " + query + ".setNull(index++, java.sql.Types.NUMERIC);"); 287 out.println("else"); 288 out.println(" " + query + ".setBigDecimal(index++, " + arg + ");"); 289 } 290 else if (type.getName().equals("[B")) { 291 out.println("if (" + arg + " == null)"); 292 out.println(" " + query + ".setNull(index++, java.sql.Types.VARBINARY);"); 293 out.println("else {"); 294 out.println(" byte []bArray = (byte []) " + arg + ";"); 295 out.println(" " + query + ".setBinaryStream(index++, new java.io.ByteArrayInputStream(bArray), bArray.length);"); 296 out.println("}"); 297 298 } 301 310 else if (type.isAssignableTo(javax.ejb.EJBLocalObject .class)) { 311 EjbEntityBean bean = config.findEntityByLocal(type.getRawType()); 312 313 if (bean == null) 314 throw new IllegalStateException (L.l("can't find bean for {0}", 315 type.getName())); 316 317 EntityType amberType = bean.getEntityType(); 318 319 ArrayList <IdField> keys = new ArrayList <IdField>(); 320 keys.addAll(amberType.getId().getKeys()); 321 Collections.sort(keys, new IdFieldCompare()); 322 323 String var = "_expr" + out.generateId(); 324 out.print(type.getPrintName()); 325 out.println(" " + var + " = " + arg + ";"); 326 327 out.println("if (" + var + " != null) {"); 328 out.pushDepth(); 329 330 for (IdField field : keys) { 331 field.generateSet(out, query + "", "index", arg); 332 } 333 334 out.popDepth(); 335 out.println("} else {"); 336 out.pushDepth(); 337 338 for (IdField field : keys) { 339 field.generateSet(out, query + "", "index", null); 340 } 341 342 out.popDepth(); 343 out.println("}"); 344 } 345 else { 346 JField []fields = type.getFields(); 347 348 String var = "_expr" + out.generateId(); 349 out.print(type.getPrintName()); 350 out.println(" " + var + " = " + arg + ";"); 351 352 out.println("if (" + var + " != null) {"); 353 out.pushDepth(); 354 for (int i = 0; i < fields.length; i++) { 355 JField field = fields[i]; 356 357 generateSetParameter(out, config, 358 field.getType(), 359 query, arg + "." + field.getName()); 360 } 361 out.popDepth(); 362 out.println("} else {"); 363 out.pushDepth(); 364 365 for (int i = 0; i < fields.length; i++) { 367 out.println(query + ".setNull(index++, 0);"); 368 } 369 370 out.popDepth(); 371 out.println("}"); 372 } 373 } 374 } 375 | Popular Tags |