Example #1
0
  /**
   * Returns two membercalc objects, substituting nulls with the hierarchy null member of the other
   * expression.
   *
   * @param exp0 first expression
   * @param exp1 second expression
   * @return two member calcs
   */
  private MemberCalc[] compileMembers(Exp exp0, Exp exp1, ExpCompiler compiler) {
    MemberCalc[] members = new MemberCalc[2];

    if (exp0.getType() instanceof NullType) {
      members[0] = null;
    } else {
      members[0] = compiler.compileMember(exp0);
    }

    if (exp1.getType() instanceof NullType) {
      members[1] = null;
    } else {
      members[1] = compiler.compileMember(exp1);
    }

    // replace any null types with hierachy null member
    // if both objects are null, throw exception

    if (members[0] == null && members[1] == null) {
      throw MondrianResource.instance().TwoNullsNotSupported.ex();
    } else if (members[0] == null) {
      Member nullMember = ((RolapMember) members[1].evaluate(null)).getHierarchy().getNullMember();
      members[0] = (MemberCalc) ConstantCalc.constantMember(nullMember);
    } else if (members[1] == null) {
      Member nullMember = ((RolapMember) members[0].evaluate(null)).getHierarchy().getNullMember();
      members[1] = (MemberCalc) ConstantCalc.constantMember(nullMember);
    }

    return members;
  }
  public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
    final Exp[] args = call.getArgs();
    final BooleanCalc[] conditionCalcs = new BooleanCalc[args.length / 2];
    final Calc[] exprCalcs = new Calc[args.length / 2];
    final List<Calc> calcList = new ArrayList<Calc>();
    for (int i = 0, j = 0; i < exprCalcs.length; i++) {
      conditionCalcs[i] = compiler.compileBoolean(args[j++]);
      calcList.add(conditionCalcs[i]);
      exprCalcs[i] = compiler.compile(args[j++]);
      calcList.add(exprCalcs[i]);
    }
    final Calc defaultCalc =
        args.length % 2 == 1
            ? compiler.compileScalar(args[args.length - 1], true)
            : ConstantCalc.constantNull(call.getType());
    calcList.add(defaultCalc);
    final Calc[] calcs = calcList.toArray(new Calc[calcList.size()]);

    return new GenericCalc(call) {
      public Object evaluate(Evaluator evaluator) {
        for (int i = 0; i < conditionCalcs.length; i++) {
          if (conditionCalcs[i].evaluateBoolean(evaluator)) {
            return exprCalcs[i].evaluate(evaluator);
          }
        }
        return defaultCalc.evaluate(evaluator);
      }

      public Calc[] getCalcs() {
        return calcs;
      }
    };
  }