001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.amber.expr;
030:
031: import com.caucho.amber.query.FromItem;
032: import com.caucho.amber.query.QueryParser;
033: import com.caucho.amber.table.Column;
034: import com.caucho.util.CharBuffer;
035:
036: import java.util.ArrayList;
037:
038: /**
039: * Links two tables by their key fields.
040: */
041: public class EqualJoinExpr extends JoinExpr {
042: private ArrayList<Column> _keyColumns;
043:
044: private FromItem _fromItemA;
045: private FromItem _fromItemB;
046:
047: /**
048: * Creates the expr.
049: */
050: EqualJoinExpr(ArrayList<Column> keyColumns, FromItem fromItemA,
051: FromItem fromItemB) {
052: _keyColumns = keyColumns;
053:
054: _fromItemA = fromItemA;
055: _fromItemB = fromItemB;
056:
057: if (fromItemA == null || fromItemB == null)
058: throw new NullPointerException();
059: }
060:
061: /**
062: * Returns true for a boolean expression.
063: */
064: public boolean isBoolean() {
065: return true;
066: }
067:
068: /**
069: * Binds the expression as a select item.
070: */
071: public AmberExpr bindSelect(QueryParser parser) {
072: return this ;
073: }
074:
075: /**
076: * Binds the link to the from item.
077: */
078: public boolean bindToFromItem() {
079: if (_fromItemA.getJoinExpr() == null
080: || _fromItemA.getJoinExpr().equals(this )) {
081: _fromItemA.setJoinExpr(this );
082: return true;
083: } else if (_fromItemB.getJoinExpr() == null) {
084: _fromItemB.setJoinExpr(this );
085:
086: return true;
087: } else
088: return false;
089: }
090:
091: /**
092: * Returns the parent join clause.
093: */
094: public FromItem getJoinTarget() {
095: return _fromItemA;
096: }
097:
098: /**
099: * Returns the id expr with the joined expression.
100: */
101: public AmberExpr replace(IdFieldExpr id) {
102: IdExpr parent = (IdExpr) id.getParent();
103:
104: if (parent.getFromItem() == _fromItemA)
105: return new ColumnExpr(new IdExpr(_fromItemA), id
106: .getColumn());
107: else if (parent.getFromItem() == _fromItemB)
108: return new ColumnExpr(new IdExpr(_fromItemB), id
109: .getColumn());
110: else
111: return id;
112: }
113:
114: /**
115: * Returns the id expr with the joined expression.
116: */
117: public AmberExpr replace(IdExpr id) {
118: return id;
119: }
120:
121: /**
122: * Generates the where expression.
123: */
124: public void generateWhere(CharBuffer cb) {
125: generateInternalWhere(cb, true);
126: }
127:
128: /**
129: * Generates the (update) where expression.
130: */
131: public void generateUpdateWhere(CharBuffer cb) {
132: generateInternalWhere(cb, false);
133: }
134:
135: /**
136: * Generates the having expression.
137: */
138: public void generateHaving(CharBuffer cb) {
139: generateWhere(cb);
140: }
141:
142: /**
143: * Test for equality.
144: */
145: public boolean equals(Object o) {
146: if (!(o instanceof EqualJoinExpr))
147: return false;
148:
149: EqualJoinExpr equalExpr = (EqualJoinExpr) o;
150:
151: return (_keyColumns.equals(equalExpr._keyColumns)
152: && _fromItemA.equals(equalExpr._fromItemA) && _fromItemB
153: .equals(equalExpr._fromItemB));
154: }
155:
156: public String toString() {
157: return ("EqualJoinExpr[" + _keyColumns + "," + _fromItemA + ","
158: + _fromItemB + "]");
159: }
160:
161: //
162: // private
163:
164: private void generateInternalWhere(CharBuffer cb, boolean select) {
165: cb.append('(');
166:
167: for (int i = 0; i < _keyColumns.size(); i++) {
168: Column column = _keyColumns.get(i);
169:
170: if (i != 0)
171: cb.append(" AND ");
172:
173: cb.append(_fromItemA.getName());
174: cb.append('.');
175: cb.append(column.getName());
176:
177: cb.append('=');
178:
179: cb.append(_fromItemB.getName());
180: cb.append('.');
181: cb.append(column.getName());
182: }
183:
184: cb.append('(');
185: }
186: }
|