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) 2001-2005 Julian Hyde
008// Copyright (C) 2005-2011 Pentaho and others
009// All Rights Reserved.
010*/
011package mondrian.olap;
012
013import mondrian.parser.*;
014import mondrian.resource.MondrianResource;
015import mondrian.server.Statement;
016
017import org.apache.log4j.Logger;
018
019/**
020 * <code>ConnectionBase</code> implements some of the methods in
021 * {@link Connection}.
022 *
023 * @author jhyde
024 * @since 6 August, 2001
025 */
026public abstract class ConnectionBase implements Connection {
027
028    protected ConnectionBase() {
029    }
030
031    protected abstract Logger getLogger();
032
033
034    public String getFullConnectString() {
035        String s = getConnectString();
036        String catalogName = getCatalogName();
037        if (catalogName != null) {
038            int len = s.length() + catalogName.length() + 32;
039            StringBuilder buf = new StringBuilder(len);
040            buf.append(s);
041            if (!s.endsWith(";")) {
042                buf.append(';');
043            }
044            buf.append("Initial Catalog=");
045            buf.append(catalogName);
046            buf.append(';');
047            s = buf.toString();
048        }
049        return s;
050    }
051
052    public abstract Statement getInternalStatement();
053
054    public Query parseQuery(String query) {
055        return (Query) parseStatement(query);
056    }
057
058    /**
059     * Parses a query, with specified function table and the mode for strict
060     * validation(if true then invalid members are not ignored).
061     *
062     * <p>This method is only used in testing and by clients that need to
063     * support customized parser behavior. That is why this method is not part
064     * of the Connection interface.
065     *
066     * <p>See test case mondrian.olap.CustomizedParserTest.
067     *
068     * @param statement Evaluation context
069     * @param query MDX query that requires special parsing
070     * @param funTable Customized function table to use in parsing
071     * @param strictValidation If true, do not ignore invalid members
072     * @return Query the corresponding Query object if parsing is successful
073     * @throws MondrianException if parsing fails
074     */
075    public QueryPart parseStatement(
076        Statement statement,
077        String query,
078        FunTable funTable,
079        boolean strictValidation)
080    {
081        MdxParserValidator parser = createParser();
082        boolean debug = false;
083
084        if (funTable == null) {
085            funTable = getSchema().getFunTable();
086        }
087
088        if (getLogger().isDebugEnabled()) {
089            //debug = true;
090            getLogger().debug(
091                Util.nl
092                + query);
093        }
094
095        try {
096            return
097                parser.parseInternal(
098                    statement, query, debug, funTable, strictValidation);
099        } catch (Exception e) {
100            throw MondrianResource.instance().FailedToParseQuery.ex(query, e);
101        }
102    }
103
104    protected MdxParserValidator createParser() {
105        return true
106            ? new JavaccParserValidatorImpl()
107            : new MdxParserValidatorImpl();
108    }
109}
110
111// End ConnectionBase.java