001: /*
002: * $Id: AssertGroupByRulesVisitor.java,v 1.4 2005/12/20 18:32:27 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2004 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040: package org.axiondb.engine.visitors;
041:
042: import java.util.List;
043:
044: import org.axiondb.AxionException;
045: import org.axiondb.ColumnIdentifier;
046: import org.axiondb.Selectable;
047: import org.axiondb.SelectableVisitor;
048: import org.axiondb.functions.ScalarFunction;
049:
050: /**
051: * Assert general rules for Group By for Select Command.
052: *
053: * @author Ahimanikya Satapathy
054: */
055: public class AssertGroupByRulesVisitor implements SelectableVisitor {
056:
057: public boolean visit(List selectList, List groupByNodes)
058: throws AxionException {
059: _groupByNodes = groupByNodes;
060:
061: if (groupByNodes != null && !groupByNodes.isEmpty()) {
062: AmbiguousColumnReferenceVisitor ambiguityCheck = new AmbiguousColumnReferenceVisitor();
063: ambiguityCheck.visit(selectList, groupByNodes);
064: }
065:
066: for (int i = 0, I = selectList.size(); i < I; i++) {
067: Selectable sel = (Selectable) selectList.get(i);
068: FindAggregateFunctionVisitor findAggr = new FindAggregateFunctionVisitor();
069: findAggr.visit(sel); // check for aggregate functions
070: if (findAggr.foundAggregateFunction()) {
071: _foundAggregateFn = true;
072: continue;
073: }
074: if (_groupByNodes == null || _groupByNodes.isEmpty()) {
075: _foundScalar = true;
076: } else {
077: visit(sel);
078: }
079: }
080:
081: if (_foundScalar && _foundAggregateFn) {
082: throw new AxionException(
083: "Can't select both scalar values and aggregate functions.");
084: }
085:
086: if (_foundScalar && _groupByNodes != null
087: && !_groupByNodes.isEmpty()) {
088: throw new AxionException("Invalid Group By Expression...");
089: }
090: return _foundScalar;
091: }
092:
093: // Allow Scalar function on the group by columns
094: public void visit(Selectable select) {
095: if (select instanceof ScalarFunction) {
096: ScalarFunction fn = (ScalarFunction) select;
097: for (int i = 0, I = fn.getArgumentCount(); i < I; i++) {
098: Selectable fnArg = fn.getArgument(i);
099: visit(fnArg);
100: }
101: } else if (select instanceof ColumnIdentifier) {
102: // column is not part of GroupBy column list
103: if (!_groupByNodes.contains(select)) {
104: _foundScalar = true;
105: }
106: }
107: }
108:
109: private boolean _foundAggregateFn = false;
110: private boolean _foundScalar = false;
111: private List _groupByNodes;
112: }
|