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: /**
038: * Represents a GROUP BY expression.
039: */
040: public class GroupResultExpr extends Expr implements GroupExpr {
041: protected static final L10N L = new L10N(GroupResultExpr.class);
042: private static final Logger log = Log.open(GroupResultExpr.class);
043:
044: private static final int LONG_VALUE = 1;
045: private static final int DOUBLE_VALUE = 2;
046: private static final int STRING_VALUE = 3;
047:
048: private Expr _expr;
049: private int _index;
050: private int _type;
051:
052: GroupResultExpr(int index, Expr expr) {
053: _index = index;
054: _expr = expr;
055:
056: if (expr instanceof GroupResultExpr)
057: Thread.dumpStack();
058:
059: if (_expr.isLong())
060: _type = LONG_VALUE;
061: else if (_expr.isDouble())
062: _type = DOUBLE_VALUE;
063: else
064: _type = STRING_VALUE;
065: }
066:
067: /**
068: * Returns any name.
069: */
070: public String getName() {
071: return _expr.getName();
072: }
073:
074: protected Expr bind(Query query) throws SQLException {
075: _expr = _expr.bind(query);
076:
077: return this ;
078: }
079:
080: /**
081: * Evaluates aggregate functions during the group phase.
082: *
083: * @param context the current database tuple
084: */
085: public void evalGroup(QueryContext context) throws SQLException {
086: if (_expr.isNull(context))
087: return;
088:
089: switch (_type) {
090: default: {
091: String value = _expr.evalString(context);
092:
093: context.setGroupString(_index, value);
094: }
095: break;
096: }
097: }
098:
099: public String evalString(QueryContext context) {
100: return context.getGroupString(_index);
101: }
102:
103: public String toString() {
104: return "GroupResult[" + _expr + "]";
105: }
106: }
|