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