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.QueryParseException;
033: import com.caucho.amber.query.QueryParser;
034: import com.caucho.util.CharBuffer;
035:
036: import java.util.ArrayList;
037:
038: /**
039: * A disjunction.
040: */
041: public class OrExpr extends AbstractAmberExpr {
042: private ArrayList<AmberExpr> _components = new ArrayList<AmberExpr>();
043:
044: /**
045: * Returns true for a boolean expression.
046: */
047: public boolean isBoolean() {
048: return true;
049: }
050:
051: /**
052: * Adds a new component.
053: */
054: public void add(AmberExpr expr) throws QueryParseException {
055: _components.add(expr.createBoolean());
056: }
057:
058: /**
059: * Binds the expression as a select item.
060: */
061: public AmberExpr bindSelect(QueryParser parser) {
062: for (int i = 0; i < _components.size(); i++) {
063: AmberExpr expr = _components.get(i);
064:
065: expr = expr.bindSelect(parser);
066:
067: _components.set(i, expr);
068: }
069:
070: return this ;
071: }
072:
073: /**
074: * Returns true if the expression uses the from item.
075: */
076: public boolean usesFrom(FromItem from, int type, boolean isNot) {
077: if (type == IS_INNER_JOIN) {
078: // returns true if the from item is used in every term of the disjunction
079: for (int i = 0; i < _components.size(); i++) {
080: AmberExpr expr = _components.get(i);
081:
082: if (!isNot && !expr.usesFrom(from, type, isNot))
083: return false;
084: else if (isNot && expr.usesFrom(from, type, isNot))
085: return true;
086: }
087:
088: return false;
089: } else {
090: for (int i = 0; i < _components.size(); i++) {
091: AmberExpr expr = _components.get(i);
092:
093: if (expr.usesFrom(from, type))
094: return true;
095: }
096:
097: return false;
098: }
099: }
100:
101: /**
102: * Returns true if the expression uses the from item.
103: */
104: public AmberExpr replaceJoin(JoinExpr join) {
105: for (int i = 0; i < _components.size(); i++) {
106: AmberExpr expr = _components.get(i);
107:
108: expr = expr.replaceJoin(join);
109:
110: _components.set(i, expr);
111: }
112:
113: return this ;
114: }
115:
116: /**
117: * Returns a single expression.
118: */
119: AmberExpr getSingle() {
120: if (_components.size() == 0)
121: return null;
122: else if (_components.size() == 1)
123: return _components.get(0);
124: else
125: return this ;
126: }
127:
128: /**
129: * Generates the where expression.
130: */
131: public void generateWhere(CharBuffer cb) {
132: generateInternalWhere(cb, true);
133: }
134:
135: /**
136: * Generates the (update) where expression.
137: */
138: public void generateUpdateWhere(CharBuffer cb) {
139: generateInternalWhere(cb, false);
140: }
141:
142: /**
143: * Generates the having expression.
144: */
145: public void generateHaving(CharBuffer cb) {
146: generateWhere(cb);
147: }
148:
149: /**
150: * Generates the join expression.
151: */
152: public void generateJoin(CharBuffer cb) {
153: cb.append('(');
154:
155: for (int i = 0; i < _components.size(); i++) {
156: if (i != 0)
157: cb.append(" OR ");
158:
159: AmberExpr expr = _components.get(i);
160:
161: expr.generateJoin(cb);
162: }
163:
164: cb.append(')');
165: }
166:
167: public String toString() {
168: if (_components.size() == 1) {
169: return _components.get(0).toString();
170: } else {
171: CharBuffer cb = new CharBuffer();
172:
173: cb.append('(');
174:
175: for (int i = 0; i < _components.size(); i++) {
176: if (i != 0)
177: cb.append(" OR ");
178:
179: AmberExpr expr = _components.get(i);
180:
181: cb.append(expr);
182: }
183:
184: cb.append(')');
185:
186: return cb.toString();
187: }
188: }
189:
190: /**
191: * Binds the argument type based on another expr.
192: */
193: public void setInternalArgType(AmberExpr other) {
194: for (int i = 0; i < _components.size(); i++)
195: _components.get(i).setInternalArgType(other);
196: }
197:
198: //
199: // private
200:
201: private void generateInternalWhere(CharBuffer cb, boolean select) {
202: cb.append('(');
203:
204: for (int i = 0; i < _components.size(); i++) {
205: if (i != 0)
206: cb.append(" OR ");
207:
208: AmberExpr expr = _components.get(i);
209:
210: if (select)
211: expr.generateWhere(cb);
212: else
213: expr.generateUpdateWhere(cb);
214: }
215:
216: cb.append(')');
217: }
218: }
|