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 BinaryLongExpr extends Expr {
039: private static final Logger log = Log.open(BinaryLongExpr.class);
040:
041: private Expr _left;
042: private Expr _right;
043: private int _op;
044:
045: BinaryLongExpr(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 long.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 long.
071: *
072: * @param rows the current tuple being evaluated
073: *
074: * @return the long value
075: */
076: public long evalLong(QueryContext context) throws SQLException {
077: switch (_op) {
078: case '+':
079: return _left.evalLong(context) + _right.evalLong(context);
080:
081: case '-':
082: return _left.evalLong(context) - _right.evalLong(context);
083:
084: case '*':
085: return _left.evalLong(context) * _right.evalLong(context);
086:
087: case '/':
088: return _left.evalLong(context) / _right.evalLong(context);
089:
090: case '%':
091: return _left.evalLong(context) % _right.evalLong(context);
092:
093: default:
094: throw new IllegalStateException();
095: }
096: }
097:
098: /**
099: * Evaluates the expression as a double.
100: *
101: * @param rows the current tuple being evaluated
102: *
103: * @return the double value
104: */
105: public double evalDouble(QueryContext context) throws SQLException {
106: return evalLong(context);
107: }
108:
109: /**
110: * Evaluates the expression as a string.
111: *
112: * @param rows the current tuple being evaluated
113: *
114: * @return the string value
115: */
116: public String evalString(QueryContext context) throws SQLException {
117: return String.valueOf(evalLong(context));
118: }
119:
120: /**
121: * Evaluates aggregate functions during the group phase.
122: *
123: * @param state the current database tuple
124: */
125: public void evalGroup(QueryContext context) throws SQLException {
126: _left.evalGroup(context);
127: _right.evalGroup(context);
128: }
129:
130: public boolean equals(Object o) {
131: if (o == null || !BinaryLongExpr.class.equals(o.getClass()))
132: return false;
133:
134: BinaryLongExpr expr = (BinaryLongExpr) o;
135:
136: return (_op == expr._op && _left.equals(expr._left) && _right
137: .equals(expr._right));
138: }
139:
140: public String toString() {
141: switch (_op) {
142: case '+':
143: return "(" + _left + " + " + _right + ")";
144:
145: case '-':
146: return "(" + _left + " - " + _right + ")";
147:
148: case '*':
149: return "(" + _left + " * " + _right + ")";
150:
151: case '/':
152: return "(" + _left + " / " + _right + ")";
153:
154: case '%':
155: return "(" + _left + " % " + _right + ")";
156:
157: default:
158: throw new IllegalStateException();
159: }
160: }
161: }
|