KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > ql > Expr


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

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 JavaDoc;
43 import java.util.Collection JavaDoc;
44 import java.util.Collections JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * Parsed expression for EJB-QL.
49  */

50 public class Expr {
51   static final Logger JavaDoc log = Log.open(Expr.class);
52   static final L10N L = new L10N(Expr.class);
53
54   protected Query _query;
55   
56   // The type of the expression value
57
private JClass _javaType;
58
59   /**
60    * Gets the Java Type of the expression.
61    */

62   public JClass getJavaType()
63   {
64     return _javaType;
65   }
66
67   /**
68    * Sets the Java Type of the expression.
69    */

70   void setJavaType(JClass javaType)
71   {
72     _javaType = javaType;
73   }
74
75   /**
76    * Sets the Java Type of the expression.
77    */

78   void setJavaType(Class JavaDoc javaType)
79   {
80     setJavaType(JClassLoader.localForName(javaType.getName()));
81   }
82
83   /**
84    * Returns the EJB name.
85    */

86   String JavaDoc getReturnEJB()
87   {
88     return null;
89   }
90
91   /**
92    * Returns true for object values.
93    */

94   boolean isKey()
95   {
96     return getReturnEJB() != null;
97   }
98
99   /**
100    * True if the type is numeric.
101    */

102   boolean isNumeric()
103   {
104     JClass type = getJavaType();
105
106     if (type == null)
107       return false;
108
109     String JavaDoc 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   /**
127    * True if the type is integer.
128    */

129   boolean isInteger()
130   {
131     JClass type = getJavaType();
132     String JavaDoc 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   /**
145    * True if the type is integer.
146    */

147   static boolean isInteger(JClass type)
148   {
149    String JavaDoc 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   /**
167    * True if the type is a string.
168    */

169   boolean isString()
170   {
171     JClass type = getJavaType();
172     String JavaDoc typeName = type.getName();
173
174     return ("java.lang.String".equals(typeName) ||
175             "char".equals(typeName) ||
176             "java.lang.Character".equals(typeName));
177   }
178
179   /**
180    * True if the type is a boolean.
181    */

182   boolean isBoolean()
183   {
184     JClass type = getJavaType();
185     String JavaDoc typeName = type.getName();
186
187     return ("boolean".equals(typeName) ||
188         "java.lang.Boolean".equals(typeName));
189   }
190
191   /**
192    * True if the type is a date.
193    */

194   boolean isDate()
195   {
196     JClass type = getJavaType();
197     String JavaDoc 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   /**
206    * Only args can be coerced.
207    */

208   boolean canCoerce()
209   {
210     return false;
211   }
212
213   /**
214    * True if the type is a collection.
215    */

216   boolean isCollection()
217   {
218     JClass type = getJavaType();
219
220     return type.isAssignableTo(Collection JavaDoc.class);
221   }
222
223   /**
224    * True if the bean refers to an external entity bean.
225    */

226   boolean isExternal()
227   {
228     return false;
229   }
230
231   /**
232    * Returns the item bean of a collection.
233    */

234   EjbEntityBean getItemBean()
235   {
236     return null;
237   }
238
239   /**
240    * Creates a field expression from this expression.
241    */

242   Expr newField(String JavaDoc 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   /**
249    * Creates an external entity reference from this expression.
250    */

251   FieldExpr newReference(String JavaDoc field)
252     throws ConfigException
253   {
254     throw error(L.l("`{0}' can't have reference `{1}'", this, field));
255   }
256
257   /**
258    * Evaluates the types for the expression
259    */

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   /**
268    * Prints the select SQL for this expression
269    */

270   void generateSelect(CharBuffer cb)
271   {
272     generateWhere(cb);
273   }
274
275   /**
276    * Returns the select's table
277    */

278   String JavaDoc getSelectTable(CharBuffer cb)
279     throws ConfigException
280   {
281     throw new IllegalStateException JavaDoc(L.l("`{0}' can't be used in a SELECT expression", this));
282   }
283
284   /**
285    * Prints the where SQL for this expression
286    */

287   void generateWhere(CharBuffer cb)
288   {
289     throw new IllegalStateException JavaDoc(L.l("{0}: '{1}' can't be used in a WHERE expression", getClass().getName(), this));
290   }
291
292   /**
293    * Prints the where SQL for this expression
294    */

295   void generateWhereSubExpr(CharBuffer cb)
296   {
297     generateWhere(cb);
298   }
299
300   /**
301    * Prints the where SQL for this expression
302    */

303   void generateComponent(CharBuffer cb, int i)
304   {
305     if (i == 0)
306       generateWhereSubExpr(cb);
307     else
308       throw new IllegalStateException JavaDoc(L.l("`{0}' can't be used in a WHERE multi-component", this));
309   }
310
311   protected String JavaDoc keyComponent(EntityType type, int index)
312   {
313     ArrayList JavaDoc<String JavaDoc> names = new ArrayList JavaDoc<String JavaDoc>();
314
315     addKeys(names, type, "");
316
317     Collections.sort(names);
318
319     return names.get(index);
320   }
321
322   protected void addKeys(ArrayList JavaDoc<String JavaDoc> names,
323              EntityType type,
324              String JavaDoc 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   /**
339    * Creates an error.
340    */

341   ConfigException error(String JavaDoc msg)
342   {
343     if (_query != null)
344       return _query.error(msg);
345     else
346       return new ConfigException(msg);
347   }
348   
349   /**
350    * Creates an error.
351    */

352   ConfigException error(Query query, String JavaDoc msg)
353   {
354     return query.error(msg);
355   }
356 }
357
Popular Tags