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 BetweenExpr extends Expr {
038: private static final Logger log = Log.open(BetweenExpr.class);
039:
040: private Expr _expr;
041: private Expr _min;
042: private Expr _max;
043: private boolean _isNot;
044:
045: private boolean _isLong;
046:
047: BetweenExpr(Expr expr, Expr min, Expr max, boolean isNot) {
048: _expr = expr;
049: _min = min;
050: _max = max;
051: _isNot = isNot;
052: }
053:
054: protected Expr bind(Query query) throws SQLException {
055: _expr = _expr.bind(query);
056: _min = _min.bind(query);
057: _max = _max.bind(query);
058:
059: _isLong = _expr.isLong();
060:
061: return this ;
062: }
063:
064: /**
065: * Returns the type of the expression.
066: */
067: public Class getType() {
068: return boolean.class;
069: }
070:
071: /**
072: * Returns the cost based on the given FromList.
073: */
074: public long subCost(ArrayList<FromItem> fromList) {
075: return (_expr.subCost(fromList) + _min.subCost(fromList) + _max
076: .subCost(fromList));
077: }
078:
079: /**
080: * Evaluates the expression as a boolean.
081: */
082: public int evalBoolean(QueryContext context) throws SQLException {
083: if (_expr.isNull(context))
084: return UNKNOWN;
085:
086: if (_isLong) {
087: long min = _min.evalLong(context);
088: long max = _max.evalLong(context);
089:
090: long value = _expr.evalLong(context);
091:
092: if (_isNot)
093: return !(min <= value && value <= max) ? TRUE : FALSE;
094: else
095: return min <= value && value <= max ? TRUE : FALSE;
096: } else {
097: double min = _min.evalDouble(context);
098: double max = _max.evalDouble(context);
099:
100: double value = _expr.evalDouble(context);
101:
102: if (_isNot)
103: return !(min <= value && value <= max) ? TRUE : FALSE;
104: else
105: return min <= value && value <= max ? TRUE : FALSE;
106: }
107: }
108:
109: public String evalString(QueryContext context) throws SQLException {
110: throw new SQLException("can't convert string to boolean");
111: }
112:
113: /**
114: * Evaluates aggregate functions during the group phase.
115: *
116: * @param state the current database tuple
117: */
118: public void evalGroup(QueryContext context) throws SQLException {
119: _expr.evalGroup(context);
120: _min.evalGroup(context);
121: _max.evalGroup(context);
122: }
123:
124: public String toString() {
125: return "(" + _expr + " BETWEEN " + _min + " AND " + _max + ")";
126: }
127: }
|