001    /*
002    // $Id: WithSetNode.java 229 2009-05-08 19:11:29Z jhyde $
003    // This software is subject to the terms of the Eclipse Public License v1.0
004    // Agreement, available at the following URL:
005    // http://www.eclipse.org/legal/epl-v10.html.
006    // Copyright (C) 2007-2008 Julian Hyde
007    // All Rights Reserved.
008    // You must accept the terms of that agreement to use this software.
009    */
010    package org.olap4j.mdx;
011    
012    import org.olap4j.type.Type;
013    
014    import java.io.PrintWriter;
015    
016    /**
017     * Parse tree node which declares a calculated set. Represented as the
018     * <code>WITH SET</code> clause of an MDX <code>SELECT</code> statement.
019     *
020     * @version $Id: WithSetNode.java 229 2009-05-08 19:11:29Z jhyde $
021     * @author jhyde
022     */
023    public class WithSetNode implements ParseTreeNode {
024    
025        private final ParseRegion region;
026        /** name of set */
027        private final IdentifierNode name;
028    
029        /** defining expression */
030        private ParseTreeNode expression;
031    
032        /**
033         * Creates a declaration of a named set.
034         *
035         * @param region Region of source code
036         * @param name Name of set
037         * @param expression Expression to calculate set
038         */
039        public WithSetNode(
040            ParseRegion region,
041            IdentifierNode name,
042            ParseTreeNode expression)
043        {
044            this.region = region;
045            this.name = name;
046            this.expression = expression;
047        }
048    
049        public ParseRegion getRegion() {
050            return region;
051        }
052    
053        public void unparse(ParseTreeWriter writer) {
054            PrintWriter pw = writer.getPrintWriter();
055            pw.print("SET ");
056            name.unparse(writer);
057            pw.print(" AS '");
058            expression.unparse(writer);
059            pw.print("'");
060        }
061    
062        /**
063         * Returns the name of the set.
064         *
065         * @return name of the set
066         */
067        public IdentifierNode getIdentifier() {
068            return name;
069        }
070    
071        /**
072         * Returns the expression which calculates the set.
073         *
074         * @return expression which calculates the set
075         */
076        public ParseTreeNode getExpression() {
077            return expression;
078        }
079    
080        /**
081         * Sets the expression which calculates the set.
082         *
083         * @param expression expression which calculates the set
084         */
085        public void setExpression(ParseTreeNode expression) {
086            this.expression = expression;
087        }
088    
089        public <T> T accept(ParseTreeVisitor<T> visitor) {
090            final T t = visitor.visit(this);
091            name.accept(visitor);
092            expression.accept(visitor);
093            return t;
094        }
095    
096        public Type getType() {
097            // not an expression
098            throw new UnsupportedOperationException();
099        }
100    
101        public WithSetNode deepCopy() {
102            return new WithSetNode(
103                this.region,
104                this.name.deepCopy(),
105                this.expression.deepCopy());
106        }
107    }
108    
109    // End WithSetNode.java