001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/fun/TupleToStrFunDef.java#5 $
003    // This software is subject to the terms of the Common Public License
004    // Agreement, available at the following URL:
005    // http://www.opensource.org/licenses/cpl.html.
006    // Copyright (C) 2006-2008 Julian Hyde
007    // All Rights Reserved.
008    // You must accept the terms of that agreement to use this software.
009    */
010    package mondrian.olap.fun;
011    
012    import mondrian.calc.Calc;
013    import mondrian.calc.ExpCompiler;
014    import mondrian.calc.MemberCalc;
015    import mondrian.calc.TupleCalc;
016    import mondrian.calc.impl.AbstractStringCalc;
017    import mondrian.mdx.ResolvedFunCall;
018    import mondrian.olap.type.TypeUtil;
019    import mondrian.olap.Evaluator;
020    import mondrian.olap.Member;
021    
022    /**
023     * Definition of the <code>TupleToStr</code> MDX function.
024     *
025     * <p>Syntax:
026     * <blockquote><code>
027     * TupleToStr(&lt;Tuple&gt;)
028     * </code></blockquote>
029     *
030     * @author jhyde
031     * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/TupleToStrFunDef.java#5 $
032     * @since Aug 3, 2006
033     */
034    class TupleToStrFunDef extends FunDefBase {
035        static final TupleToStrFunDef instance = new TupleToStrFunDef();
036    
037        private TupleToStrFunDef() {
038            super("TupleToStr", "Constructs a string from a tuple.", "fSt");
039        }
040    
041        public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
042            if (TypeUtil.couldBeMember(call.getArg(0).getType())) {
043                final MemberCalc memberCalc =
044                        compiler.compileMember(call.getArg(0));
045                return new AbstractStringCalc(call, new Calc[] {memberCalc}) {
046                    public String evaluateString(Evaluator evaluator) {
047                        final Member member =
048                                memberCalc.evaluateMember(evaluator);
049                        if (member.isNull()) {
050                            return "";
051                        }
052                        StringBuilder buf = new StringBuilder();
053                        buf.append(member.getUniqueName());
054                        return buf.toString();
055                    }
056                };
057            } else {
058                final TupleCalc tupleCalc =
059                        compiler.compileTuple(call.getArg(0));
060                return new AbstractStringCalc(call, new Calc[] {tupleCalc}) {
061                    public String evaluateString(Evaluator evaluator) {
062                        final Member[] members =
063                                tupleCalc.evaluateTuple(evaluator);
064                        if (members == null) {
065                            return "";
066                        }
067                        StringBuilder buf = new StringBuilder();
068                        SetToStrFunDef.appendTuple(buf, members);
069                        return buf.toString();
070                    }
071                };
072            }
073        }
074    
075    }
076    
077    // End TupleToStrFunDef.java