KickJava   Java API By Example, From Geeks To Geeks.

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


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.bytecode.JClass;
33 import com.caucho.config.ConfigException;
34 import com.caucho.ejb.cfg.CmrRelation;
35 import com.caucho.ejb.cfg.EjbEntityBean;
36 import com.caucho.util.CharBuffer;
37
38 /**
39  * Expression representing a single-valued relation path.
40  */

41 class RelationExpr extends PathExpr {
42   // base expression
43
private PathExpr _base;
44   // field name
45
private String JavaDoc _fieldName;
46
47   // relation information
48
private CmrRelation _relation;
49
50   private String JavaDoc _keyTable;
51   private String JavaDoc []_keyColumns;
52
53   private boolean _usesField;
54
55   /**
56    * Creates a new field expression.
57    *
58    * @param base the base expression
59    * @param field name of the field
60    */

61   RelationExpr(Query query, PathExpr base,
62                String JavaDoc field, CmrRelation relation)
63     throws ConfigException
64   {
65     super(relation.getTargetBean());
66     
67     _query = query;
68     _base = base;
69     _fieldName = field;
70     _relation = relation;
71     
72     // base.setUsesField();
73

74     /*
75     setJavaType(relation.getJavaType());
76
77     _keyTable = base.getKeyTable();
78     _keyColumns = relation.getTargetSQLColumns();
79     
80     if (relation.isImplicit())
81       setUsesField();
82     */

83   }
84
85   /**
86    * Gets the Java Type of the expression.
87    */

88   public JClass getJavaType()
89   {
90     return _relation.getTargetType();
91   }
92
93   void setUsesField()
94   {
95     if (_usesField)
96       return;
97
98     _usesField = true;
99
100     /*
101     _keyTable = "caucho" + _query.getUnique();
102
103     _query.addFromItem(_keyTable, _bean.getSQLTable());
104     _keyColumns = _relation.addCommonTargetLinks(_query,
105                          _base.getTable(),
106                          _keyTable);
107     */

108     //throw new UnsupportedOperationException();
109
}
110
111   String JavaDoc getKeyTable()
112   {
113     return _keyTable;
114   }
115
116   String JavaDoc []getKeyFields()
117   {
118     return _keyColumns;
119   }
120
121   String JavaDoc getTable()
122   {
123     return _keyTable;
124   }
125
126   /**
127    * Returns the identifier name.
128    */

129   String JavaDoc getName()
130   {
131     return _keyTable;
132   }
133
134   /**
135    * Returns the persistent bean this id is a member of
136    */

137   EjbEntityBean getBean()
138   {
139     return _bean;
140   }
141
142   EjbEntityBean getItemBean()
143   {
144     return _bean;
145   }
146
147   /**
148    * Returns the EJB name.
149    */

150   String JavaDoc getReturnEJB()
151   {
152     return _bean.getEJBName();
153   }
154
155   int getComponentCount()
156   {
157     return getItemBean().getEntityType().getId().getKeyCount();
158   }
159
160   /**
161    * Prints the select SQL for this expression
162    *
163    * @param gen the java code generator
164    */

165   void generateSelect(CharBuffer cb)
166   {
167     generateWhere(cb);
168   }
169
170   /**
171    * Prints the select SQL for this expression
172    *
173    * @param gen the java code generator
174    */

175   String JavaDoc getSelectTable(CharBuffer cb)
176   {
177     return getKeyTable();
178   }
179
180   /**
181    * Prints the where SQL for this expression
182    *
183    * @param gen the java code generator
184    */

185   void generateWhere(CharBuffer cb)
186   {
187     _base.generateWhere(cb);
188     cb.append(".");
189     cb.append(_fieldName);
190   }
191
192   /*
193   void generateComponent(CharBuffer cb, int index)
194   {
195     // generateWhere(cb);
196     cb.append(getKeyTable());
197     cb.append(".");
198     cb.append(getKeyFields()[index]);
199   }
200   */

201
202   void generateComponent(CharBuffer cb, int index)
203   {
204     EjbEntityBean bean = getItemBean();
205
206     EntityType type = bean.getEntityType();
207     
208     _base.generateWhere(cb);
209     cb.append(".");
210     cb.append(_fieldName);
211     cb.append(".");
212
213     cb.append(keyComponent(type, index));
214   }
215   
216   /**
217    * Returns true if the two expressions are equal
218    */

219   public boolean equals(Object JavaDoc bObj)
220   {
221     if (! (bObj instanceof RelationExpr))
222       return false;
223
224     RelationExpr b = (RelationExpr) bObj;
225
226     return _fieldName.equals(b._fieldName) && _base.equals(b._base);
227   }
228
229   /**
230    * Returns a hash code for the expression
231    */

232   public int hashCode()
233   {
234     return 65531 * _fieldName.hashCode() + _base.hashCode();
235   }
236
237   /**
238    * Printable version of the object.
239    */

240   public String JavaDoc toString()
241   {
242     return String.valueOf(_base) + '.' + _fieldName;
243   }
244 }
245
Popular Tags