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.db.sql;
030:
031: import com.caucho.log.Log;
032: import com.caucho.util.CharBuffer;
033:
034: import java.sql.SQLException;
035: import java.util.ArrayList;
036: import java.util.logging.Logger;
037:
038: class AndExpr extends Expr {
039: private static final Logger log = Log.open(AndExpr.class);
040:
041: private ArrayList<Expr> _exprs = new ArrayList<Expr>();
042:
043: AndExpr() {
044: }
045:
046: void add(Expr expr) {
047: _exprs.add(expr);
048: }
049:
050: Expr getSingleExpr() {
051: if (_exprs.size() == 0)
052: return null;
053: else if (_exprs.size() == 1)
054: return _exprs.get(0);
055: else
056: return this ;
057: }
058:
059: protected Expr bind(Query query) throws SQLException {
060: for (int i = 0; i < _exprs.size(); i++) {
061: Expr expr = _exprs.get(i);
062:
063: expr = expr.bind(query);
064:
065: if (!expr.getType().equals(boolean.class))
066: throw new SQLException(L.l(
067: "AND requires boolean operands at {0}", expr));
068:
069: _exprs.set(i, expr);
070: }
071:
072: return this ;
073: }
074:
075: /**
076: * Returns the type of the expression.
077: */
078: public Class getType() {
079: return boolean.class;
080: }
081:
082: /**
083: * Returns the cost based on the given FromList.
084: */
085: public long subCost(ArrayList<FromItem> fromList) {
086: long cost = 0;
087:
088: for (int i = 0; i < _exprs.size(); i++) {
089: cost += _exprs.get(i).subCost(fromList);
090: }
091:
092: return cost;
093: }
094:
095: /**
096: * Splits the expr into and blocks.
097: */
098: public void splitAnd(ArrayList<Expr> andProduct) {
099: for (int i = 0; i < _exprs.size(); i++) {
100: _exprs.get(i).splitAnd(andProduct);
101: }
102: }
103:
104: /**
105: * Returns true for a null expression
106: */
107: public boolean isNull(QueryContext context) throws SQLException {
108: boolean isNull = false;
109: for (int i = 0; i < _exprs.size(); i++) {
110: int value = _exprs.get(i).evalBoolean(context);
111:
112: if (value == FALSE)
113: return false;
114: else if (value != TRUE)
115: isNull = true;
116: }
117:
118: return isNull;
119: }
120:
121: /**
122: * Evaluates the expression as a boolean.
123: */
124: public int evalBoolean(QueryContext context) throws SQLException {
125: int value = TRUE;
126:
127: for (int i = 0; i < _exprs.size(); i++) {
128: int subValue = _exprs.get(i).evalBoolean(context);
129:
130: if (subValue == FALSE)
131: return FALSE;
132: else if (subValue == UNKNOWN)
133: value = UNKNOWN;
134: }
135:
136: return value;
137: }
138:
139: public String evalString(QueryContext context) throws SQLException {
140: switch (evalBoolean(context)) {
141: case TRUE:
142: return "1";
143: case FALSE:
144: return "0";
145: default:
146: return null;
147: }
148: }
149:
150: public String toString() {
151: CharBuffer cb = CharBuffer.allocate();
152: cb.append("(");
153:
154: for (int i = 0; i < _exprs.size(); i++) {
155: if (i != 0)
156: cb.append(" AND ");
157:
158: cb.append(_exprs.get(i));
159: }
160:
161: cb.append(")");
162:
163: return cb.close();
164: }
165: }
|