KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Links two tables.
39  *
40  * The parent table is "b" in "b.next".
41  * The child table is "b.next"
42  *
43  * The source is "b", i.e. the parent.
44  * The target is "b.next", i.e. the child.
45  */

46 public class ManyToOneJoinExpr extends JoinExpr {
47   private LinkColumns _linkColumns;
48
49   private FromItem _sourceFromItem;
50   private FromItem _targetFromItem;
51
52   /**
53    * Creates the expr.
54    */

55   public ManyToOneJoinExpr(LinkColumns link,
56                            FromItem source,
57                            FromItem target)
58   {
59     _linkColumns = link;
60
61     _sourceFromItem = source;
62     _targetFromItem = target;
63
64     // commented out: jpa/10c9
65
// if (source == null || target == null)
66

67     if (target == null)
68       throw new NullPointerException JavaDoc();
69   }
70
71   /**
72    * Returns true for a boolean expression.
73    */

74   public boolean isBoolean()
75   {
76     return true;
77   }
78
79   /**
80    * Returns true for a many-to-many expression.
81    */

82   public boolean isManyToMany()
83   {
84     if (_sourceFromItem == null)
85       return false;
86
87     return _sourceFromItem.getJoinExpr() instanceof OneToManyJoinExpr;
88   }
89
90   /**
91    * Binds the expression as a select item.
92    */

93   public AmberExpr bindSelect(QueryParser parser)
94   {
95     return this;
96   }
97
98   /**
99    * Binds the link to the from item.
100    */

101   public boolean bindToFromItem()
102   {
103     if (_targetFromItem.getJoinExpr() == null ||
104         _targetFromItem.getJoinExpr().equals(this)) {
105       _targetFromItem.setJoinExpr(this);
106       return true;
107     }
108     else if (_sourceFromItem.getJoinExpr() == null) {
109       _sourceFromItem.setJoinExpr(new OneToManyJoinExpr(_linkColumns,
110                                                         _sourceFromItem,
111                                                         _targetFromItem));
112
113       return true;
114     }
115     else
116       return false;
117   }
118
119   /**
120    * Returns the target join clause.
121    */

122   public FromItem getJoinTarget()
123   {
124     return _targetFromItem;
125   }
126
127   /**
128    * Returns the target join clause.
129    */

130   public FromItem getJoinParent()
131   {
132     return _sourceFromItem;
133   }
134
135   /**
136    * Returns true if the expression uses the from item.
137    */

138   public boolean usesFrom(FromItem from, int type, boolean isNot)
139   {
140     return from == _targetFromItem || from == _sourceFromItem;
141   }
142
143   /**
144    * Returns true if the expression uses the from item.
145    */

146   @Override JavaDoc
147   public boolean exists(FromItem from)
148   {
149     return false;
150   }
151
152   /**
153    * Returns the id expr with the joined expression.
154    */

155   public AmberExpr replace(KeyColumnExpr id)
156   {
157     PathExpr parent = (PathExpr) id.getParent();
158
159     if (parent.getChildFromItem() != _targetFromItem)
160       return id;
161
162     ForeignColumn sourceColumn = _linkColumns.getSourceColumn(id.getColumn());
163
164     if (sourceColumn == null)
165       throw new IllegalStateException JavaDoc(id.getColumn().getName());
166
167     return new ColumnExpr(_sourceFromItem.getIdExpr(), sourceColumn);
168   }
169
170   /**
171    * Returns the id expr with the joined expression.
172    */

173   public AmberExpr replace(IdExpr id)
174   {
175     return id;
176   }
177
178   /**
179    * Generates the where expression.
180    */

181   public void generateWhere(CharBuffer cb)
182   {
183     String JavaDoc sourceName = null;
184
185     // jpa/10c9
186
if (_sourceFromItem != null)
187       sourceName = _sourceFromItem.getName();
188
189     String JavaDoc targetName = _targetFromItem.getName();
190
191     cb.append(_linkColumns.generateWhere(sourceName,
192                                          targetName));
193   }
194
195   /**
196    * Generates the (update) where expression.
197    */

198   public void generateUpdateWhere(CharBuffer cb)
199   {
200     generateWhere(cb);
201   }
202
203   /**
204    * Generates the having expression.
205    */

206   public void generateHaving(CharBuffer cb)
207   {
208     generateWhere(cb);
209   }
210
211   /**
212    * Generates the where expression.
213    */

214   public void generateJoin(CharBuffer cb)
215   {
216     cb.append(_linkColumns.generateJoin(_sourceFromItem.getName(),
217                                         _targetFromItem.getName()));
218   }
219
220   /**
221    * Test for equality.
222    */

223   public boolean equals(Object JavaDoc o)
224   {
225     if (! (o instanceof ManyToOneJoinExpr))
226       return false;
227
228     ManyToOneJoinExpr joinExpr = (ManyToOneJoinExpr) o;
229
230     return (_linkColumns.equals(joinExpr._linkColumns) &&
231             _sourceFromItem.equals(joinExpr._sourceFromItem) &&
232             _targetFromItem.equals(joinExpr._targetFromItem));
233   }
234
235
236   public String JavaDoc toString()
237   {
238     return ("ManyToOneJoinExpr[" + _linkColumns + "," +
239             _sourceFromItem + "," + _targetFromItem + "]");
240   }
241 }
242
Popular Tags