KickJava   Java API By Example, From Geeks To Geeks.

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


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.config.ConfigException;
32 import com.caucho.ejb.cfg.CmpField;
33 import com.caucho.ejb.cfg.CmpRelation;
34 import com.caucho.ejb.cfg.EjbEntityBean;
35 import com.caucho.util.CharBuffer;
36
37 /**
38  * Expression representing a field or a relation.
39  */

40 class FieldExpr extends Expr {
41   // base expression
42
private PathExpr _base;
43   // field name
44
private String JavaDoc _name;
45
46   // bean information
47
private EjbEntityBean _bean;
48   // field information
49
private CmpField _field;
50   // relation information
51
private CmpRelation _relation;
52   
53   // SQL id for the results of this field
54
private String JavaDoc _tableId;
55   // base id if special
56
private String JavaDoc _baseId;
57   // sql field
58
private String JavaDoc _sqlField;
59
60   private int _primaryKeyFieldIndex;
61
62   // use count
63
private int _useCount;
64   private boolean _hasRelation;
65
66   /**
67    * Creates a new field expression.
68    *
69    * @param base the base expression
70    * @param field name of the field
71    */

72   FieldExpr(Query query, PathExpr base,
73             String JavaDoc fieldName, CmpField field)
74     throws ConfigException
75   {
76     _query = query;
77     _base = base;
78     _name = fieldName;
79
80     _field = field;
81
82     /*
83     PrimaryKey key = base.getItemBean().getPrimaryKey();
84     _primaryKeyFieldIndex = key.indexOf(field);
85     if (_primaryKeyFieldIndex < 0)
86       base.setUsesField();
87     */

88
89     setJavaType(field.getJavaType());
90   }
91
92   boolean hasRelation()
93   {
94     return _hasRelation;
95   }
96
97   void setRelation()
98   {
99     _hasRelation = true;
100   }
101
102   /**
103    * Returns the base expression
104    */

105   Expr getBase()
106   {
107     return _base;
108   }
109
110   /**
111    * Returns the field name
112    */

113   String JavaDoc getField()
114   {
115     return _name;
116   }
117
118   /**
119    * Returns the bean type of the result object
120    */

121   EjbEntityBean getBean()
122   {
123     return _bean;
124   }
125
126   /**
127    * Returns the relation
128    */

129   CmpRelation getRelation()
130   {
131     return _relation;
132   }
133
134   /**
135    * Returns the SQL table identifier for the result of the field expression.
136    */

137   String JavaDoc getTableId()
138   {
139     return _tableId;
140   }
141
142   /**
143    * Sets the SQL table identifier for the result of the field expression.
144    */

145   void setTableId(String JavaDoc tableId)
146   {
147     _tableId = tableId;
148   }
149
150   /**
151    * Returns the SQL id for the base
152    */

153   String JavaDoc getBaseId()
154   {
155     return _base.getTable();
156   }
157
158   /**
159    * Returns the SQL field reference.
160    */

161   String JavaDoc getSQLField()
162   {
163     /*
164     if (_sqlField != null)
165       return _sqlField;
166     else if (_field != null)
167       return _field.getSQLName();
168     else if (_relation != null)
169       return _relation.getTargetSQLPrefix();
170     else
171       return PersistentBean.javaToSQLName(_name);
172     */

173     throw new UnsupportedOperationException JavaDoc();
174   }
175
176   void decrementUse()
177   {
178     _useCount--;
179   }
180
181   int getUseCount()
182   {
183     return _useCount;
184   }
185
186   /**
187    * For collections, returns the underlying item bean.
188    */

189   EjbEntityBean getItemBean()
190   {
191     return _bean;
192   }
193
194   /*
195   int getComponentCount()
196   {
197     if (isPrimaryKeyField)
198       return base.getComponentCount();
199
200     return super.getComponentCount();
201   }
202   */

203
204   /**
205    * Prints the select SQL for this expression
206    *
207    * @param gen the java code generator
208    */

209     /*
210   void generateSelect(CharBuffer cb)
211   {
212     if (_primaryKeyFieldIndex >= 0) {
213       _base.printComponent(cb, _primaryKeyFieldIndex);
214       return;
215     }
216
217     if (_query.getFromList().size() == 1) {
218       // special case to handle strange databases
219       cb.append(_field.getSQLName());
220     }
221     else {
222       cb.append(_base.getTable());
223       cb.append(".");
224       cb.append(_field.getSQLName());
225     }
226     throw new UnsupportedOperationException();
227   }
228     */

229
230   /**
231    * Prints the select SQL for this expression
232    *
233    * @param gen the java code generator
234    */

235   String JavaDoc getSelectTable(CharBuffer cb)
236     throws ConfigException
237   {
238     /*
239     if (_query.getFromList().size() == 1) {
240       // special case to handle strange databases
241       return null;
242     }
243     else
244       return _base.getTable();
245     */

246     return _base.getTable();
247   }
248
249   /**
250    * Prints the where SQL for this expression
251    *
252    * @param gen the java code generator
253    */

254   void generateWhere(CharBuffer cb)
255   {
256     /*
257     if (_primaryKeyFieldIndex >= 0) {
258       _base.generateComponent(cb, _primaryKeyFieldIndex);
259       return;
260     }
261     */

262
263       /*
264     if (_query.getFromList().size() == 1) {
265       // special case to handle strange databases
266       cb.append(_field.getSQLName());
267     }
268     else {
269       cb.append(_base.getTable());
270       cb.append(".");
271       cb.append(_field.getSQLName());
272     }
273       */

274
275     _base.generateWhere(cb);
276     cb.append(".");
277     cb.append(_name);
278   }
279
280   /*
281   void generateComponent(CharBuffer cb, int i)
282   {
283     if (_primaryKeyFieldIndex >= 0) {
284       _base.generateComponent(cb, i);
285       return;
286     }
287
288     super.generateComponent(cb, i);
289   }
290   */

291
292   /**
293    * Returns true if the two expressions are equal
294    */

295   public boolean equals(Object JavaDoc bObj)
296   {
297     if (! (bObj instanceof FieldExpr))
298       return false;
299
300     FieldExpr b = (FieldExpr) bObj;
301
302     return _field.equals(b._field) && _base.equals(b._base);
303   }
304
305   /**
306    * Returns a hash code for the field
307    */

308   public int hashCode()
309   {
310     return _name.hashCode() * 65521 + _base.hashCode();
311   }
312
313   public String JavaDoc toString()
314   {
315     return _base.toString() + "." + _name;
316   }
317 }
318
Popular Tags