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