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: *
023: * Free SoftwareFoundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.db.sql;
031:
032: import com.caucho.log.Log;
033:
034: import java.sql.SQLException;
035: import java.util.ArrayList;
036: import java.util.HashSet;
037: import java.util.logging.Logger;
038:
039: class InExpr extends Expr {
040: private static final Logger log = Log.open(InExpr.class);
041:
042: private Expr _expr;
043: private final HashSet<String> _values;
044: private final boolean _isNot;
045:
046: InExpr(Expr expr, HashSet<String> values, boolean isNot) {
047: _expr = expr;
048: _values = values;
049: _isNot = isNot;
050: }
051:
052: /**
053: * Binds the expression to the actual tables.
054: */
055: protected Expr bind(Query query) throws SQLException {
056: _expr = _expr.bind(query);
057:
058: return this ;
059: }
060:
061: /**
062: * Returns the type of the expression.
063: */
064: public Class getType() {
065: return boolean.class;
066: }
067:
068: /**
069: * Returns the cost based on the given FromList.
070: */
071: public long subCost(ArrayList<FromItem> fromList) {
072: return _expr.subCost(fromList);
073: }
074:
075: /**
076: * Evaluates the expression as a boolean
077: *
078: * @param rows the current tuple being evaluated
079: *
080: * @return the boolean value
081: */
082: public int evalBoolean(QueryContext context) throws SQLException {
083: if (_values.contains(_expr.evalString(context)))
084: return _isNot ? FALSE : TRUE;
085: else
086: return _isNot ? TRUE : FALSE;
087: }
088:
089: /**
090: * Evaluates the expression as a string.
091: *
092: * @param rows the current tuple being evaluated
093: *
094: * @return the string value
095: */
096: public String evalString(QueryContext context) throws SQLException {
097: return (evalBoolean(context) == TRUE) ? "1" : "0";
098: }
099:
100: /**
101: * Evaluates aggregate functions during the group phase.
102: *
103: * @param state the current database tuple
104: */
105: public void evalGroup(QueryContext context) throws SQLException {
106: _expr.evalGroup(context);
107: }
108:
109: public String toString() {
110: return _expr + " IN " + _values;
111: }
112: }
|