KickJava   Java API By Example, From Geeks To Geeks.

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


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.type.EntityType;
32 import com.caucho.config.ConfigException;
33 import com.caucho.ejb.cfg.EjbEntityBean;
34 import com.caucho.util.CharBuffer;
35
36 /**
37  * Identifier expression for EJB-QL.
38  */

39 class IdExpr extends PathExpr {
40   // identifier name
41
private String JavaDoc _name;
42   
43   // the table name
44
private String JavaDoc _tableName;
45
46   /**
47    * Creates a new identifier expression.
48    *
49    * @param query the owning query
50    * @param name the identifier
51    * @param bean the mapped bean
52    */

53   IdExpr(Query query, String JavaDoc name, EjbEntityBean bean)
54     throws ConfigException
55   {
56     super(bean);
57
58     _query = query;
59     _name = name;
60
61     setJavaType(bean.getEJBClass());
62   }
63
64   /**
65    * Returns the identifier name.
66    */

67   String JavaDoc getName()
68   {
69     return _name;
70   }
71   
72   String JavaDoc getKeyTable()
73   {
74     return getName();
75   }
76   
77   String JavaDoc []getKeyFields()
78   {
79     /*
80     PrimaryKey key = _bean.getPrimaryKey();
81     
82     return key.getSQLColumns();
83     */

84     throw new UnsupportedOperationException JavaDoc();
85   }
86
87   /**
88    * Returns the EJB name.
89    */

90   String JavaDoc getReturnEJB()
91   {
92     return getBean().getEJBName();
93   }
94
95   String JavaDoc getTable()
96   {
97     return _name;
98   }
99
100   /**
101    * Returns the persistent bean this id is a member of
102    */

103   EjbEntityBean getBean()
104   {
105     return _bean;
106   }
107   
108   /**
109    * Sets the persistent bean this id is a member of
110    */

111   void setBean(EjbEntityBean bean)
112   {
113     _bean = bean;
114   }
115
116   /**
117    * Returns the SQL table name for the id
118    */

119   String JavaDoc getTableName()
120   {
121     if (_tableName != null)
122       return _tableName;
123     else if (_bean != null)
124       return _bean.getSQLTable();
125     else
126       return null;
127   }
128
129   /**
130    * Sets the SQL table name for the id
131    */

132   void setTableName(String JavaDoc tableName)
133   {
134     _tableName = tableName;
135   }
136
137   /**
138    * Returns the item bean of a collection.
139    */

140   EjbEntityBean getItemBean()
141   {
142     return _bean;
143   }
144
145   int getComponentCount()
146   {
147     EjbEntityBean bean = getItemBean();
148
149     EntityType type = bean.getEntityType();
150     
151     return type.getId().getKeyCount();
152   }
153
154   /**
155    * Prints the select SQL for this expression
156    *
157    * @param gen the java code generator
158    */

159   void printSelect(CharBuffer cb)
160     throws ConfigException
161   {
162     if (_bean == null)
163       throw new ConfigException("no bean for " + getName());
164
165     /*
166     PrimaryKey key = _bean.getPrimaryKey();
167
168     String []names = key.getSQLColumns();
169     for (int i = 0; i < names.length; i++) {
170       if (i != 0)
171         cb.append(", ");
172
173       if (_query.getFromList().size() == 1) {
174         // special case to handle strange databases
175         cb.append(names[i]);
176       }
177       else {
178         cb.append(getName());
179         cb.append(".");
180         cb.append(names[i]);
181       }
182     }
183     */

184     cb.append(getName());
185   }
186
187   /**
188    * Prints the select SQL for this expression
189    *
190    * @param gen the java code generator
191    */

192   String JavaDoc getSelectTable(CharBuffer cb)
193     throws ConfigException
194   {
195     /*
196     if (_query.getFromList().size() == 1) {
197       // special case to handle strange databases
198       return null;
199     }
200     else
201       return getName();
202     */

203     return getName();
204   }
205
206   /**
207    * Prints the where SQL for this expression
208    *
209    * @param gen the java code generator
210    */

211   void generateWhere(CharBuffer cb)
212   {
213     if (_bean == null)
214       throw new IllegalStateException JavaDoc("no bean for " + getName());
215
216     cb.append(getName());
217
218     /*
219     String []names = _bean.getPrimaryKey().getSQLColumns();
220     if (names.length != 1)
221       throw new RuntimeException("multiple values need special test.");
222
223     if (_query.getFromList().size() == 1) {
224       // special case to handle strange databases
225       cb.append(names[0]);
226     }
227     else {
228       cb.append(getName());
229       cb.append(".");
230       cb.append(names[0]);
231     }
232     */

233   }
234
235   void generateComponent(CharBuffer cb, int index)
236   {
237     EjbEntityBean bean = getItemBean();
238
239     EntityType type = bean.getEntityType();
240     
241     cb.append(getName());
242     cb.append(".");
243
244     cb.append(keyComponent(type, index));
245   }
246
247   /**
248    * Prints the where SQL for this expression
249    *
250    * @param gen the java code generator
251    */

252   void generateAmber(CharBuffer cb)
253   {
254     cb.append(getBean().getAbstractSchemaName());
255     cb.append(' ');
256     cb.append(getName());
257   }
258   
259   /**
260    * Returns true if the two expressions are equal
261    */

262   public boolean equals(Object JavaDoc bObj)
263   {
264     if (! (bObj instanceof IdExpr))
265       return false;
266
267     IdExpr b = (IdExpr) bObj;
268
269     return _name.equals(b._name);
270   }
271
272   /**
273    * Returns a hash code for the expression
274    */

275   public int hashCode()
276   {
277     return _name.hashCode();
278   }
279
280   /**
281    * Printable version of the object.
282    */

283   public String JavaDoc toString()
284   {
285     return _name;
286   }
287 }
288
Popular Tags