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 MinExpr extends FunExpr implements GroupExpr {
038: protected static final L10N L = new L10N(MinExpr.class);
039: private static final Logger log = Log.open(MinExpr.class);
040:
041: private Expr _expr;
042: private int _groupField;
043: private Class _type;
044:
045: protected void addArg(Expr expr) throws SQLException {
046: if (_expr != null)
047: throw new SQLException(L
048: .l("min requires a single argument"));
049:
050: _expr = expr;
051: }
052:
053: protected Expr bind(Query query) throws SQLException {
054: _groupField = query.getDataFields();
055:
056: query.setDataFields(_groupField + 1);
057: query.setGroup(true);
058:
059: _expr = _expr.bind(query);
060:
061: _type = _expr.getType();
062:
063: return this ;
064: }
065:
066: /**
067: * Returns the expected result type of the expression.
068: */
069: public Class getType() {
070: return _type;
071: }
072:
073: /**
074: * Initializes aggregate functions during the group phase.
075: *
076: * @param context the current database tuple
077: */
078: public void initGroup(QueryContext context) throws SQLException {
079: }
080:
081: /**
082: * Evaluates aggregate functions during the group phase.
083: *
084: * @param context the current database tuple
085: */
086: public void evalGroup(QueryContext context) throws SQLException {
087: if (_expr.isNull(context))
088: return;
089:
090: if (_expr.isLong()) {
091: long value = _expr.evalLong(context);
092: long oldValue = context.getGroupLong(_groupField);
093:
094: if (context.isGroupNull(_groupField))
095: context.setGroupLong(_groupField, value);
096: else if (value < oldValue)
097: context.setGroupLong(_groupField, value);
098: } else {
099: double value = _expr.evalDouble(context);
100: double oldValue = context.getGroupDouble(_groupField);
101:
102: if (context.isGroupNull(_groupField))
103: context.setGroupDouble(_groupField, value);
104: else if (value < oldValue)
105: context.setGroupDouble(_groupField, value);
106: }
107: }
108:
109: /**
110: * Returns true if the value is null.
111: *
112: * @param queryContext the query context
113: */
114: public boolean isNull(QueryContext context) throws SQLException {
115: return context.isGroupNull(_groupField);
116: }
117:
118: /**
119: * Evaluates the expression as a double.
120: *
121: * @param rows the current tuple being evaluated
122: *
123: * @return the double value
124: */
125: public double evalDouble(QueryContext context) throws SQLException {
126: return context.getGroupDouble(_groupField);
127: }
128:
129: /**
130: * Evaluates the expression as a long.
131: *
132: * @param rows the current tuple being evaluated
133: *
134: * @return the long value
135: */
136: public long evalLong(QueryContext context) throws SQLException {
137: return context.getGroupLong(_groupField);
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 evalString(QueryContext context) throws SQLException {
148: return context.getGroupString(_groupField);
149: }
150: }
|