001    /*
002    // $Id: ParameterNode.java 243 2009-05-22 07:21:37Z 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     * A parameter to an MDX query.
018     *
019     * <p>Not all dialects of MDX support parameters. If a dialect supports
020     * parameters, the driver for that dialect should extend the parser to
021     * introduce a ParameterNode into the parse tree wherever a parameter is
022     * encountered.
023     *
024     * <p>For example, in Mondrian's dialect of MDX, a call to the <code>Param(name,
025     * type, defaultValueExpr)</code> function introduces a parameter, and
026     * <code>ParamRef(name)</code> creates a reference to a parameter defined
027     * elsewhere in the query.
028     *
029     * @version $Id: ParameterNode.java 243 2009-05-22 07:21:37Z jhyde $
030     */
031    public class ParameterNode implements ParseTreeNode {
032        private String name;
033        private Type type;
034        private ParseTreeNode defaultValueExpression;
035        private final ParseRegion region;
036    
037        /**
038         * Creates a ParameterNode.
039         *
040         * <p>The <code>name</code> must not be null, and the
041         * <code>defaultValueExpression</code> must be consistent with the
042         * <code>type</code>.
043         *
044         * @param region Region of source code
045         * @param name Name of parameter
046         * @param type Type of parameter
047         * @param defaultValueExpression Expression which yields the default value
048         * of the parameter
049         */
050        public ParameterNode(
051            ParseRegion region,
052            String name,
053            Type type,
054            ParseTreeNode defaultValueExpression)
055        {
056            assert name != null;
057            assert type != null;
058            assert defaultValueExpression != null;
059            this.region = region;
060            this.name = name;
061            this.type = type;
062            this.defaultValueExpression = defaultValueExpression;
063        }
064    
065        public ParseRegion getRegion() {
066            return region;
067        }
068    
069        public <T> T accept(ParseTreeVisitor<T> visitor) {
070            final T t = visitor.visit(this);
071            defaultValueExpression.accept(visitor);
072            return t;
073        }
074    
075        public void unparse(ParseTreeWriter writer) {
076            PrintWriter pw = writer.getPrintWriter();
077            pw.print("Param(");
078            pw.print(MdxUtil.quoteForMdx(name));
079            pw.print(", ");
080            pw.print(type);
081            pw.print(", ");
082            defaultValueExpression.unparse(writer);
083            pw.print(")");
084        }
085    
086        public Type getType() {
087            // not an expression
088            return null;
089        }
090    
091        /**
092         * Returns the name of this parameter.
093         *
094         * @return name of this parameter
095         */
096        public String getName() {
097            return name;
098        }
099    
100        /**
101         * Sets the name of this parameter.
102         *
103         * @param name Parameter name
104         */
105        public void setName(String name) {
106            this.name = name;
107        }
108    
109        /**
110         * Sets the type of this parameter.
111         *
112         * @param type Type
113         */
114        public void setType(Type type) {
115            this.type = type;
116        }
117    
118    
119        /**
120         * Returns the expression which yields the default value of this parameter.
121         *
122         * @return expression which yields the default value of this parameter
123         */
124        public ParseTreeNode getDefaultValueExpression() {
125            return defaultValueExpression;
126        }
127    
128        /**
129         * Sets the expression which yields the default value of this parameter.
130         *
131         * @param defaultValueExpression default value expression
132         */
133        public void setDefaultValueExpression(ParseTreeNode defaultValueExpression)
134        {
135            this.defaultValueExpression = defaultValueExpression;
136        }
137    
138        public ParameterNode deepCopy() {
139            return new ParameterNode(
140                this.region,
141                this.name,
142                this.type, // types are immutable
143                this.defaultValueExpression.deepCopy());
144        }
145    }
146    
147    // End ParameterNode.java