001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2006-2011 Pentaho
008// All Rights Reserved.
009*/
010package mondrian.olap.fun;
011
012import mondrian.calc.Calc;
013import mondrian.calc.ExpCompiler;
014import mondrian.calc.impl.AbstractMemberCalc;
015import mondrian.calc.impl.AbstractTupleCalc;
016import mondrian.mdx.NamedSetExpr;
017import mondrian.mdx.ResolvedFunCall;
018import mondrian.olap.*;
019import mondrian.resource.MondrianResource;
020
021/**
022 * Definition of the <code>&lt;Named Set&gt;.Current</code> MDX
023 * builtin function.
024 *
025 * @author jhyde
026 * @since Oct 19, 2008
027 */
028public class NamedSetCurrentFunDef extends FunDefBase {
029    static final NamedSetCurrentFunDef instance =
030        new NamedSetCurrentFunDef();
031
032    private NamedSetCurrentFunDef() {
033        super(
034            "Current",
035            "Returns the current member or tuple of a named set.",
036            "ptx");
037    }
038
039    public Exp createCall(Validator validator, Exp[] args) {
040        assert args.length == 1;
041        final Exp arg0 = args[0];
042        if (!(arg0 instanceof NamedSetExpr)) {
043            throw MondrianResource.instance().NotANamedSet.ex();
044        }
045        return super.createCall(validator, args);
046    }
047
048    public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
049        final Exp arg0 = call.getArg(0);
050        assert arg0 instanceof NamedSetExpr : "checked this in createCall";
051        final NamedSetExpr namedSetExpr = (NamedSetExpr) arg0;
052        if (arg0.getType().getArity() == 1) {
053            return new AbstractMemberCalc(call, new Calc[0]) {
054                public Member evaluateMember(Evaluator evaluator) {
055                    return namedSetExpr.getEval(evaluator).currentMember();
056                }
057            };
058        } else {
059            return new AbstractTupleCalc(call, new Calc[0]) {
060                public Member[] evaluateTuple(Evaluator evaluator) {
061                    return namedSetExpr.getEval(evaluator).currentTuple();
062                }
063            };
064        }
065    }
066}
067
068// End NamedSetCurrentFunDef.java