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.*;
013import mondrian.calc.impl.AbstractStringCalc;
014import mondrian.mdx.ResolvedFunCall;
015import mondrian.olap.Evaluator;
016import mondrian.olap.Member;
017import mondrian.olap.type.TypeUtil;
018
019/**
020 * Definition of the <code>TupleToStr</code> MDX function.
021 *
022 * <p>Syntax:
023 * <blockquote><code>
024 * TupleToStr(&lt;Tuple&gt;)
025 * </code></blockquote>
026 *
027 * @author jhyde
028 * @since Aug 3, 2006
029 */
030class TupleToStrFunDef extends FunDefBase {
031    static final TupleToStrFunDef instance = new TupleToStrFunDef();
032
033    private TupleToStrFunDef() {
034        super("TupleToStr", "Constructs a string from a tuple.", "fSt");
035    }
036
037    public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
038        if (TypeUtil.couldBeMember(call.getArg(0).getType())) {
039            final MemberCalc memberCalc =
040                    compiler.compileMember(call.getArg(0));
041            return new AbstractStringCalc(call, new Calc[] {memberCalc}) {
042                public String evaluateString(Evaluator evaluator) {
043                    final Member member =
044                            memberCalc.evaluateMember(evaluator);
045                    if (member.isNull()) {
046                        return "";
047                    }
048                    StringBuilder buf = new StringBuilder();
049                    buf.append(member.getUniqueName());
050                    return buf.toString();
051                }
052            };
053        } else {
054            final TupleCalc tupleCalc =
055                    compiler.compileTuple(call.getArg(0));
056            return new AbstractStringCalc(call, new Calc[] {tupleCalc}) {
057                public String evaluateString(Evaluator evaluator) {
058                    final Member[] members =
059                            tupleCalc.evaluateTuple(evaluator);
060                    if (members == null) {
061                        return "";
062                    }
063                    StringBuilder buf = new StringBuilder();
064                    SetToStrFunDef.appendTuple(buf, members);
065                    return buf.toString();
066                }
067            };
068        }
069    }
070
071}
072
073// End TupleToStrFunDef.java