KickJava   Java API By Example, From Geeks To Geeks.

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


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.query.FromItem;
32 import com.caucho.amber.query.QueryParser;
33 import com.caucho.amber.table.LinkColumns;
34 import com.caucho.amber.type.RelatedType;
35 import com.caucho.util.CharBuffer;
36
37 /**
38  * Bound identifier expression.
39  */

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

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

61   public RelatedType getTargetType()
62   {
63     return _linkColumns.getSourceTable().getType();
64   }
65
66   /**
67    * Binds the expression as a select item.
68    */

69   public AmberExpr bindSelect(QueryParser parser)
70   {
71     // commented out: jpa/10b1
72
// if (_fromItem == null)
73
// _fromItem = _parent.bindSubPath(parser);
74

75     _fromItem = bindSubPath(parser);
76
77     return this;
78   }
79
80   /**
81    * Return the child from item.
82    */

83   public FromItem getChildFromItem()
84   {
85     return _childFromItem;
86   }
87
88   /**
89    * Binds the expression as a subpath.
90    */

91   public FromItem bindSubPath(QueryParser parser)
92   {
93     if (_childFromItem != null)
94       return _childFromItem;
95
96     DependentEntityOneToOneExpr pathExpr;
97     pathExpr = (DependentEntityOneToOneExpr) parser.addPath(this);
98
99     if (pathExpr != this) {
100       _fromItem = pathExpr._fromItem;
101       _childFromItem = pathExpr._childFromItem;
102
103       return _childFromItem;
104     }
105
106     _fromItem = _parent.bindSubPath(parser);
107
108     // added: jpa/10b0
109
_childFromItem = parser.getSelectQuery().createFromItem(_linkColumns.getSourceTable(),
110                                                             parser.createTableName());
111
112     // commented out: jpa/10b0
113
// if (_fromItem != null) {
114
// _childFromItem = _fromItem.getQuery().createFromItem(_linkColumns.getSourceTable(),
115
// parser.createTableName());
116
// } else {
117
// _childFromItem = parser.getSelectQuery().createFromItem(_linkColumns.getSourceTable(),
118
// parser.createTableName());
119
// }
120

121     JoinExpr join = new ManyToOneJoinExpr(_linkColumns,
122                                           _childFromItem,
123                                           _fromItem);
124
125     _childFromItem.setJoinExpr(join);
126
127     return _childFromItem;
128   }
129
130   /**
131    * Returns true if the expression uses the from item.
132    */

133   public boolean usesFrom(FromItem from, int type, boolean isNot)
134   {
135     return (_childFromItem == from && type == IS_INNER_JOIN ||
136             _fromItem == from ||
137             _parent.usesFrom(from, type));
138   }
139
140   /**
141    * Returns the table.
142    */

143   /*
144     public Table getTable()
145     {
146     if (_childFromItem != null)
147     return _childFromItem.getTable();
148     else if (_fromItem != null)
149     return _fromItem.getTable();
150     else
151     return _parent.getTable();
152     }
153   */

154
155   /**
156    * Generates the where expression.
157    */

158   public void generateMatchArgWhere(CharBuffer cb)
159   {
160     if (_fromItem != null) {
161       cb.append(_linkColumns.generateMatchArgSQL(_fromItem.getName()));
162     }
163     else {
164       cb.append(_linkColumns.generateMatchArgSQL(_parent.getChildFromItem().getName()));
165     }
166   }
167
168   /**
169    * Generates the where expression.
170    */

171   public void generateWhere(CharBuffer cb)
172   {
173     generateInternalWhere(cb, true);
174   }
175
176   /**
177    * Generates the (update) where expression.
178    */

179   public void generateUpdateWhere(CharBuffer cb)
180   {
181     generateInternalWhere(cb, false);
182   }
183
184   public String JavaDoc toString()
185   {
186     return "OneToOneExpr[" + _parent + "," + _linkColumns + "]";
187   }
188
189   public int hashCode()
190   {
191     return 65521 * _parent.hashCode() + _linkColumns.hashCode();
192   }
193
194   public boolean equals(Object JavaDoc o)
195   {
196     if (o == null || ! getClass().equals(o.getClass()))
197       return false;
198
199     DependentEntityOneToOneExpr oneToOne = (DependentEntityOneToOneExpr) o;
200
201     return (_parent.equals(oneToOne._parent) &&
202             _linkColumns.equals(oneToOne._linkColumns));
203   }
204
205   //
206
// private
207

208   private void generateInternalWhere(CharBuffer cb,
209                                      boolean select)
210   {
211     if (_fromItem != null) {
212
213       String JavaDoc s;
214
215       if (select) {
216
217         s = _fromItem.getName();
218
219         cb.append(s);
220         cb.append('.');
221       }
222
223       // commented out: jpa/0o12
224
// s = _linkColumns.getColumns().get(0).getName();
225

226       s = _childFromItem.getTable().getIdColumns().get(0).getName();
227
228       cb.append(s);
229     }
230     else {
231
232       if (select)
233         super.generateWhere(cb);
234       else
235         super.generateUpdateWhere(cb);
236     }
237   }
238 }
239
Popular Tags