KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > expr > EmbeddedExpr


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

28
29 package com.caucho.amber.expr;
30
31 import com.caucho.amber.field.EntityEmbeddedField;
32 import com.caucho.amber.query.FromItem;
33 import com.caucho.amber.query.QueryParser;
34 import com.caucho.amber.table.Column;
35 import com.caucho.amber.type.EmbeddableType;
36 import com.caucho.amber.type.Type;
37 import com.caucho.util.CharBuffer;
38
39 import java.util.HashMap JavaDoc;
40 import java.util.Map JavaDoc;
41
42 /**
43  * Embedded path expression
44  */

45 public class EmbeddedExpr extends AbstractPathExpr {
46   private PathExpr _parent;
47
48   private EmbeddableType _embeddableType;
49
50   private HashMap JavaDoc<String JavaDoc, Column> _columns;
51
52   private HashMap JavaDoc<String JavaDoc, String JavaDoc> _fieldNameByColumn;
53
54   private FromItem _fromItem;
55   private FromItem _childFromItem;
56
57   /**
58    * Creates a new expression.
59    */

60   public EmbeddedExpr(PathExpr parent,
61                       EmbeddableType embeddableType,
62                       HashMap JavaDoc<String JavaDoc, Column> columns,
63                       HashMap JavaDoc<String JavaDoc, String JavaDoc> fieldNameByColumn)
64   {
65     _parent = parent;
66     _embeddableType = embeddableType;
67     _columns = columns;
68     _fieldNameByColumn = fieldNameByColumn;
69   }
70
71   /**
72    * Returns the target type.
73    */

74   public EmbeddableType getTargetType()
75   {
76     return _embeddableType;
77   }
78
79   /**
80    * Returns the target type.
81    */

82   public Type getType()
83   {
84     return _embeddableType;
85   }
86
87   /**
88    * Returns column by name.
89    */

90   public Column getColumnByFieldName(String JavaDoc fieldName)
91   {
92     // XXX: there is no reverse lookup.
93
for (Map.Entry JavaDoc<String JavaDoc, String JavaDoc> entry :
94            _fieldNameByColumn.entrySet()) {
95
96       String JavaDoc name = entry.getValue();
97
98       if (name.equals(fieldName))
99         return _columns.get(entry.getKey());
100     }
101
102     return null;
103   }
104
105   /**
106    * Binds the expression as a select item.
107    */

108   public AmberExpr bindSelect(QueryParser parser)
109   {
110     _fromItem = _parent.bindSubPath(parser);
111
112     return this;
113   }
114
115   /**
116    * Return the parent from item.
117    */

118   public FromItem getFromItem()
119   {
120     return _fromItem;
121   }
122
123   /**
124    * Return the child from item.
125    */

126   public FromItem getChildFromItem()
127   {
128     return _childFromItem;
129   }
130
131   /**
132    * Binds the expression as a subpath.
133    */

134   public FromItem bindSubPath(QueryParser parser)
135   {
136     // if (_childFromItem != null)
137
// return _childFromItem;
138

139     FromItem parentFromItem = _parent.bindSubPath(parser);
140
141     _fromItem = parentFromItem;
142
143     return _fromItem;
144
145     /*
146       EmbeddedExpr pathExpr = (EmbeddedExpr) parser.addPath(this);
147
148       if (pathExpr != this) {
149       _fromItem = pathExpr._fromItem;
150       _childFromItem = pathExpr._childFromItem;
151
152       return _childFromItem;
153       }
154
155       // XXX: handled at constructor?
156       _parent = _parent.bindSelect(parser, null);
157
158       bindSelect(parser, parser.createTableName());
159
160       return _childFromItem;
161     */

162   }
163
164   /**
165    * Binds the expression as a select item.
166    */

167   public PathExpr bindSelect(QueryParser parser, String JavaDoc id)
168   {
169     _fromItem = bindSubPath(parser);
170
171     return this;
172
173     /*
174       if (_childFromItem != null)
175       return this;
176
177       if (_fromItem == null)
178       _fromItem = _parent.bindSubPath(parser);
179
180       return this;
181     */

182   }
183
184   /**
185    * Returns true if the expression uses the from item.
186    */

187   public boolean usesFrom(FromItem from, int type, boolean isNot)
188   {
189     return (_childFromItem == from && type == IS_INNER_JOIN ||
190             _fromItem == from ||
191             _parent.usesFrom(from, type));
192   }
193
194   /**
195    * Generates the where expression.
196    */

197   public void generateMatchArgWhere(CharBuffer cb)
198   {
199     throw new UnsupportedOperationException JavaDoc();
200
201     /*
202       if (_fromItem != null) {
203       cb.append(_linkColumns.generateMatchArgSQL(_fromItem.getName()));
204       }
205       else {
206       cb.append(_linkColumns.generateMatchArgSQL(_parent.getChildFromItem().getName()));
207       }
208     */

209   }
210
211   /**
212    * Generates the where expression.
213    */

214   public void generateWhere(CharBuffer cb)
215   {
216     generateInternalWhere(cb, true);
217   }
218
219   /**
220    * Generates the (update) where expression.
221    */

222   public void generateUpdateWhere(CharBuffer cb)
223   {
224     generateInternalWhere(cb, false);
225   }
226
227   /**
228    * Generates the select expression.
229    */

230   public void generateSelect(CharBuffer cb)
231   {
232     cb.append(EntityEmbeddedField.generateSelect(null,
233                                                  _embeddableType,
234                                                  _fieldNameByColumn,
235                                                  _columns));
236   }
237
238   /**
239    * Returns the parent.
240    */

241   public PathExpr getParent()
242   {
243     return _parent;
244   }
245
246   public int hashCode()
247   {
248     return 65521 * _parent.hashCode() + _columns.hashCode();
249   }
250
251   public boolean equals(Object JavaDoc o)
252   {
253     if (o == null || ! getClass().equals(o.getClass()))
254       return false;
255
256     EmbeddedExpr embedded = (EmbeddedExpr) o;
257
258     return (_parent.equals(embedded._parent) &&
259             _columns.equals(embedded._columns));
260   }
261
262   public String JavaDoc toString()
263   {
264     return "EmbeddedExpr[" + _childFromItem + "," + _fromItem + "," + _parent + "]";
265   }
266
267   //
268
// private
269

270   private void generateInternalWhere(CharBuffer cb,
271                                      boolean select)
272   {
273     throw new UnsupportedOperationException JavaDoc();
274   }
275 }
276
Popular Tags