001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.ejb.gen21;
031:
032: import com.caucho.ejb.cfg21.EjbEntityBean;
033: import com.caucho.ejb.gen21.AbstractQueryMethod;
034: import com.caucho.amber.type.Type;
035: import com.caucho.bytecode.JClassWrapper;
036: import com.caucho.config.ConfigException;
037: import com.caucho.ejb.cfg.*;
038: import com.caucho.ejb.ql.EjbSelectQuery;
039: import com.caucho.java.JavaWriter;
040: import com.caucho.util.L10N;
041:
042: import java.io.IOException;
043:
044: /**
045: * Generates the code for a query
046: */
047: public class AmberSelectMethod extends AbstractQueryMethod {
048: private static L10N L = new L10N(AmberSelectMethod.class);
049:
050: private EjbEntityBean _returnType;
051: private ApiMethod _method;
052: private String _contextClassName;
053: private EjbSelectQuery _query;
054: private Type _amberType;
055:
056: public AmberSelectMethod(EjbEntityBean type, ApiMethod method,
057: String contextClassName, EjbSelectQuery query,
058: Type amberType) throws ConfigException {
059: super (type, method, query);
060:
061: _returnType = type;
062: _method = method;
063: _contextClassName = contextClassName;
064: _query = query;
065: _amberType = amberType;
066:
067: if (amberType == null)
068: throw new NullPointerException();
069: }
070:
071: /**
072: * Gets the parameter types
073: */
074: public Class[] getParameterTypes() {
075: return _method.getParameterTypes();
076: }
077:
078: /**
079: * Gets the return type.
080: */
081: public Class getReturnType() {
082: return _method.getReturnType();
083: }
084:
085: /**
086: * Prints the create method
087: *
088: * @param method the create method
089: */
090: public void generateCall(JavaWriter out, String[] args)
091: throws IOException {
092: out.print("com.caucho.ejb.xa.TransactionContext trans");
093: out
094: .println(" = _ejb_context.getTransactionManager().beginSupports();");
095:
096: out.println("com.caucho.amber.query.ResultSetImpl rs = null;");
097: out.println("try {");
098: out.pushDepth();
099:
100: generatePrepareQuery(out, args);
101:
102: out
103: .println("rs = (com.caucho.amber.query.ResultSetImpl) query.executeQuery();");
104:
105: out.println("if (rs.next()) {");
106: out.pushDepth();
107:
108: out.printClass(getReturnType());
109: out.print(" v = ");
110:
111: if (getReturnType().isPrimitive()) {
112: _amberType.generateLoad(out, "rs", "0", 1);
113: out.println(";");
114: } else {
115: _amberType.generateLoad(out, "rs", "0", 1, JClassWrapper
116: .create(getReturnType()));
117: out.println(";");
118: }
119:
120: out.println();
121: out.println("return v;");
122:
123: out.popDepth();
124: out.println("}");
125: out.println();
126:
127: if (getReturnType().isPrimitive())
128: out.println("return 0;");
129: else
130: out.println("return null;");
131:
132: out.popDepth();
133: out.println("} catch (java.sql.SQLException e) {");
134: out
135: .println(" throw new com.caucho.ejb.FinderExceptionWrapper(e);");
136: out.println("} finally {");
137: out.println("if (rs != null)");
138: out.println(" rs.close();");
139:
140: out.println(" trans.commit();");
141: out.println("}");
142: }
143: }
|