KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

54   ManyToOneJoinExpr(LinkColumns link, FromItem source, FromItem target)
55   {
56     _linkColumns = link;
57     
58     _sourceFromItem = source;
59     _targetFromItem = target;
60
61     if (source == null || target == null)
62       throw new NullPointerException();
63   }
64   
65   /**
66    * Returns true for a boolean expression.
67    */

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

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

84   public boolean bindToFromItem()
85   {
86     if (_targetFromItem.getJoinExpr() == null ||
87     _targetFromItem.getJoinExpr().equals(this)) {
88       _targetFromItem.setJoinExpr(this);
89       return true;
90     }
91     else if (_sourceFromItem.getJoinExpr() == null) {
92       _sourceFromItem.setJoinExpr(new OneToManyJoinExpr(_linkColumns,
93                             _sourceFromItem,
94                             _targetFromItem));
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 target join clause.
112    */

113   FromItem getJoinParent()
114   {
115     return _sourceFromItem;
116   }
117
118   /**
119    * Returns true if the expression uses the from item.
120    */

121   public boolean usesFrom(FromItem from, int type, boolean isNot)
122   {
123     return from == _targetFromItem || from == _sourceFromItem;
124   }
125
126   /**
127    * Returns the id expr with the joined expression.
128    */

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

147   public AmberExpr replace(IdExpr id)
148   {
149     return id;
150   }
151   
152   /**
153    * Generates the where expression.
154    */

155   public void generateWhere(CharBuffer cb)
156   {
157     cb.append(_linkColumns.generateWhere(_sourceFromItem.getName(),
158                      _targetFromItem.getName()));
159   }
160   
161   /**
162    * Generates the where expression.
163    */

164   public void generateJoin(CharBuffer cb)
165   {
166     cb.append(_linkColumns.generateJoin(_sourceFromItem.getName(),
167                     _targetFromItem.getName()));
168   }
169
170   /**
171    * Test for equality.
172    */

173   public boolean equals(Object o)
174   {
175     if (! (o instanceof ManyToOneJoinExpr))
176       return false;
177
178     ManyToOneJoinExpr joinExpr = (ManyToOneJoinExpr) o;
179
180     return (_linkColumns.equals(joinExpr._linkColumns) &&
181         _sourceFromItem.equals(joinExpr._sourceFromItem) &&
182         _targetFromItem.equals(joinExpr._targetFromItem));
183   }
184   
185
186   public String toString()
187   {
188     return ("ManyToOneJoinExpr[" + _linkColumns + "," +
189         _sourceFromItem + "," + _targetFromItem + "]");
190   }
191 }
192
Popular Tags