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 ThisExpr extends PathExpr {
039: // the table name
040: private String _tableName;
041:
042: /**
043: * Creates a new identifier expression.
044: *
045: * @param query the owning query
046: * @param bean the mapped bean
047: */
048: ThisExpr(Query query, EjbEntityBean bean) throws ConfigException {
049: super (bean);
050:
051: _query = query;
052:
053: // setJavaType(bean.getBeanClass());
054: }
055:
056: /**
057: * Returns the identifier name.
058: */
059: String getName() {
060: return "this";
061: }
062:
063: String getKeyTable() {
064: return getName();
065: }
066:
067: String[] getKeyFields() {
068: /*
069: PrimaryKey key = _bean.getPrimaryKey();
070:
071: return key.getSQLColumns();
072: */
073: throw new UnsupportedOperationException();
074: }
075:
076: String getTable() {
077: return "this";
078: }
079:
080: /**
081: * Returns the persistent bean this id is a member of
082: */
083: EjbEntityBean getBean() {
084: return _bean;
085: }
086:
087: /**
088: * Sets the persistent bean this id is a member of
089: */
090: void setBean(EjbEntityBean bean) {
091: _bean = bean;
092: }
093:
094: /**
095: * Returns the SQL table name for the id
096: */
097: String getTableName() {
098: if (_tableName != null)
099: return _tableName;
100: else if (_bean != null)
101: return _bean.getSQLTable();
102: else
103: return null;
104: }
105:
106: /**
107: * Sets the SQL table name for the id
108: */
109: void setTableName(String tableName) {
110: _tableName = tableName;
111: }
112:
113: /**
114: * Returns the item bean of a collection.
115: */
116: EjbEntityBean getItemBean() {
117: return _bean;
118: }
119:
120: int getComponentCount() {
121: //return _bean.getPrimaryKey().getSQLColumns().length;
122: throw new UnsupportedOperationException();
123: }
124:
125: /**
126: * Prints the select SQL for this expression
127: *
128: * @param gen the java code generator
129: */
130: void generateSelect(CharBuffer cb) {
131: if (_bean == null)
132: throw new IllegalStateException("no bean for " + getName());
133:
134: /*
135: PrimaryKey key = _bean.getPrimaryKey();
136:
137: String []names = key.getSQLColumns();
138: for (int i = 0; i < names.length; i++) {
139: if (i != 0)
140: gen.print(", ");
141:
142: if (_query.getFromList().size() == 1) {
143: // special case to handle strange databases
144: gen.print(names[i]);
145: }
146: else {
147: gen.print(getName());
148: gen.print(".");
149: gen.print(names[i]);
150: }
151: }
152: */
153: }
154:
155: /**
156: * Prints the select SQL for this expression
157: *
158: * @param gen the java code generator
159: */
160: String getSelectTable(CharBuffer cb) throws ConfigException {
161: /*
162: if (_query.getFromList().size() == 1) {
163: // special case to handle strange databases
164: return null;
165: }
166: else
167: return getName();
168: */
169: return getName();
170: }
171:
172: /**
173: * Prints the where SQL for this expression
174: *
175: * @param gen the java code generator
176: */
177: void generateWhere(CharBuffer cb) {
178: if (_bean == null)
179: throw new IllegalStateException("no bean for " + getName());
180:
181: /*
182: String []names = _bean.getPrimaryKey().getSQLColumns();
183: if (names.length != 1)
184: throw new RuntimeException("multiple values need special test.");
185:
186: if (_query.getFromList().size() == 1) {
187: // special case to handle strange databases
188: cb.append(names[0]);
189: }
190: else {
191: cb.append(getName());
192: cb.append(".");
193: cb.append(names[0]);
194: }
195: */
196: throw new UnsupportedOperationException();
197: }
198:
199: void generateComponent(CharBuffer cb, int index) {
200: if (_bean == null)
201: throw new IllegalStateException("no bean for " + getName());
202:
203: /*
204: String []names = _bean.getPrimaryKey().getSQLColumns();
205:
206: cb.append(getName());
207: cb.append(".");
208: cb.append(names[index]);
209: */
210: throw new UnsupportedOperationException();
211: }
212:
213: /**
214: * Returns true if the two expressions are equal
215: */
216: public boolean equals(Object bObj) {
217: return bObj instanceof ThisExpr;
218: }
219:
220: /**
221: * Returns a hash code for the expression
222: */
223: public int hashCode() {
224: return "this".hashCode();
225: }
226:
227: /**
228: * Printable version of the object.
229: */
230: public String toString() {
231: return "this";
232: }
233: }
|