KickJava   Java API By Example, From Geeks To Geeks.

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


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 SumExpr 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   protected Expr bind(Query query)
54     throws SQLException JavaDoc
55   {
56     _groupField = query.getDataFields();
57
58     query.setDataFields(_groupField + 1);
59     query.setGroup(true);
60
61     _expr = _expr.bind(query);
62     
63     return this;
64   }
65
66   public Class JavaDoc getType()
67   {
68     if (_expr.isLong())
69       return long.class;
70     else
71       return double.class;
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   }
84
85   /**
86    * Evaluates aggregate functions during the group phase.
87    *
88    * @param context the current database tuple
89    */

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

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

122   public double evalDouble(QueryContext context)
123     throws SQLException JavaDoc
124   {
125     return context.getGroupDouble(_groupField);
126   }
127
128   /**
129    * Evaluates the expression as a long.
130    *
131    * @param rows the current tuple being evaluated
132    *
133    * @return the long value
134    */

135   public long evalLong(QueryContext context)
136     throws SQLException JavaDoc
137   {
138     return (long) evalDouble(context);
139   }
140
141   /**
142    * Evaluates the expression as a string.
143    *
144    * @param rows the current tuple being evaluated
145    *
146    * @return the string value
147    */

148   public String JavaDoc evalString(QueryContext context)
149     throws SQLException JavaDoc
150   {
151     return String.valueOf(evalLong(context));
152   }
153 }
154
Popular Tags