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: import java.lang.reflect.Method;
036:
037: /**
038: * A 'member' expression
039: */
040: class MemberExpr extends Expr {
041: // value expression
042: private PathExpr _item;
043: // value expression
044: private CollectionExpr _collection;
045: private CollectionIdExpr _collectionId;
046: // true if this is a negative member
047: private boolean _isNot;
048:
049: private Query _parent;
050:
051: /**
052: * Creates a like expression.
053: *
054: * @param value the value expression
055: * @param isNot if true, this the like is negated
056: */
057: MemberExpr(boolean isNot, Expr item, Expr collection)
058: throws ConfigException {
059: _isNot = isNot;
060:
061: if (collection instanceof CollectionExpr)
062: _collection = (CollectionExpr) collection;
063: else if (collection instanceof CollectionIdExpr) {
064: _collectionId = (CollectionIdExpr) collection;
065: } else
066: throw error(L
067: .l(
068: "MEMBER OF needs a collection-valued field at `{0}'.",
069: collection));
070:
071: if (!(item instanceof PathExpr))
072: throw error(L.l(
073: "MEMBER OF needs a single-valued field at `{0}'.",
074: item));
075:
076: _item = (PathExpr) item;
077:
078: setJavaType(boolean.class);
079: }
080:
081: public String addRelation(EjbEntityBean bean, FieldExpr id)
082: throws ConfigException {
083: return null;
084: }
085:
086: public Method getMethod() {
087: //return _parent.getMethod();
088: throw new UnsupportedOperationException();
089: }
090:
091: public EjbEntityBean getPersistentBean() {
092: //return _parent.getPersistentBean();
093: throw new UnsupportedOperationException();
094: }
095:
096: public void addArg(Expr arg) {
097: //_parent.addArg(arg);
098: throw new UnsupportedOperationException();
099: }
100:
101: /**
102: * Prints the where SQL for this expression
103: *
104: * @param gen the java code generator
105: */
106: void generateWhere(CharBuffer cb) {
107: if (_isNot)
108: cb.append("NOT ");
109:
110: _item.generateWhere(cb);
111:
112: cb.append(" MEMBER OF (");
113:
114: if (_collection != null)
115: _collection.generateSelect(cb);
116: else
117: _collectionId.generateSelect(cb);
118:
119: cb.append(")");
120:
121: /*
122: if (_collection != null) {
123: if (_isNot)
124: cb.append("NOT ");
125:
126: String tableSQL = _collection.getRelation().getSQLTable();
127:
128: String id = "caucho" + "666";//gen.getUnique();
129:
130: cb.append("EXISTS(SELECT * FROM " + tableSQL + " " + id + " ");
131: cb.append("WHERE ");
132:
133: for (int i = 0; i < tableSrc.length; i++) {
134: if (i != 0)
135: cb.append(" AND ");
136:
137: cb.append(id + "." + tableSrc[i] + " = ");
138:
139: _collection.getBase().generateComponent(cb, i);
140: }
141:
142: cb.append(" AND ");
143:
144: for (int i = 0; i < tableDst.length; i++) {
145: if (i != 0)
146: cb.append(" AND ");
147:
148: cb.append(id + "." + tableDst[i] + " = ");
149:
150: _item.generateComponent(cb, i);
151: }
152:
153: cb.append(")");
154:
155: throw new UnsupportedOperationException();
156: }
157: else {
158: int len = _collectionId.getKeyFields().length;
159:
160: if (_isNot)
161: cb.append("NOT (");
162:
163: for (int i = 0; i < len; i++) {
164: if (i != 0)
165: cb.append(" AND ");
166:
167: _collectionId.generateComponent(cb, i);
168: cb.append(" = ");
169: _item.generateComponent(cb, i);
170: }
171:
172: if (_isNot)
173: cb.append(")");
174: }
175: */
176: }
177:
178: public String toString() {
179: String str = _item.toString();
180:
181: if (_isNot)
182: return str + " NOT MEMBER OF " + _collection;
183: else
184: return str + " MEMBER OF " + _collection;
185: }
186: }
|