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 CountExpr extends FunExpr implements GroupExpr {
038: protected static final L10N L = new L10N(CountExpr.class);
039: private static final Logger log = Log.open(CountExpr.class);
040:
041: private Expr _expr;
042: private int _groupField;
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: _groupField = query.getDataFields();
054:
055: query.setDataFields(_groupField + 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 long.class;
068: }
069:
070: /**
071: * Initializes aggregate functions during the group phase.
072: *
073: * @param context the current database tuple
074: */
075: public void initGroup(QueryContext context) throws SQLException {
076: context.setGroupDouble(_groupField, 0);
077: }
078:
079: /**
080: * Evaluates aggregate functions during the group phase.
081: *
082: * @param context the current database tuple
083: */
084: public void evalGroup(QueryContext context) throws SQLException {
085: if (_expr.isNull(context))
086: return;
087:
088: double oldValue = context.getGroupDouble(_groupField);
089:
090: context.setGroupDouble(_groupField, oldValue + 1);
091: }
092:
093: /**
094: * Returns true for a null value.
095: *
096: * @param rows the current tuple being evaluated
097: *
098: * @return true if null
099: */
100: public boolean isNull(QueryContext context) throws SQLException {
101: return context.isGroupNull(_groupField);
102: }
103:
104: /**
105: * Evaluates the expression as a double.
106: *
107: * @param rows the current tuple being evaluated
108: *
109: * @return the double value
110: */
111: public double evalDouble(QueryContext context) throws SQLException {
112: return context.getGroupDouble(_groupField);
113: }
114:
115: /**
116: * Evaluates the expression as a long.
117: *
118: * @param rows the current tuple being evaluated
119: *
120: * @return the long value
121: */
122: public long evalLong(QueryContext context) throws SQLException {
123: return (long) evalDouble(context);
124: }
125:
126: /**
127: * Evaluates the expression as a string.
128: *
129: * @param rows the current tuple being evaluated
130: *
131: * @return the string value
132: */
133: public String evalString(QueryContext context) throws SQLException {
134: return String.valueOf(evalLong(context));
135: }
136: }
|