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: import com.caucho.util.L10N;
033:
034: import java.sql.SQLException;
035: import java.util.logging.Logger;
036:
037: public class SumExpr extends FunExpr implements GroupExpr {
038: protected static final L10N L = new L10N(SumExpr.class);
039: private static final Logger log = Log.open(SumExpr.class);
040:
041: private Expr _expr;
042: private int _groupField;
043:
044: protected void addArg(Expr expr) throws SQLException {
045: if (_expr != null)
046: throw new SQLException(L
047: .l("sum requires a single argument"));
048:
049: _expr = expr;
050: }
051:
052: protected Expr bind(Query query) throws SQLException {
053: _groupField = query.getDataFields();
054:
055: query.setDataFields(_groupField + 1);
056: query.setGroup(true);
057:
058: _expr = _expr.bind(query);
059:
060: return this ;
061: }
062:
063: public Class getType() {
064: if (_expr.isLong())
065: return long.class;
066: else
067: return double.class;
068: }
069:
070: /**
071: * Initializes aggregate functions during the group phase.
072: *
073: * @param context the current database tuple
074: */
075: public void initGroup(QueryContext context) throws SQLException {
076: context.setGroupDouble(_groupField, 0);
077: }
078:
079: /**
080: * Evaluates aggregate functions during the group phase.
081: *
082: * @param context the current database tuple
083: */
084: public void evalGroup(QueryContext context) throws SQLException {
085: if (_expr.isNull(context))
086: return;
087:
088: double value = _expr.evalDouble(context);
089: double oldValue = context.getGroupDouble(_groupField);
090:
091: context.setGroupDouble(_groupField, value + oldValue);
092: }
093:
094: /**
095: * Returns true for a null value.
096: *
097: * @param rows the current tuple being evaluated
098: *
099: * @return true if null
100: */
101: public boolean isNull(QueryContext context) throws SQLException {
102: return context.isGroupNull(_groupField);
103: }
104:
105: /**
106: * Evaluates the expression as a double.
107: *
108: * @param rows the current tuple being evaluated
109: *
110: * @return the double value
111: */
112: public double evalDouble(QueryContext context) throws SQLException {
113: return context.getGroupDouble(_groupField);
114: }
115:
116: /**
117: * Evaluates the expression as a long.
118: *
119: * @param rows the current tuple being evaluated
120: *
121: * @return the long value
122: */
123: public long evalLong(QueryContext context) throws SQLException {
124: return (long) evalDouble(context);
125: }
126:
127: /**
128: * Evaluates the expression as a string.
129: *
130: * @param rows the current tuple being evaluated
131: *
132: * @return the string value
133: */
134: public String evalString(QueryContext context) throws SQLException {
135: return String.valueOf(evalLong(context));
136: }
137: }
|