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.CmrRelation;
034: import com.caucho.ejb.cfg21.EjbEntityBean;
035: import com.caucho.util.CharBuffer;
036:
037: /**
038: * Expression representing a single-valued relation path.
039: */
040: class RelationExpr extends PathExpr {
041: // base expression
042: private PathExpr _base;
043: // field name
044: private String _fieldName;
045:
046: // relation information
047: private CmrRelation _relation;
048:
049: private String _keyTable;
050: private String[] _keyColumns;
051:
052: private boolean _usesField;
053:
054: /**
055: * Creates a new field expression.
056: *
057: * @param base the base expression
058: * @param field name of the field
059: */
060: RelationExpr(Query query, PathExpr base, String field,
061: CmrRelation relation) throws ConfigException {
062: super (relation.getTargetBean());
063:
064: _query = query;
065: _base = base;
066: _fieldName = field;
067: _relation = relation;
068:
069: // base.setUsesField();
070:
071: /*
072: setJavaType(relation.getJavaType());
073:
074: _keyTable = base.getKeyTable();
075: _keyColumns = relation.getTargetSQLColumns();
076:
077: if (relation.isImplicit())
078: setUsesField();
079: */
080: }
081:
082: /**
083: * Gets the Java Type of the expression.
084: */
085: public Class getJavaType() {
086: return _relation.getTargetType().getJavaClass();
087: }
088:
089: void setUsesField() {
090: if (_usesField)
091: return;
092:
093: _usesField = true;
094:
095: /*
096: _keyTable = "caucho" + _query.getUnique();
097:
098: _query.addFromItem(_keyTable, _bean.getSQLTable());
099: _keyColumns = _relation.addCommonTargetLinks(_query,
100: _base.getTable(),
101: _keyTable);
102: */
103: //throw new UnsupportedOperationException();
104: }
105:
106: String getKeyTable() {
107: return _keyTable;
108: }
109:
110: String[] getKeyFields() {
111: return _keyColumns;
112: }
113:
114: String getTable() {
115: return _keyTable;
116: }
117:
118: /**
119: * Returns the identifier name.
120: */
121: String getName() {
122: return _keyTable;
123: }
124:
125: /**
126: * Returns the persistent bean this id is a member of
127: */
128: EjbEntityBean getBean() {
129: return _bean;
130: }
131:
132: EjbEntityBean getItemBean() {
133: return _bean;
134: }
135:
136: /**
137: * Returns the EJB name.
138: */
139: String getReturnEJB() {
140: return _bean.getEJBName();
141: }
142:
143: int getComponentCount() {
144: return getItemBean().getEntityType().getId().getKeyCount();
145: }
146:
147: /**
148: * Prints the select SQL for this expression
149: *
150: * @param gen the java code generator
151: */
152: void generateSelect(CharBuffer cb) {
153: generateWhere(cb);
154: }
155:
156: /**
157: * Prints the select SQL for this expression
158: *
159: * @param gen the java code generator
160: */
161: String getSelectTable(CharBuffer cb) {
162: return getKeyTable();
163: }
164:
165: /**
166: * Prints the where SQL for this expression
167: *
168: * @param gen the java code generator
169: */
170: void generateWhere(CharBuffer cb) {
171: _base.generateWhere(cb);
172: cb.append(".");
173: cb.append(_fieldName);
174: }
175:
176: /*
177: void generateComponent(CharBuffer cb, int index)
178: {
179: // generateWhere(cb);
180: cb.append(getKeyTable());
181: cb.append(".");
182: cb.append(getKeyFields()[index]);
183: }
184: */
185:
186: void generateComponent(CharBuffer cb, int index) {
187: EjbEntityBean bean = getItemBean();
188:
189: EntityType type = bean.getEntityType();
190:
191: _base.generateWhere(cb);
192: cb.append(".");
193: cb.append(_fieldName);
194: cb.append(".");
195:
196: cb.append(keyComponent(type, index));
197: }
198:
199: /**
200: * Returns true if the two expressions are equal
201: */
202: public boolean equals(Object bObj) {
203: if (!(bObj instanceof RelationExpr))
204: return false;
205:
206: RelationExpr b = (RelationExpr) bObj;
207:
208: return _fieldName.equals(b._fieldName) && _base.equals(b._base);
209: }
210:
211: /**
212: * Returns a hash code for the expression
213: */
214: public int hashCode() {
215: return 65531 * _fieldName.hashCode() + _base.hashCode();
216: }
217:
218: /**
219: * Printable version of the object.
220: */
221: public String toString() {
222: return String.valueOf(_base) + '.' + _fieldName;
223: }
224: }
|