001    /*
002    // $Id: WithSetNode.java 349 2010-08-12 08:49:13Z 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-2010 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 349 2010-08-12 08:49:13Z 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            writer.indent();
058            pw.println(" AS");
059            expression.unparse(writer);
060            writer.outdent();
061        }
062    
063        /**
064         * Returns the name of the set.
065         *
066         * @return name of the set
067         */
068        public IdentifierNode getIdentifier() {
069            return name;
070        }
071    
072        /**
073         * Returns the expression which calculates the set.
074         *
075         * @return expression which calculates the set
076         */
077        public ParseTreeNode getExpression() {
078            return expression;
079        }
080    
081        /**
082         * Sets the expression which calculates the set.
083         *
084         * @param expression expression which calculates the set
085         */
086        public void setExpression(ParseTreeNode expression) {
087            this.expression = expression;
088        }
089    
090        public <T> T accept(ParseTreeVisitor<T> visitor) {
091            final T t = visitor.visit(this);
092            name.accept(visitor);
093            expression.accept(visitor);
094            return t;
095        }
096    
097        public Type getType() {
098            // not an expression
099            throw new UnsupportedOperationException();
100        }
101    
102        public WithSetNode deepCopy() {
103            return new WithSetNode(
104                this.region,
105                this.name.deepCopy(),
106                this.expression.deepCopy());
107        }
108    }
109    
110    // End WithSetNode.java