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 DoubleCmpExpr extends Expr {
038: private static final Logger log = Log.open(DoubleCmpExpr.class);
039:
040: private Expr _left;
041: private Expr _right;
042: private int _op;
043:
044: DoubleCmpExpr(int op, Expr left, Expr right) {
045: _left = left;
046: _right = right;
047: _op = op;
048: }
049:
050: protected Expr bind(Query query) throws SQLException {
051: Expr newLeft = _left.bind(query);
052: Expr newRight = _right.bind(query);
053:
054: if (_left == newLeft && _right == newRight)
055: return this ;
056: else
057: return new CmpExpr(newLeft, newRight, _op);
058: }
059:
060: /**
061: * Returns the type of the expression.
062: */
063: public Class getType() {
064: return boolean.class;
065: }
066:
067: /**
068: * Returns the cost based on the given FromList.
069: */
070: public long subCost(ArrayList<FromItem> fromList) {
071: return _left.subCost(fromList) + _right.subCost(fromList);
072: }
073:
074: /**
075: * Evaluates the expression as a boolean.
076: */
077: public int evalBoolean(QueryContext context) throws SQLException {
078: if (_left.isNull(context) || _right.isNull(context))
079: return UNKNOWN;
080:
081: switch (_op) {
082: case Parser.LT:
083: if (_left.evalDouble(context) < _right.evalDouble(context))
084: return TRUE;
085: else
086: return FALSE;
087:
088: case Parser.LE:
089: if (_left.evalDouble(context) <= _right.evalDouble(context))
090: return TRUE;
091: else
092: return FALSE;
093:
094: case Parser.GT:
095: if (_left.evalDouble(context) > _right.evalDouble(context))
096: return TRUE;
097: else
098: return FALSE;
099:
100: case Parser.GE:
101: if (_left.evalDouble(context) >= _right.evalDouble(context))
102: return TRUE;
103: else
104: return FALSE;
105:
106: case Parser.NE:
107: if (_left.evalDouble(context) != _right.evalDouble(context))
108: return TRUE;
109: else
110: return FALSE;
111:
112: case Parser.EQ:
113: if (_left.evalDouble(context) == _right.evalDouble(context))
114: return TRUE;
115: else
116: return FALSE;
117:
118: default:
119: throw new SQLException("can't compare");
120: }
121: }
122:
123: public String evalString(QueryContext context) throws SQLException {
124: throw new SQLException("can't convert string to boolean");
125: }
126:
127: /**
128: * Evaluates aggregate functions during the group phase.
129: *
130: * @param state the current database tuple
131: */
132: public void evalGroup(QueryContext context) throws SQLException {
133: _left.evalGroup(context);
134: _right.evalGroup(context);
135: }
136:
137: public String toString() {
138: return "(" + _left + " = " + _right + ")";
139: }
140: }
|