001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/ConnectionBase.java#25 $
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) 2001-2002 Kana Software, Inc.
007    // Copyright (C) 2001-2008 Julian Hyde and others
008    // All Rights Reserved.
009    // You must accept the terms of that agreement to use this software.
010    //
011    // jhyde, 6 August, 2001
012    */
013    
014    package mondrian.olap;
015    
016    import mondrian.resource.MondrianResource;
017    
018    import org.apache.log4j.Logger;
019    
020    /**
021     * <code>ConnectionBase</code> implements some of the methods in
022     * {@link Connection}.
023     *
024     * @author jhyde
025     * @since 6 August, 2001
026     * @version $Id: //open/mondrian/src/main/mondrian/olap/ConnectionBase.java#25 $
027     */
028    public abstract class ConnectionBase implements Connection {
029    
030        public static void memoryUsageNotification(Query query, String msg) {
031            query.setOutOfMemory(msg);
032        }
033    
034        protected ConnectionBase() {
035        }
036    
037        protected abstract Logger getLogger();
038    
039    
040        public String getFullConnectString() {
041            String s = getConnectString();
042            String catalogName = getCatalogName();
043            if (catalogName != null) {
044                int len = s.length() + catalogName.length() + 32;
045                StringBuilder buf = new StringBuilder(len);
046                buf.append(s);
047                if (!s.endsWith(";")) {
048                    buf.append(';');
049                }
050                buf.append("Initial Catalog=");
051                buf.append(catalogName);
052                buf.append(';');
053                s = buf.toString();
054            }
055            return s;
056        }
057    
058        public Query parseQuery(String query) {
059            return parseQuery(query, null, false, false);
060        }
061    
062        public Query parseQuery(String query, boolean load) {
063            return parseQuery(query, null, load, false);
064        }
065    
066        /**
067         * Parses a query, with specified function table and the mode for strict
068         * validation(if true then invalid members are not ignored).
069         *
070         * <p>This method is only used in testing and by clients that need to
071         * support customized parser behavior. That is why this method is not part
072         * of the Connection interface.
073         *
074         * @param query MDX query that requires special parsing
075         * @param funTable Customized function table to use in parsing
076         * @param strictValidation If true, do not ignore invalid members
077         * @return Query the corresponding Query object if parsing is successful
078         * @throws MondrianException if parsing fails
079         * @see mondrian.olap.CustomizedParserTest
080         */
081        public Query parseQuery(String query, FunTable funTable,
082            boolean strictValidation) {
083            return parseQuery(query, funTable, false, strictValidation);
084        }
085    
086        public Exp parseExpression(String expr) {
087            boolean debug = false;
088            if (getLogger().isDebugEnabled()) {
089                //debug = true;
090                StringBuilder buf = new StringBuilder(256);
091                buf.append(Util.nl);
092                buf.append(expr);
093                getLogger().debug(buf.toString());
094            }
095            try {
096                Parser parser = new Parser();
097                final FunTable funTable = getSchema().getFunTable();
098                Exp q = parser.parseExpression(this, expr, debug, funTable);
099                return q;
100            } catch (Throwable exception) {
101                throw
102                    MondrianResource.instance().FailedToParseQuery.ex(
103                        expr,
104                        exception);
105            }
106        }
107    
108        private Query parseQuery(String query, FunTable cftab, boolean load,
109            boolean strictValidation) {
110            Parser parser = new Parser();
111            boolean debug = false;
112            final FunTable funTable;
113    
114            if (cftab == null) {
115                funTable = getSchema().getFunTable();
116            } else {
117                funTable = cftab;
118            }
119    
120            if (getLogger().isDebugEnabled()) {
121                //debug = true;
122                StringBuilder buf = new StringBuilder(256);
123                buf.append(Util.nl);
124                buf.append(query);
125                getLogger().debug(buf.toString());
126            }
127    
128            try {
129                Query q =
130                    parser.parseInternal(this, query, debug, funTable, load,
131                        strictValidation);
132                return q;
133            } catch (Throwable e) {
134                throw MondrianResource.instance().FailedToParseQuery.ex(query, e);
135            }
136        }
137    }
138    
139    // End ConnectionBase.java