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.amber.type.EntityType;
032: import com.caucho.config.ConfigException;
033: import com.caucho.ejb.cfg21.EjbEntityBean;
034: import com.caucho.util.CharBuffer;
035:
036: /**
037: * Identifier expression for EJB-QL.
038: */
039: class IdExpr extends PathExpr {
040: // identifier name
041: private String _name;
042:
043: // the table name
044: private String _tableName;
045:
046: /**
047: * Creates a new identifier expression.
048: *
049: * @param query the owning query
050: * @param name the identifier
051: * @param bean the mapped bean
052: */
053: IdExpr(Query query, String name, EjbEntityBean bean)
054: throws ConfigException {
055: super (bean);
056:
057: _query = query;
058: _name = name;
059:
060: setJavaType(bean.getEJBClass());
061: }
062:
063: /**
064: * Returns the identifier name.
065: */
066: String getName() {
067: return _name;
068: }
069:
070: String getKeyTable() {
071: return getName();
072: }
073:
074: String[] getKeyFields() {
075: /*
076: PrimaryKey key = _bean.getPrimaryKey();
077:
078: return key.getSQLColumns();
079: */
080: throw new UnsupportedOperationException();
081: }
082:
083: /**
084: * Returns the EJB name.
085: */
086: String getReturnEJB() {
087: return getBean().getEJBName();
088: }
089:
090: String getTable() {
091: return _name;
092: }
093:
094: /**
095: * Returns the persistent bean this id is a member of
096: */
097: EjbEntityBean getBean() {
098: return _bean;
099: }
100:
101: /**
102: * Sets the persistent bean this id is a member of
103: */
104: void setBean(EjbEntityBean bean) {
105: _bean = bean;
106: }
107:
108: /**
109: * Returns the SQL table name for the id
110: */
111: String getTableName() {
112: if (_tableName != null)
113: return _tableName;
114: else if (_bean != null)
115: return _bean.getSQLTable();
116: else
117: return null;
118: }
119:
120: /**
121: * Sets the SQL table name for the id
122: */
123: void setTableName(String tableName) {
124: _tableName = tableName;
125: }
126:
127: /**
128: * Returns the item bean of a collection.
129: */
130: EjbEntityBean getItemBean() {
131: return _bean;
132: }
133:
134: int getComponentCount() {
135: EjbEntityBean bean = getItemBean();
136:
137: EntityType type = bean.getEntityType();
138:
139: return type.getId().getKeyCount();
140: }
141:
142: /**
143: * Prints the select SQL for this expression
144: *
145: * @param gen the java code generator
146: */
147: void printSelect(CharBuffer cb) throws ConfigException {
148: if (_bean == null)
149: throw new ConfigException("no bean for " + getName());
150:
151: /*
152: PrimaryKey key = _bean.getPrimaryKey();
153:
154: String []names = key.getSQLColumns();
155: for (int i = 0; i < names.length; i++) {
156: if (i != 0)
157: cb.append(", ");
158:
159: if (_query.getFromList().size() == 1) {
160: // special case to handle strange databases
161: cb.append(names[i]);
162: }
163: else {
164: cb.append(getName());
165: cb.append(".");
166: cb.append(names[i]);
167: }
168: }
169: */
170: cb.append(getName());
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: /*
180: if (_query.getFromList().size() == 1) {
181: // special case to handle strange databases
182: return null;
183: }
184: else
185: return getName();
186: */
187: return getName();
188: }
189:
190: /**
191: * Prints the where SQL for this expression
192: *
193: * @param gen the java code generator
194: */
195: void generateWhere(CharBuffer cb) {
196: if (_bean == null)
197: throw new IllegalStateException("no bean for " + getName());
198:
199: cb.append(getName());
200:
201: /*
202: String []names = _bean.getPrimaryKey().getSQLColumns();
203: if (names.length != 1)
204: throw new RuntimeException("multiple values need special test.");
205:
206: if (_query.getFromList().size() == 1) {
207: // special case to handle strange databases
208: cb.append(names[0]);
209: }
210: else {
211: cb.append(getName());
212: cb.append(".");
213: cb.append(names[0]);
214: }
215: */
216: }
217:
218: void generateComponent(CharBuffer cb, int index) {
219: EjbEntityBean bean = getItemBean();
220:
221: EntityType type = bean.getEntityType();
222:
223: cb.append(getName());
224: cb.append(".");
225:
226: cb.append(keyComponent(type, index));
227: }
228:
229: /**
230: * Prints the where SQL for this expression
231: *
232: * @param gen the java code generator
233: */
234: void generateAmber(CharBuffer cb) {
235: cb.append(getBean().getAbstractSchemaName());
236: cb.append(' ');
237: cb.append(getName());
238: }
239:
240: /**
241: * Returns true if the two expressions are equal
242: */
243: public boolean equals(Object bObj) {
244: if (!(bObj instanceof IdExpr))
245: return false;
246:
247: IdExpr b = (IdExpr) bObj;
248:
249: return _name.equals(b._name);
250: }
251:
252: /**
253: * Returns a hash code for the expression
254: */
255: public int hashCode() {
256: return _name.hashCode();
257: }
258:
259: /**
260: * Printable version of the object.
261: */
262: public String toString() {
263: return _name;
264: }
265: }
|