1 29 30 package com.caucho.ejb.gen; 31 32 import com.caucho.amber.type.Type; 33 import com.caucho.bytecode.JClass; 34 import com.caucho.bytecode.JMethod; 35 import com.caucho.config.ConfigException; 36 import com.caucho.ejb.cfg.EjbEntityBean; 37 import com.caucho.ejb.ql.EjbSelectQuery; 38 import com.caucho.java.JavaWriter; 39 import com.caucho.util.L10N; 40 41 import java.io.IOException ; 42 43 46 public class AmberSelectMethod extends AbstractQueryMethod { 47 private static L10N L = new L10N(AmberSelectMethod.class); 48 49 private EjbEntityBean _returnType; 50 private JMethod _method; 51 private String _contextClassName; 52 private EjbSelectQuery _query; 53 private Type _amberType; 54 55 public AmberSelectMethod(EjbEntityBean type, 56 JMethod method, 57 String contextClassName, 58 EjbSelectQuery query, 59 Type amberType) 60 throws ConfigException 61 { 62 super(type, method, query); 63 64 _returnType = type; 65 _method = method; 66 _contextClassName = contextClassName; 67 _query = query; 68 _amberType = amberType; 69 70 if (amberType == null) 71 throw new NullPointerException (); 72 } 73 74 77 public JClass []getParameterTypes() 78 { 79 return _method.getParameterTypes(); 80 } 81 82 85 public JClass getReturnType() 86 { 87 return _method.getReturnType(); 88 } 89 90 95 public void generateCall(JavaWriter out, String []args) 96 throws IOException 97 { 98 out.print("com.caucho.ejb.xa.TransactionContext trans"); 99 out.println(" = _ejb_context.getTransactionManager().beginSupports();"); 100 101 out.println("com.caucho.amber.query.ResultSetImpl rs = null;"); 102 out.println("try {"); 103 out.pushDepth(); 104 105 generatePrepareQuery(out, args); 106 107 out.println("rs = (com.caucho.amber.query.ResultSetImpl) query.executeQuery();"); 108 109 out.println("if (rs.next()) {"); 110 out.pushDepth(); 111 112 out.print(getReturnType().getPrintName()); 113 out.print(" v = "); 114 115 if (getReturnType().isPrimitive()) { 116 _amberType.generateLoad(out, "rs", "0", 1); 117 out.println(";"); 118 } 119 else { 120 _amberType.generateLoad(out, "rs", "0", 1, getReturnType()); 121 out.println(";"); 122 } 123 124 out.println(); 125 out.println("return v;"); 126 127 out.popDepth(); 128 out.println("}"); 129 out.println(); 130 131 if (getReturnType().isPrimitive()) 132 out.println("return 0;"); 133 else 134 out.println("return null;"); 135 136 out.popDepth(); 137 out.println("} catch (java.sql.SQLException e) {"); 138 out.println(" throw new com.caucho.ejb.FinderExceptionWrapper(e);"); 139 out.println("} finally {"); 140 out.println("if (rs != null)"); 141 out.println(" rs.close();"); 142 143 out.println(" trans.commit();"); 144 out.println("}"); 145 } 146 } 147 | Popular Tags |