KickJava   Java API By Example, From Geeks To Geeks.

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


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.ForeignColumn;
34 import com.caucho.amber.table.LinkColumns;
35 import com.caucho.util.CharBuffer;
36
37 /**
38  * Joins two tables as "a.children".
39  */

40 public class OneToManyJoinExpr extends JoinExpr {
41   private LinkColumns _linkColumns;
42
43   private FromItem _sourceFromItem;
44   private FromItem _targetFromItem;
45
46   /**
47    * Creates the expr.
48    */

49   public OneToManyJoinExpr(LinkColumns linkColumns,
50                            FromItem source,
51                            FromItem target)
52   {
53     _linkColumns = linkColumns;
54
55     _sourceFromItem = source;
56     _targetFromItem = target;
57
58     if (source == null || target == null)
59       throw new NullPointerException JavaDoc();
60   }
61
62   /**
63    * Returns true for a boolean expression.
64    */

65   public boolean isBoolean()
66   {
67     return true;
68   }
69
70   /**
71    * Binds the expression as a select item.
72    */

73   public AmberExpr bindSelect(QueryParser parser)
74   {
75     return this;
76   }
77
78   /**
79    * Binds the link to the from item.
80    */

81   public boolean bindToFromItem()
82   {
83     if (_sourceFromItem.getJoinExpr() == null ||
84         _sourceFromItem.getJoinExpr().equals(this)) {
85       _sourceFromItem.setJoinExpr(this);
86       return true;
87     }
88
89     ManyToOneJoinExpr manyToOne = new ManyToOneJoinExpr(_linkColumns,
90                                                         _sourceFromItem,
91                                                         _targetFromItem);
92
93     if (_targetFromItem.getJoinExpr() == null ||
94         _targetFromItem.getJoinExpr().equals(manyToOne)) {
95       _targetFromItem.setJoinExpr(manyToOne);
96
97       return true;
98     }
99     else
100       return false;
101   }
102
103   /**
104    * Returns the target join clause.
105    */

106   public FromItem getJoinTarget()
107   {
108     return _targetFromItem;
109   }
110
111   /**
112    * Returns the parent join clause, i.e. the first in the FROM order.
113    * <pre>
114    * FROM o, o.children
115    * <pre>
116    */

117   public FromItem getJoinParent()
118   {
119     return _targetFromItem;
120   }
121
122   /**
123    * Returns the id expr with the joined expression.
124    */

125   public AmberExpr replace(KeyColumnExpr id)
126   {
127     PathExpr parent = id.getParent();
128
129     if (parent.getChildFromItem() != _targetFromItem)
130       return id;
131
132     ForeignColumn sourceColumn = _linkColumns.getSourceColumn(id.getColumn());
133
134     if (sourceColumn == null)
135       throw new IllegalStateException JavaDoc(id.getColumn().getName());
136
137     return new ColumnExpr(_sourceFromItem.getIdExpr(), sourceColumn);
138   }
139
140   /**
141    * Returns the id expr with the joined expression.
142    */

143   public AmberExpr replace(IdExpr id)
144   {
145     return id;
146   }
147
148   /**
149    * Returns the where clause once the parent is removed
150    */

151   public AmberExpr getWhere()
152   {
153     /*
154      * The code is partially correct, but the is null test isn't
155      * needed in many cases, so should probably test the actual
156      * expression
157      AndExpr and = new AndExpr();
158      IdExpr id = new IdExpr(_sourceFromItem);
159
160      for (ForeignColumn column : _linkColumns.getColumns()) {
161      and.add(new UnaryExpr(QueryParser.NOT_NULL,
162      new ColumnExpr(id, column)));
163      }
164
165      return and.getSingle();
166     */

167
168     return null;
169   }
170
171   /**
172    * Generates the where expression.
173    */

174   public void generateWhere(CharBuffer cb)
175   {
176     cb.append(_linkColumns.generateWhere(_sourceFromItem.getName(),
177                                          _targetFromItem.getName()));
178   }
179
180   /**
181    * Generates the (update) where expression.
182    */

183   public void generateUpdateWhere(CharBuffer cb)
184   {
185     generateWhere(cb);
186   }
187
188   /**
189    * Generates the having expression.
190    */

191   public void generateHaving(CharBuffer cb)
192   {
193     generateWhere(cb);
194   }
195
196   /**
197    * Generates the where expression.
198    */

199   public void generateJoin(CharBuffer cb)
200   {
201     cb.append(_linkColumns.generateJoin(_sourceFromItem.getName(),
202                                         _targetFromItem.getName()));
203   }
204
205   /**
206    * Test for equality.
207    */

208   public boolean equals(Object JavaDoc o)
209   {
210     if (! (o instanceof OneToManyJoinExpr))
211       return false;
212
213     OneToManyJoinExpr joinExpr = (OneToManyJoinExpr) o;
214
215     return (_linkColumns.equals(joinExpr._linkColumns) &&
216             _targetFromItem.equals(joinExpr._targetFromItem) &&
217             _sourceFromItem.equals(joinExpr._sourceFromItem));
218   }
219
220
221   public String JavaDoc toString()
222   {
223     return ("OneToManyJoinExpr[" + _linkColumns + "," +
224             _targetFromItem + "," + _sourceFromItem + "]");
225   }
226 }
227
Popular Tags