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 Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.amber.expr;
030:
031: import com.caucho.amber.query.FromItem;
032: import com.caucho.amber.query.QueryParser;
033: import com.caucho.amber.type.BooleanType;
034: import com.caucho.amber.type.Type;
035: import com.caucho.util.CharBuffer;
036:
037: import java.util.ArrayList;
038:
039: /**
040: * "in" expression
041: */
042: public class InExpr extends AbstractAmberExpr {
043: private AmberExpr _expr;
044:
045: private ArrayList<AmberExpr> _values;
046: private boolean _isNot;
047:
048: /**
049: * Creates a new cmp expression
050: */
051: public InExpr(AmberExpr expr, ArrayList<AmberExpr> values,
052: boolean isNot) {
053: _expr = expr;
054: _values = values;
055: _isNot = isNot;
056: }
057:
058: /**
059: * Returns true for a boolean expression.
060: */
061: public boolean isBoolean() {
062: return true;
063: }
064:
065: /**
066: * Returns the expr type.
067: */
068: public Type getType() {
069: return BooleanType.create();
070: }
071:
072: /**
073: * Binds the expression as a select item.
074: */
075: public AmberExpr bindSelect(QueryParser parser) {
076: _expr = _expr.bindSelect(parser);
077:
078: for (int i = 0; i < _values.size(); i++) {
079: _values.set(i, _values.get(i).bindSelect(parser));
080: }
081:
082: return this ;
083: }
084:
085: /**
086: * Returns true if the expression uses the from item.
087: */
088: public boolean usesFrom(FromItem from, int type, boolean isNot) {
089: if (_expr.usesFrom(from, type))
090: return true;
091:
092: for (int i = 0; i < _values.size(); i++) {
093: if (_values.get(i).usesFrom(from, type))
094: return true;
095: }
096:
097: return false;
098: }
099:
100: /**
101: * Returns true if the expression uses the from item.
102: */
103: public AmberExpr replaceJoin(JoinExpr join) {
104: _expr = _expr.replaceJoin(join);
105:
106: for (int i = 0; i < _values.size(); i++) {
107: _values.set(i, _values.get(i).replaceJoin(join));
108: }
109:
110: return this ;
111: }
112:
113: /**
114: * Generates the where expression.
115: */
116: public void generateWhere(CharBuffer cb) {
117: generateInternalWhere(cb, true);
118: }
119:
120: /**
121: * Generates the (update) where expression.
122: */
123: public void generateUpdateWhere(CharBuffer cb) {
124: generateInternalWhere(cb, false);
125: }
126:
127: /**
128: * Generates the having expression.
129: */
130: public void generateHaving(CharBuffer cb) {
131: generateWhere(cb);
132: }
133:
134: public String toString() {
135: CharBuffer cb = new CharBuffer();
136:
137: cb.append('(');
138: cb.append(_expr);
139:
140: if (_isNot)
141: cb.append(" NOT");
142:
143: cb.append(" IN (");
144:
145: for (int i = 0; i < _values.size(); i++) {
146: if (i != 0)
147: cb.append(',');
148:
149: cb.append(_values.get(i));
150: }
151:
152: cb.append("))");
153:
154: return cb.toString();
155: }
156:
157: //
158: // private
159:
160: private void generateInternalWhere(CharBuffer cb, boolean select) {
161: cb.append('(');
162:
163: if (select)
164: _expr.generateWhere(cb);
165: else
166: _expr.generateUpdateWhere(cb);
167:
168: if (_isNot)
169: cb.append(" NOT");
170:
171: cb.append(" IN (");
172:
173: for (int i = 0; i < _values.size(); i++) {
174: if (i != 0)
175: cb.append(',');
176:
177: if (select)
178: _values.get(i).generateWhere(cb);
179: else
180: _values.get(i).generateUpdateWhere(cb);
181: }
182:
183: cb.append("))");
184: }
185: }
|