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 Software Foundation, 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.logging.Logger;
037:
038: class BinaryDoubleExpr extends Expr {
039: private static final Logger log = Log.open(BinaryDoubleExpr.class);
040:
041: private Expr _left;
042: private Expr _right;
043: private int _op;
044:
045: BinaryDoubleExpr(Expr left, Expr right, int op) {
046: _left = left;
047: _right = right;
048: _op = op;
049: }
050:
051: protected Expr bind(FromItem[] fromItems) throws SQLException {
052: throw new UnsupportedOperationException();
053: }
054:
055: /**
056: * Returns the type of the expression.
057: */
058: public Class getType() {
059: return double.class;
060: }
061:
062: /**
063: * Returns the cost based on the given FromList.
064: */
065: public long subCost(ArrayList<FromItem> fromList) {
066: return _left.subCost(fromList) + _right.subCost(fromList);
067: }
068:
069: /**
070: * Evaluates the expression as a double.
071: *
072: * @param rows the current tuple being evaluated
073: *
074: * @return the double value
075: */
076: public double evalDouble(QueryContext context) throws SQLException {
077: switch (_op) {
078: case '+':
079: return _left.evalDouble(context)
080: + _right.evalDouble(context);
081:
082: case '-':
083: return _left.evalDouble(context)
084: - _right.evalDouble(context);
085:
086: case '*':
087: return _left.evalDouble(context)
088: * _right.evalDouble(context);
089:
090: case '/':
091: return _left.evalDouble(context)
092: / _right.evalDouble(context);
093:
094: case '%':
095: return _left.evalDouble(context)
096: % _right.evalDouble(context);
097:
098: default:
099: throw new IllegalStateException();
100: }
101: }
102:
103: /**
104: * Evaluates the expression as a double.
105: *
106: * @param rows the current tuple being evaluated
107: *
108: * @return the double value
109: */
110: public long evalLong(QueryContext context) throws SQLException {
111: return (long) evalDouble(context);
112: }
113:
114: /**
115: * Evaluates the expression as a string.
116: *
117: * @param rows the current tuple being evaluated
118: *
119: * @return the string value
120: */
121: public String evalString(QueryContext context) throws SQLException {
122: return String.valueOf(evalDouble(context));
123: }
124:
125: /**
126: * Evaluates aggregate functions during the group phase.
127: *
128: * @param state the current database tuple
129: */
130: public void evalGroup(QueryContext context) throws SQLException {
131: _left.evalGroup(context);
132: _right.evalGroup(context);
133: }
134:
135: public boolean equals(Object o) {
136: if (o == null || !BinaryDoubleExpr.class.equals(o.getClass()))
137: return false;
138:
139: BinaryDoubleExpr expr = (BinaryDoubleExpr) o;
140:
141: return (_op == expr._op && _left.equals(expr._left) && _right
142: .equals(expr._right));
143: }
144:
145: public String toString() {
146: switch (_op) {
147: case '+':
148: return "(" + _left + " + " + _right + ")";
149:
150: case '-':
151: return "(" + _left + " - " + _right + ")";
152:
153: case '*':
154: return "(" + _left + " * " + _right + ")";
155:
156: case '/':
157: return "(" + _left + " / " + _right + ")";
158:
159: case '%':
160: return "(" + _left + " % " + _right + ")";
161:
162: default:
163: throw new IllegalStateException();
164: }
165: }
166: }
|