KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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

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

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

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

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

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

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

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

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

182   public void generateJoin(CharBuffer cb)
183   {
184     cb.append(_linkColumns.generateJoin(_sourceFromItem.getName(),
185                     _targetFromItem.getName()));
186   }
187
188   /**
189    * Test for equality.
190    */

191   public boolean equals(Object o)
192   {
193     if (! (o instanceof OneToManyJoinExpr))
194       return false;
195
196     OneToManyJoinExpr joinExpr = (OneToManyJoinExpr) o;
197
198     return (_linkColumns.equals(joinExpr._linkColumns) &&
199         _targetFromItem.equals(joinExpr._targetFromItem) &&
200         _sourceFromItem.equals(joinExpr._sourceFromItem));
201   }
202   
203
204   public String toString()
205   {
206     return ("OneToManyJoinExpr[" + _linkColumns + "," +
207         _targetFromItem + "," + _sourceFromItem + "]");
208   }
209 }
210
Popular Tags