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.util.CharBuffer;
033:
034: import java.util.ArrayList;
035:
036: /**
037: * A binary expression
038: */
039: class AndExpr extends Expr {
040: // components to the and
041: private ArrayList<Expr> _components = new ArrayList<Expr>();
042:
043: /**
044: * Creates an and expression.
045: */
046: AndExpr(Query query) throws ConfigException {
047: setJavaType(boolean.class);
048: }
049:
050: /**
051: * Evaluates the types for the expression
052: */
053: void evalTypes() throws ConfigException {
054: setJavaType(boolean.class);
055: }
056:
057: /**
058: * Adds a subexpression.
059: */
060: void add(Expr expr) {
061: _components.add(expr);
062: }
063:
064: /**
065: * Return the expression if single.
066: */
067: Expr getSingleExpr() {
068: if (_components.size() == 1)
069: return _components.get(0);
070: else
071: return this ;
072: }
073:
074: /**
075: * Prints the where SQL for this expression
076: *
077: * @param gen the java code generator
078: */
079: void generateWhere(CharBuffer cb) {
080: if (_components.size() == 1) {
081: Expr comp = _components.get(0);
082:
083: comp.generateWhere(cb);
084: return;
085: }
086:
087: for (int i = 0; i < _components.size(); i++) {
088: Expr comp = _components.get(i);
089:
090: if (i != 0)
091: cb.append(" AND ");
092:
093: comp.generateWhereSubExpr(cb);
094: }
095: }
096:
097: /**
098: * Prints the where SQL for this expression
099: */
100: void generateWhereSubExpr(CharBuffer cb) {
101: cb.append("(");
102: generateWhere(cb);
103: cb.append(")");
104: }
105:
106: void printSelect(CharBuffer cb) throws ConfigException {
107: cb.append("(");
108: generateWhere(cb);
109: cb.append(")");
110: }
111:
112: public String toString() {
113: CharBuffer value = CharBuffer.allocate();
114:
115: value.append("(");
116:
117: for (int i = 0; i < _components.size(); i++) {
118: Expr comp = _components.get(i);
119:
120: if (i != 0)
121: value.append(" AND ");
122:
123: value.append(comp.toString());
124: }
125:
126: value.append(")");
127:
128: return value.close();
129: }
130: }
|