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 OrExpr extends Expr {
038: private static final Logger log = Log.open(OrExpr.class);
039:
040: private Expr _left;
041: private Expr _right;
042:
043: OrExpr(Expr left, Expr right) {
044: _left = left;
045: _right = right;
046: }
047:
048: protected Expr bind(Query query) throws SQLException {
049: Expr newLeft = _left.bind(query);
050: Expr newRight = _right.bind(query);
051:
052: if (!newLeft.isBoolean())
053: throw new SQLException(L.l("OR requires boolean operands"));
054: if (!newRight.isBoolean())
055: throw new SQLException(L.l("OR requires boolean operands"));
056:
057: if (_left == newLeft && _right == newRight)
058: return this ;
059: else
060: return new OrExpr(newLeft, newRight);
061: }
062:
063: /**
064: * Returns the type of the expression.
065: */
066: public Class getType() {
067: return boolean.class;
068: }
069:
070: /**
071: * Returns the cost based on the given FromList.
072: */
073: public long subCost(ArrayList<FromItem> fromList) {
074: return Integer.MAX_VALUE;
075: }
076:
077: /**
078: * Returns true if the expressoin evaluates to null
079: */
080: public boolean isNull(QueryContext context) throws SQLException {
081: return evalBoolean(context) == UNKNOWN;
082: }
083:
084: /**
085: * Evaluates the expression as a boolean.
086: */
087: public int evalBoolean(QueryContext context) throws SQLException {
088: int leftValue = _left.evalBoolean(context);
089:
090: if (leftValue == TRUE)
091: return TRUE;
092:
093: int rightValue = _right.evalBoolean(context);
094:
095: if (rightValue == TRUE)
096: return TRUE;
097:
098: if (leftValue == FALSE && rightValue == FALSE)
099: return FALSE;
100: else
101: return UNKNOWN;
102: }
103:
104: public String evalString(QueryContext context) throws SQLException {
105: switch (evalBoolean(context)) {
106: case TRUE:
107: return "1";
108: case FALSE:
109: return "0";
110: default:
111: return null;
112: }
113: }
114:
115: public String toString() {
116: return "(" + _left + " OR " + _right + ")";
117: }
118: }
|