001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.ReplaceAggregatesWithCRVisitor
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql.compile;
023:
024: import org.apache.derby.iapi.sql.compile.Visitable;
025: import org.apache.derby.iapi.sql.compile.Visitor;
026:
027: import org.apache.derby.iapi.error.StandardException;
028:
029: /**
030: * Replace all aggregates with result columns.
031: *
032: * @author jamie
033: */
034: public class ReplaceAggregatesWithCRVisitor implements Visitor {
035: private ResultColumnList rcl;
036: private Class skipOverClass;
037: private int tableNumber;
038:
039: /**
040: * Replace all aggregates with column references. Add
041: * the reference to the RCL. Delegates most work to
042: * AggregateNode.replaceAggregatesWithColumnReferences(rcl, tableNumber).
043: *
044: * @param rcl the result column list
045: * @param tableNumber The tableNumber for the new CRs
046: */
047: public ReplaceAggregatesWithCRVisitor(ResultColumnList rcl,
048: int tableNumber) {
049: this .rcl = rcl;
050: this .tableNumber = tableNumber;
051: }
052:
053: /**
054: * Replace all aggregates with column references. Add
055: * the reference to the RCL. Delegates most work to
056: * AggregateNode.replaceAggregatesWithColumnReferences(rcl).
057: * Doesn't traverse below the passed in class.
058: *
059: * @param rcl the result column list
060: * @param nodeToSkip don't examine anything below nodeToSkip
061: */
062: public ReplaceAggregatesWithCRVisitor(ResultColumnList rcl,
063: Class nodeToSkip) {
064: this .rcl = rcl;
065: this .skipOverClass = nodeToSkip;
066: }
067:
068: ////////////////////////////////////////////////
069: //
070: // VISITOR INTERFACE
071: //
072: ////////////////////////////////////////////////
073:
074: /**
075: * Don't do anything unless we have an aggregate
076: * node.
077: *
078: * @param node the node to process
079: *
080: * @return me
081: *
082: * @exception StandardException on error
083: */
084: public Visitable visit(Visitable node) throws StandardException {
085: if (node instanceof AggregateNode) {
086: /*
087: ** Let aggregateNode replace itself.
088: */
089: node = ((AggregateNode) node)
090: .replaceAggregatesWithColumnReferences(rcl,
091: tableNumber);
092: }
093:
094: return node;
095: }
096:
097: /**
098: * Don't visit childen under the skipOverClass
099: * node, if it isn't null.
100: *
101: * @return true/false
102: */
103: public boolean skipChildren(Visitable node) {
104: return (skipOverClass == null) ? false : skipOverClass
105: .isInstance(node);
106: }
107:
108: public boolean stopTraversal() {
109: return false;
110: }
111: }
|