1 13 package com.tonbeller.jpivot.olap.mdxparse; 14 15 import org.apache.log4j.Logger; 16 17 21 public class FunCall implements Exp { 22 23 static Logger logger = Logger.getLogger(FunCall.class); 24 25 public static final int TypeFunction = 0; 27 public static final int TypeProperty = 1; public static final int TypeMethod = 2; 29 public static final int TypeInfix = 3; 30 public static final int TypePrefix = 4; 31 public static final int TypeBraces = 5; 32 public static final int TypeParentheses = 6; 33 public static final int TypeCase = 7; 34 public static final int TypeMask = 0xFF; 35 public static final int TypePropertyQuoted = TypeProperty | 0x100; 36 public static final int TypePropertyAmpQuoted = TypeProperty | 0x200; 37 38 private int syntacticType; 39 private String function; 40 private Exp[] args; 41 42 ParsedQuery pQuery = null; 44 public FunCall(String fun, Exp[] args) { 45 this(fun, args, TypeFunction); 46 } 47 48 public FunCall(String fun, Exp[] args, int syntacticType) { 49 this.function = fun; 50 this.args = args; 51 this.syntacticType = syntacticType; 52 } 53 54 57 public String toMdx() { 58 if (this.isCallTo("Parameter") || this.isCallTo("ParamRef")) { 59 return evaluateParameter(); 61 } 62 63 StringBuffer sb = new StringBuffer (); 64 65 78 79 boolean isFollow = false; 80 81 switch (syntacticType) { 82 case TypeFunction : sb.append(function); 84 sb.append("("); 85 for (int i = 0; i < args.length; i++) { 86 if (isFollow) 87 sb.append(", "); 88 isFollow = true; 89 sb.append(args[i].toMdx()); 90 } 91 sb.append(")"); 92 break; 93 94 case TypeBraces : sb.append("{"); 96 for (int i = 0; i < args.length; i++) { 97 if (isFollow) 98 sb.append(", "); 99 isFollow = true; 100 sb.append(args[i].toMdx()); 101 } 102 sb.append("}"); 103 break; 104 105 case TypeParentheses : sb.append("("); 107 for (int i = 0; i < args.length; i++) { 108 if (isFollow) 109 sb.append(", "); 110 isFollow = true; 111 sb.append(args[i].toMdx()); 112 } 113 sb.append(")"); 114 break; 115 116 case TypePrefix : sb.append(function); 118 sb.append(" "); 119 sb.append(args[0].toMdx()); 120 break; 121 122 case TypeInfix : sb.append(args[0].toMdx()); 124 sb.append(" "); 125 sb.append(function); 126 sb.append(" "); 127 sb.append(args[1].toMdx()); 128 break; 129 130 case TypeProperty : case TypePropertyQuoted : 132 case TypePropertyAmpQuoted : 133 sb.append(args[0].toMdx()); 134 sb.append("."); 135 sb.append(function); 136 break; 137 138 default : 139 System.out.println("unexpected FunCall syntatic type"); 140 141 } 142 return sb.toString(); 143 } 144 145 149 public Object clone() { 150 Exp[] cloneArgs = new Exp[args.length]; 151 for (int i = 0; i < cloneArgs.length; i++) { 152 cloneArgs[i] = (Exp) args[i].clone(); 153 } 154 return new FunCall(function, cloneArgs, syntacticType); 155 } 156 157 162 public boolean isCallTo(String fName) { 163 return (fName.compareToIgnoreCase(function) == 0); 164 } 165 166 170 public Exp[] getArgs() { 171 return args; 172 } 173 174 177 public String getFunction() { 178 return function; 179 } 180 181 184 private String evaluateParameter() { 185 Literal eName = (Literal) args[0]; 186 String paraName = eName.stringValue(); 187 Parameter param = (Parameter) pQuery.paraMap.get(paraName.toUpperCase()); 188 if (param == null) { 189 logger.error("could not find parameter " + paraName); 191 return ("Parameter( \"" + paraName + "\" )"); } 193 int type = param.getType(); 194 if (type == Parameter.TYPE_NUMERIC) { 195 Object value = param.getOValue(); 196 return value.toString(); } else if (type == Parameter.TYPE_STRING) { 198 String str = (String ) param.getOValue(); 199 return "\"" + str + "\""; 200 } else { 201 String str = (String ) param.getOValue(); 203 return str; 204 205 } 206 207 } 208 209 212 public void accept(ExpVisitor visitor) { 213 visitor.visitFunCall(this); 214 } 215 216 } | Popular Tags |