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: * An 'is' expression
039: */
040: class EmptyExpr extends Expr {
041: // value expression
042: private CollectionExpr _collection;
043: // true for negative
044: private boolean _isNot;
045: //
046: private Query _parent;
047:
048: /**
049: * Creates a like expression.
050: *
051: * @param value the value expression
052: * @param isNot if true, this the like is negated
053: */
054: EmptyExpr(Expr collection, boolean isNot) throws ConfigException {
055: _isNot = isNot;
056:
057: if (collection instanceof CollectionExpr)
058: _collection = (CollectionExpr) collection;
059: else
060: throw error(L.l(
061: "IS EMPTY needs a collection-path at `{0}'.",
062: collection));
063:
064: evalTypes();
065: }
066:
067: /**
068: * Evaluates the types for the expression
069: */
070: void evalTypes() throws ConfigException {
071: setJavaType(boolean.class);
072: }
073:
074: public String addRelation(EjbEntityBean bean, FieldExpr id)
075: throws ConfigException {
076: return null;
077: }
078:
079: public Method getMethod() {
080: //return _parent.getMethod();
081: throw new UnsupportedOperationException();
082: }
083:
084: public EjbEntityBean getPersistentBean() {
085: //return _parent.getPersistentBean();
086: throw new UnsupportedOperationException();
087: }
088:
089: public void addArg(Expr arg) {
090: //_parent.addArg(arg);
091: throw new UnsupportedOperationException();
092: }
093:
094: /**
095: * Prints the where SQL for this expression
096: *
097: * @param gen the java code generator
098: */
099: void generateWhere(CharBuffer cb) {
100: if (_collection != null) {
101: _collection.generateSelect(cb);
102:
103: if (_isNot)
104: cb.append(" IS NOT EMPTY");
105: else
106: cb.append(" IS EMPTY");
107:
108: /*
109: String tableSQL = _collection.getRelation().getSQLTable();
110: String []tableSrc = _collection.getRelation().getSourceSQLColumns();
111: String []tableDst = _collection.getRelation().getTargetSQLColumns();
112:
113: String id = "caucho" + gen.getUnique();
114:
115: cb.append("EXISTS(SELECT * FROM " + tableSQL + " " + id + " ");
116: cb.append("WHERE ");
117:
118: for (int i = 0; i < tableSrc.length; i++) {
119: if (i != 0)
120: cb.append(" AND ");
121:
122: cb.append(id + "." + tableSrc[i] + " = ");
123:
124: _collection.getBase().printComponent(gen, i);
125: }
126:
127: cb.append(")");
128: */
129: } else {
130: cb.append("TRUE");
131: }
132: }
133:
134: public String toString() {
135: String str = _collection.toString();
136:
137: if (_isNot)
138: return str + " IS NOT EMPTY";
139: else
140: return str + " IS EMPTY";
141: }
142: }
|