001    /*
002    // $Id: WithSetNode.java 482 2012-01-05 23:27:27Z jhyde $
003    //
004    // Licensed to Julian Hyde under one or more contributor license
005    // agreements. See the NOTICE file distributed with this work for
006    // additional information regarding copyright ownership.
007    //
008    // Julian Hyde licenses this file to you under the Apache License,
009    // Version 2.0 (the "License"); you may not use this file except in
010    // compliance with 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    package org.olap4j.mdx;
021    
022    import org.olap4j.type.Type;
023    
024    import java.io.PrintWriter;
025    
026    /**
027     * Parse tree node which declares a calculated set. Represented as the
028     * <code>WITH SET</code> clause of an MDX <code>SELECT</code> statement.
029     *
030     * @version $Id: WithSetNode.java 482 2012-01-05 23:27:27Z jhyde $
031     * @author jhyde
032     */
033    public class WithSetNode implements ParseTreeNode {
034    
035        private final ParseRegion region;
036        /** name of set */
037        private final IdentifierNode name;
038    
039        /** defining expression */
040        private ParseTreeNode expression;
041    
042        /**
043         * Creates a declaration of a named set.
044         *
045         * @param region Region of source code
046         * @param name Name of set
047         * @param expression Expression to calculate set
048         */
049        public WithSetNode(
050            ParseRegion region,
051            IdentifierNode name,
052            ParseTreeNode expression)
053        {
054            this.region = region;
055            this.name = name;
056            this.expression = expression;
057        }
058    
059        public ParseRegion getRegion() {
060            return region;
061        }
062    
063        public void unparse(ParseTreeWriter writer) {
064            PrintWriter pw = writer.getPrintWriter();
065            pw.print("SET ");
066            name.unparse(writer);
067            writer.indent();
068            pw.println(" AS");
069            expression.unparse(writer);
070            writer.outdent();
071        }
072    
073        /**
074         * Returns the name of the set.
075         *
076         * @return name of the set
077         */
078        public IdentifierNode getIdentifier() {
079            return name;
080        }
081    
082        /**
083         * Returns the expression which calculates the set.
084         *
085         * @return expression which calculates the set
086         */
087        public ParseTreeNode getExpression() {
088            return expression;
089        }
090    
091        /**
092         * Sets the expression which calculates the set.
093         *
094         * @param expression expression which calculates the set
095         */
096        public void setExpression(ParseTreeNode expression) {
097            this.expression = expression;
098        }
099    
100        public <T> T accept(ParseTreeVisitor<T> visitor) {
101            final T t = visitor.visit(this);
102            name.accept(visitor);
103            expression.accept(visitor);
104            return t;
105        }
106    
107        public Type getType() {
108            // not an expression
109            throw new UnsupportedOperationException();
110        }
111    
112        public WithSetNode deepCopy() {
113            return new WithSetNode(
114                this.region,
115                this.name.deepCopy(),
116                this.expression.deepCopy());
117        }
118    }
119    
120    // End WithSetNode.java