1 10 package mondrian.olap; 11 12 import java.io.PrintWriter ; 13 14 21 public enum Syntax { 22 26 Function { 27 public void unparse(String fun, Exp[] args, PrintWriter pw) { 28 ExpBase.unparseList(pw, args, fun + "(", ", ", ")"); 29 } 30 }, 31 32 35 Property { 36 public void unparse(String fun, Exp[] args, PrintWriter pw) { 37 Util.assertTrue(args.length >= 1); 38 args[0].unparse(pw); pw.print("."); 40 pw.print(fun); 41 } 42 43 public String getSignature(String name, int returnType, int[] argTypes) { 44 return getTypeDescription(argTypes[0]) + "." + name; 46 } 47 }, 48 49 54 Method { 55 public void unparse(String fun, Exp[] args, PrintWriter pw) { 56 Util.assertTrue(args.length >= 1); 57 args[0].unparse(pw); pw.print("."); 59 pw.print(fun); 60 pw.print("("); 61 for (int i = 1; i < args.length; i++) { 62 if (i > 1) { 63 pw.print(", "); 64 } 65 args[i].unparse(pw); 66 } 67 pw.print(")"); 68 } 69 70 public String getSignature(String name, int returnType, int[] argTypes) { 71 return (returnType == Category.Unknown ? "" : 73 getTypeDescription(returnType) + " ") + 74 getTypeDescription(argTypes[0]) + "." + 75 name + "(" + getTypeDescriptionCommaList(argTypes, 1) + 76 ")"; 77 } 78 }, 79 80 84 Infix { 85 public void unparse(String fun, Exp[] args, PrintWriter pw) { 86 if (needParen(args)) { 87 ExpBase.unparseList(pw, args, "(", " " + fun + " ", ")"); 88 } else { 89 ExpBase.unparseList(pw, args, "", " " + fun + " ", ""); 90 } 91 } 92 93 public String getSignature(String name, int returnType, int[] argTypes) { 94 return getTypeDescription(argTypes[0]) + " " + name + " " + 96 getTypeDescription(argTypes[1]); 97 } 98 }, 99 100 104 Prefix { 105 public void unparse(String fun, Exp[] args, PrintWriter pw) { 106 if (needParen(args)) { 107 ExpBase.unparseList(pw, args, "(" + fun + " ", null, ")"); 108 } else { 109 ExpBase.unparseList(pw, args, fun + " ", null, ""); 110 } 111 } 112 113 public String getSignature(String name, int returnType, int[] argTypes) { 114 return name + " " + getTypeDescription(argTypes[0]); 116 } 117 }, 118 119 123 Postfix { 124 public void unparse(String fun, Exp[] args, PrintWriter pw) { 125 if (needParen(args)) { 126 ExpBase.unparseList(pw, args, "(", null, " " + fun + ")"); 127 } else { 128 ExpBase.unparseList(pw, args, "", null, " " + fun); 129 } 130 } 131 132 public String getSignature(String name, int returnType, int[] argTypes) { 133 return getTypeDescription(argTypes[0]) + " " + name; 135 } 136 }, 137 138 143 Braces { 144 public String getSignature(String name, int returnType, int[] argTypes) { 145 return "{" + getTypeDescriptionCommaList(argTypes, 0) + "}"; 146 } 147 148 public void unparse(String fun, Exp[] args, PrintWriter pw) { 149 ExpBase.unparseList(pw, args, "{", ", ", "}"); 150 } 151 }, 152 153 158 Parentheses { 159 public String getSignature(String name, int returnType, int[] argTypes) { 160 return "(" + getTypeDescriptionCommaList(argTypes, 0) + ")"; 161 } 162 163 public void unparse(String fun, Exp[] args, PrintWriter pw) { 164 ExpBase.unparseList(pw, args, "(", ", ", ")"); 165 } 166 }, 167 168 171 Case { 172 public void unparse(String fun, Exp[] args, PrintWriter pw) { 173 if (fun.equals("_CaseTest")) { 174 pw.print("CASE"); 175 int j = 0; 176 int clauseCount = (args.length - j) / 2; 177 for (int i = 0; i < clauseCount; i++) { 178 pw.print(" WHEN "); 179 args[j++].unparse(pw); 180 pw.print(" THEN "); 181 args[j++].unparse(pw); 182 } 183 if (j < args.length) { 184 pw.print(" ELSE "); 185 args[j++].unparse(pw); 186 } 187 Util.assertTrue(j == args.length); 188 pw.print(" END"); 189 } else { 190 Util.assertTrue(fun.equals("_CaseMatch")); 191 192 pw.print("CASE "); 193 int j = 0; 194 args[j++].unparse(pw); 195 int clauseCount = (args.length - j) / 2; 196 for (int i = 0; i < clauseCount; i++) { 197 pw.print(" WHEN "); 198 args[j++].unparse(pw); 199 pw.print(" THEN "); 200 args[j++].unparse(pw); 201 } 202 if (j < args.length) { 203 pw.print(" ELSE "); 204 args[j++].unparse(pw); 205 } 206 Util.assertTrue(j == args.length); 207 pw.print(" END"); 208 } 209 } 210 211 public String getSignature(String name, int returnType, int[] argTypes) { 212 String s = getTypeDescription(argTypes[0]); 213 if (argTypes[0] == Category.Logical) { 214 return "CASE WHEN " + s + " THEN <Expression> ... END"; 215 } else { 216 return "CASE " + s + " WHEN " + s + " THEN <Expression> ... END"; 217 } 218 } 219 }, 220 221 225 Internal, 226 227 231 Cast { 232 public void unparse(String fun, Exp[] args, PrintWriter pw) { 233 pw.print("CAST("); 234 args[0].unparse(pw); 235 pw.print(" AS "); 236 args[1].unparse(pw); 237 pw.print(")"); 238 } 239 240 public String getSignature(String name, int returnType, int[] argTypes) { 241 return "CAST(<Expression> AS <Type>)"; 242 } 243 }, 244 245 249 QuotedProperty, 250 251 255 AmpersandQuotedProperty; 256 257 264 public void unparse(String fun, Exp[] args, PrintWriter pw) { 265 throw new UnsupportedOperationException (); 266 } 267 268 277 public String getSignature(String name, int returnType, int[] argTypes) { 278 return (returnType == Category.Unknown ? "" : 280 getTypeDescription(returnType) + " ") + 281 name + "(" + getTypeDescriptionCommaList(argTypes, 0) + 282 ")"; 283 } 284 285 private static boolean needParen(Exp[] args) { 286 return !(args.length == 1 && 287 args[0] instanceof FunCall && 288 ((FunCall) args[0]).getSyntax() == Syntax.Parentheses); 289 } 290 291 private static String getTypeDescription(int type) { 292 return "<" + Category.instance.getDescription(type & Category.Mask) + ">"; 293 } 294 295 private static String getTypeDescriptionCommaList(int[] types, int start) { 296 int initialSize = (types.length - start) * 16; 297 StringBuilder sb = new StringBuilder (initialSize > 0 ? initialSize : 16); 298 for (int i = start; i < types.length; i++) { 299 if (i > start) { 300 sb.append(", "); 301 } 302 sb.append("<") 303 .append(Category.instance.getDescription(types[i] & Category.Mask)) 304 .append(">"); 305 } 306 return sb.toString(); 307 } 308 } 309 310 | Popular Tags |