KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > gen > AbstractQueryMethod


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.Collections JavaDoc;
49
50 /**
51  * Generates the code for a query
52  */

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     /*
73     EjbConfig ejbConfig = beanType.getConfig();
74
75     AmberPersistenceUnit amberManager = beanType.getConfig().getEJBManager().getAmberManager();
76     */

77   }
78
79   /**
80    * Sets the query-loads-bean propery.
81    */

82   public void setQueryLoadsBean(boolean queryLoadsBean)
83   {
84     _queryLoadsBean = queryLoadsBean;
85   }
86
87   /**
88    * Gets the parameter types
89    */

90   public JClass []getParameterTypes()
91   {
92     return _method.getParameterTypes();
93   }
94
95   /**
96    * Gets the return type.
97    */

98   public JClass getReturnType()
99   {
100     return _method.getReturnType();
101   }
102
103   protected String JavaDoc generateBeanId()
104   {
105     return "bean.__caucho_getPrimaryKey()";
106   }
107   
108   void generatePrepareQuery(JavaWriter out, String JavaDoc []args)
109     throws IOException JavaDoc
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 JavaDoc query)
151     throws IOException JavaDoc
152   {
153     EntityType amberType = bean.getEntityType();
154
155     ArrayList JavaDoc<IdField> keys = new ArrayList JavaDoc<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 JavaDoc arg)
165     throws IOException JavaDoc
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 JavaDoc query,
174                       String JavaDoc arg)
175     throws IOException JavaDoc
176   {
177     if (type.getName().equals("boolean")) {
178       // printCheckNull(out, type, arg);
179

180       out.println(query + ".setBoolean(index++, " + arg + ");");
181     }
182     else if (type.getName().equals("byte")) {
183       // printCheckNull(index, type, expr);
184

185       out.println(query + ".setInt(index++, " + arg + ");");
186     }
187     else if (type.getName().equals("short")) {
188       // printCheckNull(index, type, expr);
189

190       out.println(query + ".setInt(index++, " + arg + ");");
191     }
192     else if (type.getName().equals("int")) {
193       // printCheckNull(index, type, expr);
194

195       out.println(query + ".setInt(index++, " + arg + ");");
196     }
197     else if (type.getName().equals("long")) {
198       // printCheckNull(index, type, expr);
199

200       out.println(query + ".setLong(index++, " + arg + ");");
201     }
202     else if (type.getName().equals("char")) {
203       // printCheckNull(index, type, expr);
204

205       out.println(query + ".setString(index++, String.valueOf(" + arg + "));");
206     }
207     else if (type.getName().equals("float")) {
208       // printCheckNull(index, type, expr);
209

210       out.println(query + ".setFloat(index++, " + arg + ");");
211     }
212     else if (type.getName().equals("double")) {
213       // printCheckNull(index, type, expr);
214

215       out.println(query + ".setDouble(index++, " + arg + ");");
216     }
217     else if (type.isAssignableTo(java.sql.Timestamp JavaDoc.class)) {
218       out.println(query + ".setTimestamp(index++, " + arg + ");");
219     }
220     else if (type.isAssignableTo(java.sql.Date JavaDoc.class))
221       out.println(query + ".setDate(index++, " + arg + ");");
222     else if (type.isAssignableTo(java.sql.Time JavaDoc.class))
223       out.println(query + ".setTime(index++, " + arg + ");");
224     else if (type.isAssignableTo(java.util.Date JavaDoc.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       // XXX; fixing oracle issues
299
// out.println(" " + query + ".setBytes(index++, (byte []) " + arg + ");");
300
}
301     /*
302     else if (Serializable.class.isAssignableFrom(type)) {
303       out.println("if (" + arg + " == null)");
304       out.println(" " + query + ".setNull(index++, 0);");
305       out.println("else");
306       out.println(" " + query + ".setBytes(index++, _caucho_serialize(" + arg + "));");
307       hasSerialization = true;
308     }
309     */

310     else if (type.isAssignableTo(javax.ejb.EJBLocalObject JavaDoc.class)) {
311       EjbEntityBean bean = config.findEntityByLocal(type.getRawType());
312
313       if (bean == null)
314     throw new IllegalStateException JavaDoc(L.l("can't find bean for {0}",
315                         type.getName()));
316
317       EntityType amberType = bean.getEntityType();
318
319       ArrayList JavaDoc<IdField> keys = new ArrayList JavaDoc<IdField>();
320       keys.addAll(amberType.getId().getKeys());
321       Collections.sort(keys, new IdFieldCompare());
322
323       String JavaDoc 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 JavaDoc 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       // XXX:
366
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