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.ForeignColumn;
034: import com.caucho.amber.table.LinkColumns;
035: import com.caucho.util.CharBuffer;
036:
037: /**
038: * Links two tables.
039: *
040: * The parent table is "b" in "b.next".
041: * The child table is "b.next"
042: *
043: * The source is "b", i.e. the parent.
044: * The target is "b.next", i.e. the child.
045: */
046: public class ManyToOneJoinExpr extends JoinExpr {
047: private LinkColumns _linkColumns;
048:
049: private FromItem _sourceFromItem;
050: private FromItem _targetFromItem;
051:
052: /**
053: * Creates the expr.
054: */
055: public ManyToOneJoinExpr(LinkColumns link, FromItem source,
056: FromItem target) {
057: _linkColumns = link;
058:
059: _sourceFromItem = source;
060: _targetFromItem = target;
061:
062: // commented out: jpa/10c9
063: // if (source == null || target == null)
064:
065: if (target == null)
066: throw new NullPointerException();
067: }
068:
069: /**
070: * Returns true for a boolean expression.
071: */
072: public boolean isBoolean() {
073: return true;
074: }
075:
076: /**
077: * Returns true for a many-to-many expression.
078: */
079: public boolean isManyToMany() {
080: if (_sourceFromItem == null)
081: return false;
082:
083: return _sourceFromItem.getJoinExpr() instanceof OneToManyJoinExpr;
084: }
085:
086: /**
087: * Binds the expression as a select item.
088: */
089: public AmberExpr bindSelect(QueryParser parser) {
090: return this ;
091: }
092:
093: /**
094: * Binds the link to the from item.
095: */
096: public boolean bindToFromItem() {
097: if (_targetFromItem.getJoinExpr() == null
098: || _targetFromItem.getJoinExpr().equals(this )) {
099: _targetFromItem.setJoinExpr(this );
100: return true;
101: } else if (_sourceFromItem.getJoinExpr() == null) {
102: _sourceFromItem.setJoinExpr(new OneToManyJoinExpr(
103: _linkColumns, _sourceFromItem, _targetFromItem));
104:
105: return true;
106: } else
107: return false;
108: }
109:
110: /**
111: * Returns the target join clause.
112: */
113: public FromItem getJoinTarget() {
114: return _targetFromItem;
115: }
116:
117: /**
118: * Returns the target join clause.
119: */
120: public FromItem getJoinParent() {
121: return _sourceFromItem;
122: }
123:
124: /**
125: * Returns true if the expression uses the from item.
126: */
127: public boolean usesFrom(FromItem from, int type, boolean isNot) {
128: return from == _targetFromItem || from == _sourceFromItem;
129: }
130:
131: /**
132: * Returns true if the expression uses the from item.
133: */
134: @Override
135: public boolean exists(FromItem from) {
136: return false;
137: }
138:
139: /**
140: * Returns the id expr with the joined expression.
141: */
142: public AmberExpr replace(KeyColumnExpr id) {
143: PathExpr parent = (PathExpr) id.getParent();
144:
145: if (parent.getChildFromItem() != _targetFromItem)
146: return id;
147:
148: ForeignColumn sourceColumn = _linkColumns.getSourceColumn(id
149: .getColumn());
150:
151: if (sourceColumn == null)
152: throw new IllegalStateException(id.getColumn().getName());
153:
154: return new ColumnExpr(_sourceFromItem.getIdExpr(), sourceColumn);
155: }
156:
157: /**
158: * Returns the id expr with the joined expression.
159: */
160: public AmberExpr replace(IdExpr id) {
161: return id;
162: }
163:
164: /**
165: * Generates the where expression.
166: */
167: public void generateWhere(CharBuffer cb) {
168: String sourceName = null;
169:
170: // jpa/10c9
171: if (_sourceFromItem != null)
172: sourceName = _sourceFromItem.getName();
173:
174: String targetName = _targetFromItem.getName();
175:
176: cb.append(_linkColumns.generateWhere(sourceName, targetName));
177: }
178:
179: /**
180: * Generates the (update) where expression.
181: */
182: public void generateUpdateWhere(CharBuffer cb) {
183: generateWhere(cb);
184: }
185:
186: /**
187: * Generates the having expression.
188: */
189: public void generateHaving(CharBuffer cb) {
190: generateWhere(cb);
191: }
192:
193: /**
194: * Generates the where expression.
195: */
196: public void generateJoin(CharBuffer cb) {
197: cb.append(_linkColumns.generateJoin(_sourceFromItem.getName(),
198: _targetFromItem.getName()));
199: }
200:
201: /**
202: * Test for equality.
203: */
204: public boolean equals(Object o) {
205: if (!(o instanceof ManyToOneJoinExpr))
206: return false;
207:
208: ManyToOneJoinExpr joinExpr = (ManyToOneJoinExpr) o;
209:
210: return (_linkColumns.equals(joinExpr._linkColumns)
211: && _sourceFromItem.equals(joinExpr._sourceFromItem) && _targetFromItem
212: .equals(joinExpr._targetFromItem));
213: }
214:
215: public String toString() {
216: return ("ManyToOneJoinExpr[" + _linkColumns + ","
217: + _sourceFromItem + "," + _targetFromItem + "]");
218: }
219: }
|