KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 1998-2004 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.amber.query;
30
31 import com.caucho.util.CharBuffer;
32
33 import com.caucho.amber.type.Type;
34 import com.caucho.amber.type.EntityType;
35
36 import com.caucho.amber.table.Table;
37 import com.caucho.amber.table.LinkColumns;
38
39 /**
40  * Link expression to a new table
41  */

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

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

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

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

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

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

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

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

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

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

161   public boolean usesFrom(FromItem from, int type, boolean isNot)
162   {
163     return (_childFromItem == from && type == IS_INNER_JOIN ||
164         _fromItem == from ||
165         _parent.usesFrom(from, type));
166   }
167
168   /**
169    * Returns the table.
170    */

171   /*
172   public Table getTable()
173   {
174     if (_childFromItem != null)
175       return _childFromItem.getTable();
176     else if (_fromItem != null)
177       return _fromItem.getTable();
178     else
179       return _parent.getTable();
180   }
181   */

182   
183   /**
184    * Generates the where expression.
185    */

186   public void generateMatchArgWhere(CharBuffer cb)
187   {
188     if (_fromItem != null) {
189       cb.append(_linkColumns.generateMatchArgSQL(_fromItem.getName()));
190     }
191     else {
192       cb.append(_linkColumns.generateMatchArgSQL(_parent.getChildFromItem().getName()));
193     }
194   }
195   
196   /**
197    * Generates the select expression.
198    */

199   // ejb/06q4
200
public void generateSelect(CharBuffer cb)
201   {
202     String tableName;
203     
204     if (_fromItem != null)
205       tableName = _fromItem.getName();
206     else
207       tableName = _parent.getChildFromItem().getName();
208
209     cb.append(_linkColumns.generateSelectSQL(tableName));
210   }
211
212   public int hashCode()
213   {
214     return 65521 * _parent.hashCode() + _linkColumns.hashCode();
215   }
216
217   public boolean equals(Object o)
218   {
219     if (o == null || ! getClass().equals(o.getClass()))
220       return false;
221
222     ManyToOneExpr manyToOne = (ManyToOneExpr) o;
223
224     return (_parent.equals(manyToOne._parent) &&
225         _linkColumns.equals(manyToOne._linkColumns));
226   }
227
228   public String toString()
229   {
230     return "ManyToOneExpr[" + _childFromItem + "," + _fromItem + "," + _parent + "]";
231   }
232 }
233
Popular Tags