KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > sql > AvgExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.db.sql;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.L10N;
33
34 import java.sql.SQLException JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36
37 public class AvgExpr extends FunExpr implements GroupExpr {
38   protected static final L10N L = new L10N(SumExpr.class);
39   private static final Logger JavaDoc log = Log.open(SumExpr.class);
40   
41   private Expr _expr;
42   private int _groupField;
43
44   protected void addArg(Expr expr)
45     throws SQLException JavaDoc
46   {
47     if (_expr != null)
48       throw new SQLException JavaDoc(L.l("sum requires a single argument"));
49
50     _expr = expr;
51   }
52
53   /**
54    * Returns the expected result type of the expression.
55    */

56   public Class JavaDoc getType()
57   {
58     return double.class;
59   }
60
61   protected Expr bind(Query query)
62     throws SQLException JavaDoc
63   {
64     _groupField = query.getDataFields();
65
66     query.setDataFields(_groupField + 2);
67     query.setGroup(true);
68
69     _expr = _expr.bind(query);
70     
71     return this;
72   }
73
74   /**
75    * Initializes aggregate functions during the group phase.
76    *
77    * @param context the current database tuple
78    */

79   public void initGroup(QueryContext context)
80     throws SQLException JavaDoc
81   {
82     context.setGroupDouble(_groupField, 0);
83     context.setGroupDouble(_groupField + 1, 0);
84   }
85
86   /**
87    * Evaluates aggregate functions during the group phase.
88    *
89    * @param context the current database tuple
90    */

91   public void evalGroup(QueryContext context)
92     throws SQLException JavaDoc
93   {
94     if (_expr.isNull(context))
95       return;
96
97     double count = context.getGroupDouble(_groupField);
98     
99     context.setGroupDouble(_groupField, count + 1);
100     
101     double value = _expr.evalDouble(context);
102     double sum = context.getGroupDouble(_groupField + 1);
103
104     context.setGroupDouble(_groupField + 1, sum + value);
105   }
106
107   /**
108    * Returns true for a null value.
109    *
110    * @param rows the current tuple being evaluated
111    *
112    * @return true if null
113    */

114   public boolean isNull(QueryContext context)
115     throws SQLException JavaDoc
116   {
117     return context.isGroupNull(_groupField);
118   }
119
120   /**
121    * Evaluates the expression as a double.
122    *
123    * @param rows the current tuple being evaluated
124    *
125    * @return the double value
126    */

127   public double evalDouble(QueryContext context)
128     throws SQLException JavaDoc
129   {
130     double count = context.getGroupDouble(_groupField);
131
132     if (count == 0)
133       return 0;
134     else
135       return context.getGroupDouble(_groupField + 1) / count;
136   }
137
138   /**
139    * Evaluates the expression as a long.
140    *
141    * @param rows the current tuple being evaluated
142    *
143    * @return the long value
144    */

145   public long evalLong(QueryContext context)
146     throws SQLException JavaDoc
147   {
148     return (long) evalDouble(context);
149   }
150
151   /**
152    * Evaluates the expression as a string.
153    *
154    * @param rows the current tuple being evaluated
155    *
156    * @return the string value
157    */

158   public String JavaDoc evalString(QueryContext context)
159     throws SQLException JavaDoc
160   {
161     return String.valueOf(evalDouble(context));
162   }
163 }
164
Popular Tags