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 AvgExpr 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: /**
053: * Returns the expected result type of the expression.
054: */
055: public Class getType() {
056: return double.class;
057: }
058:
059: protected Expr bind(Query query) throws SQLException {
060: _groupField = query.getDataFields();
061:
062: query.setDataFields(_groupField + 2);
063: query.setGroup(true);
064:
065: _expr = _expr.bind(query);
066:
067: return this ;
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: context.setGroupDouble(_groupField + 1, 0);
078: }
079:
080: /**
081: * Evaluates aggregate functions during the group phase.
082: *
083: * @param context the current database tuple
084: */
085: public void evalGroup(QueryContext context) throws SQLException {
086: if (_expr.isNull(context))
087: return;
088:
089: double count = context.getGroupDouble(_groupField);
090:
091: context.setGroupDouble(_groupField, count + 1);
092:
093: double value = _expr.evalDouble(context);
094: double sum = context.getGroupDouble(_groupField + 1);
095:
096: context.setGroupDouble(_groupField + 1, sum + value);
097: }
098:
099: /**
100: * Returns true for a null value.
101: *
102: * @param rows the current tuple being evaluated
103: *
104: * @return true if null
105: */
106: public boolean isNull(QueryContext context) throws SQLException {
107: return context.isGroupNull(_groupField);
108: }
109:
110: /**
111: * Evaluates the expression as a double.
112: *
113: * @param rows the current tuple being evaluated
114: *
115: * @return the double value
116: */
117: public double evalDouble(QueryContext context) throws SQLException {
118: double count = context.getGroupDouble(_groupField);
119:
120: if (count == 0)
121: return 0;
122: else
123: return context.getGroupDouble(_groupField + 1) / count;
124: }
125:
126: /**
127: * Evaluates the expression as a long.
128: *
129: * @param rows the current tuple being evaluated
130: *
131: * @return the long value
132: */
133: public long evalLong(QueryContext context) throws SQLException {
134: return (long) evalDouble(context);
135: }
136:
137: /**
138: * Evaluates the expression as a string.
139: *
140: * @param rows the current tuple being evaluated
141: *
142: * @return the string value
143: */
144: public String evalString(QueryContext context) throws SQLException {
145: return String.valueOf(evalDouble(context));
146: }
147: }
|