1 10 package mondrian.mdx; 11 12 import mondrian.olap.*; 13 import mondrian.olap.type.Type; 14 import mondrian.olap.type.TypeUtil; 15 import mondrian.calc.Calc; 16 import mondrian.calc.ExpCompiler; 17 import mondrian.calc.ParameterCompilable; 18 19 import java.io.PrintWriter ; 20 21 27 public class ParameterExpr extends ExpBase { 28 29 private Parameter parameter; 30 31 public ParameterExpr(Parameter parameter) 32 { 33 this.parameter = parameter; 34 } 35 36 public Type getType() { 37 return parameter.getType(); 38 } 39 40 public int getCategory() { 41 return TypeUtil.typeToCategory(parameter.getType()); 42 } 43 44 public Exp accept(Validator validator) { 45 String parameterName = parameter.getName(); 52 Parameter p = validator.getQuery().getSchemaReader(false).getParameter( 53 parameterName); 54 if (p == null) { 55 this.parameter = validator.createOrLookupParam( 56 true, 57 parameter.getName(), 58 parameter.getType(), 59 parameter.getDefaultExp(), 60 parameter.getDescription()); 61 } 62 else { 63 this.parameter = p; 64 } 65 return this; 66 } 67 68 public Calc accept(ExpCompiler compiler) { 69 return ((ParameterCompilable) parameter).compile(compiler); 70 } 71 72 public Object accept(MdxVisitor visitor) { 73 return visitor.visit(this); 74 } 75 76 public ParameterExpr clone() { 77 return new ParameterExpr(parameter); 78 } 79 80 90 public void unparse(PrintWriter pw) { 91 final boolean def; 95 if (pw instanceof QueryPrintWriter && 96 parameter.getScope() == Parameter.Scope.Statement) { 97 def = ((QueryPrintWriter) pw).parameters.add(parameter); 98 } else { 99 def = false; 100 } 101 final String name = parameter.getName(); 102 final Type type = parameter.getType(); 103 final int category = TypeUtil.typeToCategory(type); 104 if (def) { 105 pw.print("Parameter(" + Util.quoteForMdx(name) + ", "); 106 switch (category) { 107 case Category.String: 108 case Category.Numeric: 109 pw.print(Category.instance.getName(category).toUpperCase()); 110 break; 111 case Category.Member: 112 String memberName = 113 type.getLevel() != null ? 114 type.getLevel().getUniqueName() : 115 type.getHierarchy() != null ? 116 type.getHierarchy().getUniqueName() : 117 type.getDimension().getUniqueName(); 118 pw.print(memberName); 119 break; 120 default: 121 throw Category.instance.badValue(category); 122 } 123 pw.print(", "); 124 final Object value = parameter.getValue(); 125 if (value == null) { 126 parameter.getDefaultExp().unparse(pw); 127 } else if (value instanceof String ) { 128 String s = (String ) value; 129 pw.print(Util.quoteForMdx(s)); 130 } else { 131 pw.print(value); 132 } 133 final String description = parameter.getDescription(); 134 if (description != null) { 135 pw.print(", " + Util.quoteForMdx(description)); 136 } 137 pw.print(")"); 138 } else { 139 pw.print("ParamRef(" + Util.quoteForMdx(name) + ")"); 140 } 141 } 142 143 public Object [] getChildren() { 146 return null; 147 } 148 149 153 public boolean equals(Object other) { 154 if (!(other instanceof ParameterExpr)) { 155 return false; 156 } 157 ParameterExpr that = (ParameterExpr) other; 158 return this.parameter == that.parameter; 159 } 160 161 164 public boolean isModifiable() { 165 return true; 166 } 167 168 171 public Parameter getParameter() { 172 return parameter; 173 } 174 } 175 176 | Popular Tags |