KickJava   Java API By Example, From Geeks To Geeks.

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


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 CountExpr extends FunExpr implements GroupExpr {
38   protected static final L10N L = new L10N(CountExpr.class);
39   private static final Logger JavaDoc log = Log.open(CountExpr.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("max 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   /**
67    * Returns the expected result type of the expression.
68    */

69   public Class JavaDoc getType()
70   {
71     return long.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 oldValue = context.getGroupDouble(_groupField);
97     
98     context.setGroupDouble(_groupField, oldValue + 1);
99   }
100
101   /**
102    * Returns true for a null value.
103    *
104    * @param rows the current tuple being evaluated
105    *
106    * @return true if null
107    */

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

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

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

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