KickJava   Java API By Example, From Geeks To Geeks.

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


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 MaxExpr extends FunExpr implements GroupExpr {
38   protected static final L10N L = new L10N(MaxExpr.class);
39   private static final Logger JavaDoc log = Log.open(MaxExpr.class);
40
41   private Expr _expr;
42   private int _dataField;
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     _dataField = query.getDataFields();
57
58     query.setDataFields(_dataField + 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 _expr.getType();
72   }
73
74   /**
75    * Evaluates aggregate functions during the group phase.
76    *
77    * @param context the current database tuple
78    */

79   public void evalGroup(QueryContext context)
80     throws SQLException JavaDoc
81   {
82     if (_expr.isNull(context))
83       return;
84     
85     double value = _expr.evalDouble(context);
86     double oldValue = context.getGroupDouble(_dataField);
87     
88     if (context.isGroupNull(_dataField))
89       context.setGroupDouble(_dataField, value);
90     else if (oldValue < value)
91       context.setGroupDouble(_dataField, value);
92   }
93
94   /**
95    * Returns true if the value is null.
96    *
97    * @param queryContext the query context
98    */

99   public boolean isNull(QueryContext context)
100     throws SQLException JavaDoc
101   {
102     return context.isGroupNull(_dataField);
103   }
104
105   /**
106    * Evaluates the expression as a double.
107    *
108    * @param rows the current tuple being evaluated
109    *
110    * @return the double value
111    */

112   public double evalDouble(QueryContext context)
113     throws SQLException JavaDoc
114   {
115     if (context.isGroupNull(_dataField))
116       return 0;
117     else
118       return context.getGroupDouble(_dataField);
119   }
120
121   /**
122    * Evaluates the expression as a long.
123    *
124    * @param rows the current tuple being evaluated
125    *
126    * @return the long value
127    */

128   public long evalLong(QueryContext context)
129     throws SQLException JavaDoc
130   {
131     return (long) evalDouble(context);
132   }
133
134   /**
135    * Evaluates the expression as a string.
136    *
137    * @param rows the current tuple being evaluated
138    *
139    * @return the string value
140    */

141   public String JavaDoc evalString(QueryContext context)
142     throws SQLException JavaDoc
143   {
144     if (context.isGroupNull(_dataField))
145       return null;
146     
147     double value = context.getGroupDouble(_dataField);;
148     
149     return String.valueOf((long) value);
150   }
151
152   public String JavaDoc toString()
153   {
154     return "max(" + _expr + ")";
155   }
156 }
157
Popular Tags