KickJava   Java API By Example, From Geeks To Geeks.

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


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

29
30 package com.caucho.amber.expr;
31
32 import com.caucho.amber.query.FromItem;
33 import com.caucho.amber.query.QueryParser;
34 import com.caucho.amber.table.LinkColumns;
35 import com.caucho.amber.table.Table;
36 import com.caucho.amber.type.RelatedType;
37 import com.caucho.amber.type.Type;
38 import com.caucho.util.CharBuffer;
39
40 /**
41  * Link expression to a new table
42  */

43 public class ManyToOneExpr extends AbstractPathExpr {
44   private PathExpr _parent;
45
46   private LinkColumns _linkColumns;
47
48   private FromItem _fromItem;
49   private FromItem _childFromItem;
50
51   /**
52    * Creates a new unbound id expression.
53    */

54   public ManyToOneExpr(PathExpr parent, LinkColumns linkColumns)
55   {
56     _parent = parent;
57     _linkColumns = linkColumns;
58   }
59
60   /**
61    * Returns the entity class.
62    */

63   public RelatedType getTargetType()
64   {
65     return _linkColumns.getTargetTable().getType();
66   }
67
68   /**
69    * Returns the entity class.
70    */

71   public Type getType()
72   {
73     return getTargetType();
74   }
75
76   /**
77    * Binds the expression as a select item.
78    */

79   public AmberExpr bindSelect(QueryParser parser)
80   {
81     _fromItem = _parent.bindSubPath(parser);
82
83     return this;
84   }
85
86   /**
87    * Return the parent from item.
88    */

89   public FromItem getFromItem()
90   {
91     return _fromItem;
92   }
93
94   /**
95    * Returns the link columns.
96    */

97   public LinkColumns getLinkColumns()
98   {
99     return _linkColumns;
100   }
101
102   /**
103    * Return the child from item.
104    */

105   public FromItem getChildFromItem()
106   {
107     return _childFromItem;
108   }
109
110   /**
111    * Binds the expression as a subpath.
112    */

113   public FromItem bindSubPath(QueryParser parser)
114   {
115     if (_childFromItem != null)
116       return _childFromItem;
117
118     ManyToOneExpr pathExpr = (ManyToOneExpr) parser.addPath(this);
119
120     if (pathExpr != this) {
121       _fromItem = pathExpr._fromItem;
122       _childFromItem = pathExpr._childFromItem;
123
124       return _childFromItem;
125     }
126
127     // XXX: handled at constructor?
128
_parent = _parent.bindSelect(parser, null);
129
130     bindSelect(parser, parser.createTableName());
131
132     return _childFromItem;
133   }
134
135   /**
136    * Binds the expression as a select item.
137    */

138   public PathExpr bindSelect(QueryParser parser, String JavaDoc id)
139   {
140     if (_childFromItem != null)
141       return this;
142
143     if (_fromItem == null)
144       _fromItem = _parent.bindSubPath(parser);
145
146     Table targetTable = _linkColumns.getTargetTable();
147     _childFromItem = parser.addFromItem(targetTable, id);
148
149     JoinExpr joinExpr;
150     joinExpr = new ManyToOneJoinExpr(_linkColumns,
151                                      _fromItem,
152                                      _childFromItem);
153
154     _childFromItem.setJoinExpr(joinExpr);
155
156     return this;
157   }
158
159   /**
160    * Returns true if the expression uses the from item.
161    */

162   public boolean usesFrom(FromItem from, int type, boolean isNot)
163   {
164     return (_childFromItem == from && type == IS_INNER_JOIN
165         || _fromItem == from
166         || _parent.usesFrom(from, type));
167   }
168
169   /**
170    * Returns true if the item is forced to exist by the parent
171    */

172   @Override JavaDoc
173   public boolean exists(FromItem from)
174   {
175     System.out.println("P: " + _parent);
176     return (_fromItem == from
177         && _parent.exists());
178   }
179
180   /**
181    * Returns the table.
182    */

183   /*
184     public Table getTable()
185     {
186     if (_childFromItem != null)
187     return _childFromItem.getTable();
188     else if (_fromItem != null)
189     return _fromItem.getTable();
190     else
191     return _parent.getTable();
192     }
193   */

194
195   /**
196    * Generates the where expression.
197    */

198   public void generateMatchArgWhere(CharBuffer cb)
199   {
200     if (_fromItem != null) {
201       cb.append(_linkColumns.generateMatchArgSQL(_fromItem.getName()));
202     }
203     else {
204       cb.append(_linkColumns.generateMatchArgSQL(_parent.getChildFromItem().getName()));
205     }
206   }
207
208   /**
209    * Generates the where expression.
210    */

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

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

227   // ejb/06q4
228
public void generateSelect(CharBuffer cb)
229   {
230     String JavaDoc tableName;
231
232     if (_fromItem != null)
233       tableName = _fromItem.getName();
234     else
235       tableName = _parent.getChildFromItem().getName();
236
237     cb.append(_linkColumns.generateSelectSQL(tableName));
238   }
239
240   /**
241    * Returns the parent.
242    */

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

272   private void generateInternalWhere(CharBuffer cb,
273                                      boolean select)
274   {
275     if (_fromItem != null) {
276
277       if (select) {
278         cb.append(_fromItem.getName());
279         cb.append('.');
280       }
281
282       cb.append(_linkColumns.getColumns().get(0).getName());
283     }
284     else {
285
286       if (select)
287         super.generateWhere(cb);
288       else
289         super.generateUpdateWhere(cb);
290     }
291   }
292 }
293
Popular Tags