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 the a collection specified in the FROM field,
039: * e.g. FROM l in o.list
040: */
041: class CollectionExpr extends Expr {
042: // base expression
043: private PathExpr _base;
044: // field name
045: private String _fieldName;
046:
047: // relation information
048: private CmrRelation _relation;
049:
050: private CollectionIdExpr _id;
051: private boolean _usesField;
052:
053: /**
054: * Creates a new field expression.
055: *
056: * @param base the base expression
057: * @param field name of the field
058: */
059: CollectionExpr(Query query, PathExpr base, String field,
060: CmrRelation relation) throws ConfigException {
061: _query = query;
062: _base = base;
063: _fieldName = field;
064: _relation = relation;
065:
066: base.setUsesField();
067:
068: // setJavaType(relation.getJavaType());
069: }
070:
071: void setId(CollectionIdExpr id) {
072: if (_id != null)
073: throw new RuntimeException();
074:
075: _id = id;
076: }
077:
078: CollectionIdExpr getId() {
079: return _id;
080: }
081:
082: void setUsesField() {
083: if (!_usesField) {
084: _usesField = true;
085: }
086: }
087:
088: PathExpr getBase() {
089: return _base;
090: }
091:
092: EjbEntityBean getItemBean() {
093: return _relation.getTargetBean();
094: }
095:
096: int getComponentCount() {
097: EjbEntityBean bean = getItemBean();
098:
099: EntityType type = bean.getEntityType();
100:
101: return type.getId().getKeyCount();
102: }
103:
104: /**
105: * Returns the EJB name.
106: */
107: String getReturnEJB() {
108: return getItemBean().getEJBName();
109: }
110:
111: CmrRelation getRelation() {
112: return _relation;
113: }
114:
115: /**
116: * Prints the select SQL for this expression
117: *
118: * @param cb the java code generator
119: */
120: void generateSelect(CharBuffer cb) {
121: _base.generateSelect(cb);
122: cb.append('.');
123: cb.append(_fieldName);
124:
125: // _id.generateSelect(cb);
126: }
127:
128: public String toString() {
129: return String.valueOf(_base) + "." + _fieldName;
130: }
131: }
|