001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.db.sql;
030:
031: import com.caucho.log.Log;
032: import com.caucho.util.L10N;
033:
034: import java.sql.SQLException;
035: import java.util.logging.Logger;
036:
037: public class MaxExpr extends FunExpr implements GroupExpr {
038: protected static final L10N L = new L10N(MaxExpr.class);
039: private static final Logger log = Log.open(MaxExpr.class);
040:
041: private Expr _expr;
042: private int _dataField;
043:
044: protected void addArg(Expr expr) throws SQLException {
045: if (_expr != null)
046: throw new SQLException(L
047: .l("max requires a single argument"));
048:
049: _expr = expr;
050: }
051:
052: protected Expr bind(Query query) throws SQLException {
053: _dataField = query.getDataFields();
054:
055: query.setDataFields(_dataField + 1);
056: query.setGroup(true);
057:
058: _expr = _expr.bind(query);
059:
060: return this ;
061: }
062:
063: /**
064: * Returns the expected result type of the expression.
065: */
066: public Class getType() {
067: return _expr.getType();
068: }
069:
070: /**
071: * Evaluates aggregate functions during the group phase.
072: *
073: * @param context the current database tuple
074: */
075: public void evalGroup(QueryContext context) throws SQLException {
076: if (_expr.isNull(context))
077: return;
078:
079: double value = _expr.evalDouble(context);
080: double oldValue = context.getGroupDouble(_dataField);
081:
082: if (context.isGroupNull(_dataField))
083: context.setGroupDouble(_dataField, value);
084: else if (oldValue < value)
085: context.setGroupDouble(_dataField, value);
086: }
087:
088: /**
089: * Returns true if the value is null.
090: *
091: * @param queryContext the query context
092: */
093: public boolean isNull(QueryContext context) throws SQLException {
094: return context.isGroupNull(_dataField);
095: }
096:
097: /**
098: * Evaluates the expression as a double.
099: *
100: * @param rows the current tuple being evaluated
101: *
102: * @return the double value
103: */
104: public double evalDouble(QueryContext context) throws SQLException {
105: if (context.isGroupNull(_dataField))
106: return 0;
107: else
108: return context.getGroupDouble(_dataField);
109: }
110:
111: /**
112: * Evaluates the expression as a long.
113: *
114: * @param rows the current tuple being evaluated
115: *
116: * @return the long value
117: */
118: public long evalLong(QueryContext context) throws SQLException {
119: return (long) evalDouble(context);
120: }
121:
122: /**
123: * Evaluates the expression as a string.
124: *
125: * @param rows the current tuple being evaluated
126: *
127: * @return the string value
128: */
129: public String evalString(QueryContext context) throws SQLException {
130: if (context.isGroupNull(_dataField))
131: return null;
132:
133: double value = context.getGroupDouble(_dataField);
134: ;
135:
136: return String.valueOf((long) value);
137: }
138:
139: public String toString() {
140: return "max(" + _expr + ")";
141: }
142: }
|