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.query;
030:
031: import com.caucho.amber.entity.AmberEntityHome;
032: import com.caucho.amber.expr.CollectionIdExpr;
033: import com.caucho.amber.expr.IdExpr;
034: import com.caucho.amber.expr.JoinExpr;
035: import com.caucho.amber.expr.PathExpr;
036: import com.caucho.amber.table.Table;
037: import com.caucho.amber.type.EntityType;
038:
039: public class FromItem {
040: private String _name;
041:
042: private EntityType _entityType;
043:
044: private Table _table;
045:
046: private AbstractQuery _query;
047:
048: private PathExpr _collectionExpr;
049:
050: private IdExpr _idExpr;
051:
052: private int _index;
053:
054: private JoinExpr _joinExpr;
055:
056: private boolean _isUsed;
057:
058: enum JoinSemantics {
059: UNKNOWN, INNER, OUTER
060: }
061:
062: private JoinSemantics _joinSemantics = JoinSemantics.UNKNOWN;
063:
064: FromItem(EntityType entityType, Table table, String name, int index) {
065: _entityType = entityType;
066: _table = table;
067: _name = name;
068: _index = index;
069: }
070:
071: /**
072: * Sets the id expr.
073: */
074: public void setIdExpr(IdExpr idExpr) {
075: _idExpr = idExpr;
076: }
077:
078: /**
079: * Gets the id expr.
080: */
081: public IdExpr getIdExpr() {
082: if (_idExpr != null) {
083: } else if (_collectionExpr != null) {
084: _idExpr = new CollectionIdExpr(this , _collectionExpr);
085: } else
086: _idExpr = new IdExpr(this );
087:
088: return _idExpr;
089: }
090:
091: /**
092: * Sets the collection expr.
093: */
094: public void setCollectionExpr(PathExpr collectionExpr) {
095: _collectionExpr = collectionExpr;
096: }
097:
098: /**
099: * Gets the id expr.
100: */
101: public PathExpr getCollectionExpr() {
102: return _collectionExpr;
103: }
104:
105: /**
106: * Returns the owning query.
107: */
108: public AbstractQuery getQuery() {
109: return _query;
110: }
111:
112: /**
113: * Sets the owning query.
114: */
115: public void setQuery(AbstractQuery query) {
116: _query = query;
117: }
118:
119: /**
120: * Returns the from item's name.
121: */
122: public String getName() {
123: return _name;
124: }
125:
126: /**
127: * Gets the entity class.
128: */
129: public EntityType getEntityType() {
130: if (_entityType != null) {
131: // jpa/0l12
132: return _entityType;
133: }
134:
135: return (EntityType) getTableType();
136: }
137:
138: /**
139: * Gets the table type
140: */
141: public EntityType getTableType() {
142: return (EntityType) _table.getType();
143: }
144:
145: /**
146: * Returns the table.
147: */
148: public Table getTable() {
149: return _table;
150: }
151:
152: /**
153: * Returns true if there is an entity type.
154: */
155: public boolean isEntityType() {
156: return _entityType != null;
157: }
158:
159: /**
160: * Sets the table.
161: */
162: public void setTable(Table table) {
163: _table = table;
164: }
165:
166: /**
167: * Sets the join expr.
168: */
169: public void setJoinExpr(JoinExpr joinExpr) {
170: _joinExpr = joinExpr;
171: }
172:
173: /**
174: * Returns true if the from is used.
175: */
176: public boolean isUsed() {
177: return _isUsed;
178: }
179:
180: /**
181: * Returns true if the from is used.
182: */
183: public void setUsed(boolean isUsed) {
184: _isUsed = isUsed;
185: }
186:
187: /**
188: * Returns true if the from has no outer join.
189: */
190: public boolean isInnerJoin() {
191: return _joinSemantics == JoinSemantics.INNER;
192: }
193:
194: /**
195: * Returns true if the from needs an outer join.
196: */
197: public boolean isOuterJoin() {
198: return _joinSemantics == JoinSemantics.OUTER;
199: }
200:
201: /**
202: * Sets the join semantics.
203: */
204: public void setJoinSemantics(JoinSemantics joinSemantics) {
205: _joinSemantics = joinSemantics;
206: }
207:
208: /**
209: * Sets the join semantics to OUTER (true) or
210: * INNER (false).
211: */
212: public void setOuterJoin(boolean isOuterJoin) {
213: _joinSemantics = isOuterJoin ? JoinSemantics.OUTER
214: : JoinSemantics.INNER;
215: }
216:
217: /**
218: * Gets the join expr.
219: */
220: public JoinExpr getJoinExpr() {
221: return _joinExpr;
222: }
223:
224: /**
225: * Returns the entity home.
226: */
227: public AmberEntityHome getEntityHome() {
228: return ((EntityType) getTableType()).getHome();
229: }
230:
231: /**
232: * Gets the index within the cartesian product for the item.
233: */
234: public int getIndex() {
235: return _index;
236: }
237:
238: public String toString() {
239: return "FromItem[" + _table.getName() + " AS " + getName()
240: + "]";
241: }
242: }
|