KickJava   Java API By Example, From Geeks To Geeks.

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


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.Column;
34 import com.caucho.util.CharBuffer;
35
36 import java.util.ArrayList JavaDoc;
37
38 /**
39  * Links two tables by their key fields.
40  */

41 public class EqualJoinExpr extends JoinExpr {
42   private ArrayList JavaDoc<Column> _keyColumns;
43
44   private FromItem _fromItemA;
45   private FromItem _fromItemB;
46
47   /**
48    * Creates the expr.
49    */

50   EqualJoinExpr(ArrayList JavaDoc<Column> keyColumns,
51                 FromItem fromItemA,
52                 FromItem fromItemB)
53   {
54     _keyColumns = keyColumns;
55
56     _fromItemA = fromItemA;
57     _fromItemB = fromItemB;
58
59     if (fromItemA == null || fromItemB == null)
60       throw new NullPointerException JavaDoc();
61   }
62
63   /**
64    * Returns true for a boolean expression.
65    */

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

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

82   public boolean bindToFromItem()
83   {
84     if (_fromItemA.getJoinExpr() == null ||
85         _fromItemA.getJoinExpr().equals(this)) {
86       _fromItemA.setJoinExpr(this);
87       return true;
88     }
89     else if (_fromItemB.getJoinExpr() == null) {
90       _fromItemB.setJoinExpr(this);
91
92       return true;
93     }
94     else
95       return false;
96   }
97
98   /**
99    * Returns the parent join clause.
100    */

101   public FromItem getJoinTarget()
102   {
103     return _fromItemA;
104   }
105
106   /**
107    * Returns the id expr with the joined expression.
108    */

109   public AmberExpr replace(IdFieldExpr id)
110   {
111     IdExpr parent = (IdExpr) id.getParent();
112
113     if (parent.getFromItem() == _fromItemA)
114       return new ColumnExpr(new IdExpr(_fromItemA), id.getColumn());
115     else if (parent.getFromItem() == _fromItemB)
116       return new ColumnExpr(new IdExpr(_fromItemB), id.getColumn());
117     else
118       return id;
119   }
120
121   /**
122    * Returns the id expr with the joined expression.
123    */

124   public AmberExpr replace(IdExpr id)
125   {
126     return id;
127   }
128
129   /**
130    * Generates the where expression.
131    */

132   public void generateWhere(CharBuffer cb)
133   {
134     generateInternalWhere(cb, true);
135   }
136
137   /**
138    * Generates the (update) where expression.
139    */

140   public void generateUpdateWhere(CharBuffer cb)
141   {
142     generateInternalWhere(cb, false);
143   }
144
145   /**
146    * Generates the having expression.
147    */

148   public void generateHaving(CharBuffer cb)
149   {
150     generateWhere(cb);
151   }
152
153   /**
154    * Test for equality.
155    */

156   public boolean equals(Object JavaDoc o)
157   {
158     if (! (o instanceof EqualJoinExpr))
159       return false;
160
161     EqualJoinExpr equalExpr = (EqualJoinExpr) o;
162
163     return (_keyColumns.equals(equalExpr._keyColumns) &&
164             _fromItemA.equals(equalExpr._fromItemA) &&
165             _fromItemB.equals(equalExpr._fromItemB));
166   }
167
168
169   public String JavaDoc toString()
170   {
171     return ("EqualJoinExpr[" + _keyColumns + "," +
172             _fromItemA + "," + _fromItemB + "]");
173   }
174
175   //
176
// private
177

178   private void generateInternalWhere(CharBuffer cb,
179                                      boolean select)
180   {
181     cb.append('(');
182
183     for (int i = 0; i < _keyColumns.size(); i++) {
184       Column column = _keyColumns.get(i);
185
186       if (i != 0)
187         cb.append(" AND ");
188
189       cb.append(_fromItemA.getName());
190       cb.append('.');
191       cb.append(column.getName());
192
193       cb.append('=');
194
195       cb.append(_fromItemB.getName());
196       cb.append('.');
197       cb.append(column.getName());
198     }
199
200     cb.append('(');
201   }
202 }
203
Popular Tags