KickJava   Java API By Example, From Geeks To Geeks.

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


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 MinExpr extends FunExpr implements GroupExpr {
38   protected static final L10N L = new L10N(MinExpr.class);
39   private static final Logger JavaDoc log = Log.open(MinExpr.class);
40   
41   private Expr _expr;
42   private int _groupField;
43   private Class JavaDoc _type;
44
45   protected void addArg(Expr expr)
46     throws SQLException JavaDoc
47   {
48     if (_expr != null)
49       throw new SQLException JavaDoc(L.l("min requires a single argument"));
50
51     _expr = expr;
52   }
53
54   protected Expr bind(Query query)
55     throws SQLException JavaDoc
56   {
57     _groupField = query.getDataFields();
58
59     query.setDataFields(_groupField + 1);
60     query.setGroup(true);
61
62     _expr = _expr.bind(query);
63
64     _type = _expr.getType();
65     
66     return this;
67   }
68
69   /**
70    * Returns the expected result type of the expression.
71    */

72   public Class JavaDoc getType()
73   {
74     return _type;
75   }
76
77   /**
78    * Initializes aggregate functions during the group phase.
79    *
80    * @param context the current database tuple
81    */

82   public void initGroup(QueryContext context)
83     throws SQLException JavaDoc
84   {
85   }
86
87   /**
88    * Evaluates aggregate functions during the group phase.
89    *
90    * @param context the current database tuple
91    */

92   public void evalGroup(QueryContext context)
93     throws SQLException JavaDoc
94   {
95     if (_expr.isNull(context))
96       return;
97
98     if (_expr.isLong()) {
99       long value = _expr.evalLong(context);
100       long oldValue = context.getGroupLong(_groupField);
101     
102       if (context.isGroupNull(_groupField))
103     context.setGroupLong(_groupField, value);
104       else if (value < oldValue)
105     context.setGroupLong(_groupField, value);
106     }
107     else {
108       double value = _expr.evalDouble(context);
109       double oldValue = context.getGroupDouble(_groupField);
110     
111       if (context.isGroupNull(_groupField))
112     context.setGroupDouble(_groupField, value);
113       else if (value < oldValue)
114     context.setGroupDouble(_groupField, value);
115     }
116   }
117
118   /**
119    * Returns true if the value is null.
120    *
121    * @param queryContext the query context
122    */

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

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

149   public long evalLong(QueryContext context)
150     throws SQLException JavaDoc
151   {
152     return context.getGroupLong(_groupField);
153   }
154
155   /**
156    * Evaluates the expression as a string.
157    *
158    * @param rows the current tuple being evaluated
159    *
160    * @return the string value
161    */

162   public String JavaDoc evalString(QueryContext context)
163     throws SQLException JavaDoc
164   {
165     return context.getGroupString(_groupField);
166   }
167 }
168
Popular Tags