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.LinkColumns;
034: import com.caucho.amber.table.Table;
035: import com.caucho.amber.type.EntityType;
036: import com.caucho.amber.type.Type;
037: import com.caucho.util.CharBuffer;
038:
039: /**
040: * Expression to a collection of rows
041: *
042: * The relation is maintained by a link from the child objects
043: * to the parent object.
044: */
045: public class OneToManyExpr extends AbstractPathExpr {
046: private PathExpr _parent;
047:
048: // Link from the target to the parent
049: private LinkColumns _linkColumns;
050:
051: private FromItem _fromItem;
052: private FromItem _childFromItem;
053:
054: /**
055: * Creates a new expression to the child objects.
056: */
057: public OneToManyExpr(QueryParser parser, PathExpr parent,
058: LinkColumns linkColumns) {
059: _parent = parent;
060:
061: _linkColumns = linkColumns;
062: }
063:
064: /**
065: * Returns the link columns.
066: */
067: public LinkColumns getLinkColumns() {
068: return _linkColumns;
069: }
070:
071: /**
072: * Returns the expr type.
073: */
074: public Type getType() {
075: return _linkColumns.getSourceTable().getType();
076: }
077:
078: /**
079: * Returns the expr type.
080: */
081: public EntityType getTargetType() {
082: return (EntityType) getType();
083: }
084:
085: /**
086: * Returns true if the expression uses the from item.
087: */
088: public boolean usesFrom(FromItem from, int type, boolean isNot) {
089: return (from == _childFromItem || type == IS_INNER_JOIN
090: && _parent.usesFrom(from, type));
091: }
092:
093: /**
094: * Returns the parent.
095: */
096: public PathExpr getParent() {
097: return _parent;
098: }
099:
100: /**
101: * Binds the expression as a select item.
102: */
103: public AmberExpr bindSelect(QueryParser parser) {
104: return bindSelect(parser, null);
105: }
106:
107: /**
108: * Binds the expression as a select item.
109: */
110: public PathExpr bindSelect(QueryParser parser, String id) {
111: if (_fromItem != null)
112: return this ;
113:
114: _fromItem = _parent.bindSubPath(parser);
115:
116: Table sourceTable = _linkColumns.getSourceTable();
117: _childFromItem = parser.addFromItem(sourceTable, id);
118:
119: JoinExpr joinExpr;
120: joinExpr = new OneToManyJoinExpr(_linkColumns, _childFromItem,
121: _fromItem);
122:
123: _childFromItem.setJoinExpr(joinExpr);
124: _childFromItem.setCollectionExpr(this );
125:
126: return this ;
127: }
128:
129: /**
130: * Returns the child from item.
131: */
132: public FromItem getChildFromItem() {
133: return _childFromItem;
134: }
135:
136: /**
137: * Binds the expression as a subpath.
138: */
139: public FromItem bindSubPath(QueryParser parser) {
140: if (_childFromItem == null)
141: bindSelect(parser, null);
142:
143: return _childFromItem;
144: }
145:
146: /**
147: * Returns the table.
148: */
149: public Table getTable() {
150: // return _field.getTable();
151: return _fromItem.getTable();
152: }
153:
154: /**
155: * Generates the where expression.
156: */
157: public void generateWhere(CharBuffer cb) {
158: throw new IllegalStateException(getClass().getName());
159: }
160:
161: /**
162: * Generates the (update) where expression.
163: */
164: public void generateUpdateWhere(CharBuffer cb) {
165: generateWhere(cb);
166: }
167:
168: /**
169: * Generates the having expression.
170: */
171: public void generateHaving(CharBuffer cb) {
172: generateWhere(cb);
173: }
174:
175: /**
176: * Generates the where expression.
177: */
178: public void generateSelect(CharBuffer cb) {
179: String id = _childFromItem.getName();
180:
181: cb.append(_linkColumns.generateSelectSQL(id));
182: }
183:
184: public boolean equals(Object o) {
185: if (o == null || !getClass().equals(o.getClass()))
186: return false;
187:
188: OneToManyExpr oneToMany = (OneToManyExpr) o;
189:
190: return (_parent.equals(oneToMany._parent) && _linkColumns
191: .equals(oneToMany._linkColumns));
192: }
193:
194: public String toString() {
195: return "OneToManyExpr[" + _parent + "," + _linkColumns + "]";
196: }
197: }
|