KickJava   Java API By Example, From Geeks To Geeks.

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


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.EntityType;
34
35 import com.caucho.amber.field.AmberField;
36 import com.caucho.amber.field.IdField;
37 import com.caucho.amber.field.KeyManyToOneField;
38 import com.caucho.amber.field.KeyPropertyField;
39
40 import com.caucho.amber.table.Column;
41
42 /**
43  * Bound identifier expression.
44  */

45 public class KeyManyToOneExpr extends AbstractPathExpr
46   implements IdFieldExpr {
47   private PathExpr _parent;
48   // identifier name value
49
private KeyManyToOneField _manyToOne;
50
51   private FromItem _fromItem;
52   private FromItem _childFromItem;
53
54   /**
55    * Creates a new unbound id expression.
56    */

57   public KeyManyToOneExpr(PathExpr parent, KeyManyToOneField manyToOne)
58   {
59     _parent = parent;
60     _manyToOne = manyToOne;
61   }
62
63   /**
64    * Returns the parent expression.
65    */

66   public PathExpr getParent()
67   {
68     return _parent;
69   }
70
71   /**
72    * Returns the name.
73    */

74   public IdField getField()
75   {
76     return _manyToOne;
77   }
78
79   /**
80    * Returns the entity class.
81    */

82   public EntityType getTargetType()
83   {
84     return _manyToOne.getEntityType();
85   }
86
87   /**
88    * Returns the column.
89    */

90   public Column getColumn()
91   {
92     throw new UnsupportedOperationException();
93   }
94   
95   /**
96    * Creates the expr from the path.
97    */

98   public AmberExpr createField(QueryParser parser, String name)
99   {
100     AmberField field = getTargetType().getField(name);
101
102     if (field == null)
103       return null;
104     else if (field instanceof IdField) {
105       KeyPropertyField idField = _manyToOne.getIdField((IdField) field);
106
107       return idField.createExpr(parser, _parent);
108     }
109     else
110       return field.createExpr(parser, this);
111   }
112
113   /**
114    * Binds the expression as a select item.
115    */

116   public AmberExpr bindSelect(QueryParser parser)
117   {
118     _fromItem = _parent.bindSubPath(parser);
119
120     return this;
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     KeyManyToOneExpr pathExpr = (KeyManyToOneExpr) parser.addPath(this);
140
141     if (pathExpr != this) {
142       _fromItem = pathExpr._fromItem;
143       _childFromItem = pathExpr._childFromItem;
144
145       return _childFromItem;
146     }
147     
148     _fromItem = _parent.bindSubPath(parser);
149
150     if (_fromItem != null)
151       _childFromItem = _fromItem.getQuery().createFromItem(getTargetType().getTable(),
152                                parser.createTableName());
153     else
154       _childFromItem = parser.getSelectQuery().createFromItem(getTargetType().getTable(),
155                             parser.createTableName());
156
157     JoinExpr link = new ManyToOneJoinExpr(_manyToOne.getLinkColumns(),
158                       _fromItem,
159                       _childFromItem);
160
161     _childFromItem.setJoinExpr(link);
162     
163     return _childFromItem;
164   }
165
166   /**
167    * Returns true if the expression uses the from item.
168    */

169   public boolean usesFrom(FromItem from, int type, boolean isNot)
170   {
171     return from == _childFromItem || _parent.usesFrom(from, type);
172   }
173
174   /**
175    * Replaces linked join to eliminate a table.
176    */

177   /*
178   public AmberExpr replaceJoin(JoinExpr join)
179   {
180     if (_parent instanceof IdExpr) {
181       return join.replace(this);
182     }
183     else
184       return super.replaceJoin(join);
185   }
186   */

187
188   /**
189    * Returns the table.
190    */

191   /*
192   public String getTable()
193   {
194     if (_childFromItem != null)
195       return _childFromItem.getName();
196     else if (_fromItem != null)
197       return _fromItem.getName();
198     else
199       return _parent.getChildFromItem().getName();
200   }
201   */

202   
203   /**
204    * Generates the where expression.
205    */

206   public void generateMatchArgWhere(CharBuffer cb)
207   {
208     String table;
209     
210     if (_fromItem != null)
211       table = _fromItem.getName();
212     else
213       table = _parent.getChildFromItem().getName();
214     
215     cb.append(_manyToOne.getLinkColumns().generateMatchArgSQL(table));
216   }
217
218   public String toString()
219   {
220     return _parent + "." + getField();
221   }
222
223   public int hashCode()
224   {
225     return 65521 * _parent.hashCode() + getField().hashCode();
226   }
227
228   public boolean equals(Object o)
229   {
230     if (o == null || ! getClass().equals(o.getClass()))
231       return false;
232
233     KeyManyToOneExpr manyToOne = (KeyManyToOneExpr) o;
234
235     return (_parent.equals(manyToOne._parent) &&
236         getField().equals(manyToOne.getField()));
237   }
238 }
239
Popular Tags