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 UnaryExpr extends Expr {
038: private static final Logger log = Log.open(UnaryExpr.class);
039:
040: private Expr _sub;
041: private int _op;
042:
043: UnaryExpr(Expr sub, int op) {
044: _sub = sub;
045: _op = op;
046: }
047:
048: /**
049: * Binds the expression to the actual tables.
050: */
051: protected Expr bind(Query query) throws SQLException {
052: Expr newSub = _sub.bind(query);
053:
054: switch (_op) {
055: case '-':
056: if (!newSub.isDouble())
057: throw new SQLException(
058: L
059: .l(
060: "unary minus requires a numeric expression at '{0}'",
061: newSub));
062: break;
063: }
064:
065: if (newSub == _sub)
066: return this ;
067: else
068: return new UnaryExpr(newSub, _op);
069: }
070:
071: /**
072: * Returns the type of the expression.
073: */
074: public Class getType() {
075: switch (_op) {
076: case '-':
077: if (_sub.isLong())
078: return long.class;
079: else
080: return double.class;
081:
082: default:
083: return Object.class;
084: }
085: }
086:
087: /**
088: * Returns the cost based on the given FromList.
089: */
090: public long subCost(ArrayList<FromItem> fromList) {
091: return _sub.subCost(fromList);
092: }
093:
094: /**
095: * Evaluates the expression for nulls
096: */
097: public boolean isNull(QueryContext context) throws SQLException {
098: return _sub.isNull(context);
099: }
100:
101: /**
102: * Evaluates the expression as a boolean
103: *
104: * @param rows the current tuple being evaluated
105: *
106: * @return the boolean value
107: */
108: public int evalBoolean(QueryContext context) throws SQLException {
109: switch (_op) {
110: default:
111: throw new IllegalStateException();
112: }
113: }
114:
115: /**
116: * Evaluates the expression as a long.
117: *
118: * @param rows the current tuple being evaluated
119: *
120: * @return the long value
121: */
122: public long evalLong(QueryContext context) throws SQLException {
123: switch (_op) {
124: case '-':
125: return -_sub.evalLong(context);
126:
127: default:
128: throw new IllegalStateException();
129: }
130: }
131:
132: /**
133: * Evaluates the expression as a double.
134: *
135: * @param rows the current tuple being evaluated
136: *
137: * @return the long value
138: */
139: public double evalDouble(QueryContext context) throws SQLException {
140: switch (_op) {
141: case '-':
142: return -_sub.evalDouble(context);
143:
144: default:
145: throw new IllegalStateException();
146: }
147: }
148:
149: /**
150: * Evaluates the expression as a string.
151: *
152: * @param rows the current tuple being evaluated
153: *
154: * @return the string value
155: */
156: public String evalString(QueryContext context) throws SQLException {
157: switch (_op) {
158: case '-':
159: if (isLong())
160: return String.valueOf(evalLong(context));
161: else
162: return String.valueOf(evalDouble(context));
163:
164: default:
165: throw new IllegalStateException();
166: }
167: }
168:
169: /**
170: * Evaluates aggregate functions during the group phase.
171: *
172: * @param state the current database tuple
173: */
174: public void evalGroup(QueryContext context) throws SQLException {
175: _sub.evalGroup(context);
176: }
177:
178: public String toString() {
179: switch (_op) {
180: case '-':
181: return "- " + _sub;
182:
183: default:
184: throw new IllegalStateException("can't compare:" + _op);
185: }
186: }
187: }
|