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-2009 Pentaho
008// All Rights Reserved.
009*/
010package mondrian.olap.fun;
011
012import mondrian.calc.Calc;
013import mondrian.calc.ExpCompiler;
014import mondrian.calc.impl.AbstractIntegerCalc;
015import mondrian.mdx.NamedSetExpr;
016import mondrian.mdx.ResolvedFunCall;
017import mondrian.olap.*;
018import mondrian.resource.MondrianResource;
019
020/**
021 * Definition of the <code>&lt;Named Set&gt;.CurrentOrdinal</code> MDX builtin
022 * function.
023 *
024 * @author jhyde
025 * @since Oct 19, 2008
026 */
027public class NamedSetCurrentOrdinalFunDef extends FunDefBase {
028    static final NamedSetCurrentOrdinalFunDef instance =
029        new NamedSetCurrentOrdinalFunDef();
030
031    private NamedSetCurrentOrdinalFunDef() {
032        super(
033            "CurrentOrdinal",
034            "Returns the ordinal of the current iteration through a named set.",
035            "pix");
036    }
037
038    public Exp createCall(Validator validator, Exp[] args) {
039        assert args.length == 1;
040        final Exp arg0 = args[0];
041        if (!(arg0 instanceof NamedSetExpr)) {
042            throw MondrianResource.instance().NotANamedSet.ex();
043        }
044        return super.createCall(validator, args);
045    }
046
047    public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
048        final Exp arg0 = call.getArg(0);
049        assert arg0 instanceof NamedSetExpr : "checked this in createCall";
050        final NamedSetExpr namedSetExpr = (NamedSetExpr) arg0;
051        return new AbstractIntegerCalc(call, new Calc[0]) {
052            public int evaluateInteger(Evaluator evaluator) {
053                return namedSetExpr.getEval(evaluator).currentOrdinal();
054            }
055        };
056    }
057}
058
059// End NamedSetCurrentOrdinalFunDef.java