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 SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.ejb.ql;
030:
031: import com.caucho.config.ConfigException;
032: import com.caucho.ejb.cfg21.EjbEntityBean;
033: import com.caucho.util.CharBuffer;
034:
035: /**
036: * Identifier expression for EJB-QL.
037: */
038: class CollectionIdExpr extends PathExpr {
039: // identifier name
040: private String _name;
041:
042: // the persistent bean this id is a member of
043: private CollectionExpr _path;
044:
045: private String _keyTable;
046: private String[] _keyColumns;
047:
048: private boolean _usesField;
049:
050: /**
051: * Creates a new identifier expression.
052: *
053: * @param query the owning query
054: * @param name the identifier
055: * @param bean the mapped bean
056: */
057: CollectionIdExpr(Query query, String name, CollectionExpr path)
058: throws ConfigException {
059: super (path.getItemBean());
060:
061: _query = query;
062: _name = name;
063: _path = path;
064:
065: path.setId(this );
066:
067: if (getBean() == null)
068: throw new NullPointerException("unknown bean for " + name
069: + " " + query);
070:
071: // setJavaType(getBean().getJavaType());
072:
073: /*
074: PersistentRelation relation = path.getRelation();
075:
076: if (relation.hasMiddleTable()) {
077: _keyTable = "caucho" + query.getUnique();
078: PrimaryKey key = relation.getTargetBean().getPrimaryKey();
079: _keyColumns = relation.getTargetSQLColumns();
080:
081: query.addFromItem(_keyTable, relation.getSQLTable());
082: _keyColumns = relation.addTargetLinks(query,
083: path.getBase().getTable(),
084: _keyTable);
085: }
086: else {
087: _usesField = true;
088: path.setUsesField();
089:
090: _keyTable = name;
091: PrimaryKey key = relation.getTargetBean().getPrimaryKey();
092:
093: query.addFromItem(_keyTable, relation.getSQLTable());
094: _keyColumns = relation.addTargetLinks(query, path.getBase().getTable(),
095: _keyTable);
096: }
097: */
098: }
099:
100: CollectionExpr getPath() {
101: return _path;
102: }
103:
104: void setUsesField() {
105: if (_usesField)
106: return;
107:
108: _usesField = true;
109:
110: /*
111: _query.addFromItem(_name, _bean.getSQLTable());
112: _keyColumns = _path.getRelation().addSourceLinks(_query, _keyTable, _name);
113: _keyTable = _name;
114: */
115: }
116:
117: String getKeyTable() {
118: return _keyTable;
119: }
120:
121: String[] getKeyFields() {
122: return _keyColumns;
123: }
124:
125: int getComponentCount() {
126: return _path.getComponentCount();
127: }
128:
129: String getTable() {
130: return _name;
131: }
132:
133: /**
134: * Returns the identifier name.
135: */
136: String getName() {
137: return _name;
138: }
139:
140: /**
141: * Returns the persistent bean this id is a member of
142: */
143: EjbEntityBean getBean() {
144: return _bean;
145: }
146:
147: EjbEntityBean getItemBean() {
148: return _bean;
149: }
150:
151: String getReturnEJB() {
152: return getItemBean().getEJBName();
153: }
154:
155: /**
156: * Prints the select SQL for this expression
157: *
158: * @param gen the java code generator
159: */
160: void printSelect(CharBuffer cb) throws ConfigException {
161: String[] names = getKeyFields();
162:
163: for (int i = 0; i < names.length; i++) {
164: if (i != 0)
165: cb.append(", ");
166:
167: cb.append(getKeyTable());
168: cb.append(".");
169: cb.append(names[i]);
170: }
171: }
172:
173: /**
174: * Prints the select SQL for this expression
175: *
176: * @param gen the java code generator
177: */
178: String getSelectTable(CharBuffer cb) throws ConfigException {
179: return getKeyTable();
180: }
181:
182: /**
183: * Prints the where SQL for this expression
184: *
185: * @param gen the java code generator
186: */
187: void printWhere(CharBuffer cb) throws ConfigException {
188: String[] names = getKeyFields();
189: if (names.length != 1)
190: throw new RuntimeException();
191:
192: cb.append(getKeyTable());
193: cb.append(".");
194: cb.append(names[0]);
195: }
196:
197: void printComponent(CharBuffer cb, int index)
198: throws ConfigException {
199: cb.append(getKeyTable());
200: cb.append(".");
201: cb.append(getKeyFields()[index]);
202: }
203:
204: /**
205: * Prints the where SQL for this expression
206: *
207: * @param gen the java code generator
208: */
209: void generateWhere(CharBuffer cb) {
210: if (_bean == null)
211: throw new IllegalStateException("no bean for " + getName());
212:
213: cb.append(getName());
214: }
215:
216: /**
217: * Prints the where SQL for this expression
218: *
219: * @param gen the java code generator
220: */
221: void generateComponent(CharBuffer cb, int i) {
222: if (_bean == null)
223: throw new IllegalStateException("no bean for " + getName());
224:
225: cb.append(getName());
226: cb.append('.');
227: cb.append(generateKeyField(getItemBean().getEntityType(), i));
228: }
229:
230: /**
231: * Prints the where SQL for this expression
232: *
233: * @param gen the java code generator
234: */
235: void generateAmber(CharBuffer cb) {
236: cb.append("IN(");
237: _path.generateSelect(cb);
238: cb.append(") ");
239: cb.append(getName());
240: }
241:
242: /**
243: * Returns true if the two expressions are equal
244: */
245: public boolean equals(Object bObj) {
246: if (!(bObj instanceof CollectionIdExpr))
247: return false;
248:
249: CollectionIdExpr b = (CollectionIdExpr) bObj;
250:
251: return _name.equals(b._name);
252: }
253:
254: /**
255: * Returns a hash code for the expression
256: */
257: public int hashCode() {
258: return _name.hashCode();
259: }
260:
261: /**
262: * Printable version of the object.
263: */
264: public String toString() {
265: return _name;
266: }
267: }
|